触控两点,onTouchEvent

本文介绍两种实现屏幕对角点击弹出对话框的方法:一是通过重写onTouchEvent方法捕捉多点触摸事件;二是分别在屏幕两个对角放置View并监听点击事件。
项目中遇到一个需求,需要同时点击屏幕的一个对角弹出一个对话框,查阅相关资料,找到一个常用的方法,即重写onTouchEvent,在方法中做相关处理,自己写了个demo,代码如下package com.example.twotouch;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.Toast;

public class MainActivity extends Activity {
    private int x0;
    private int y0;
    private int x1;
    private int y1;
    private int width;
    private int height;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取屏幕宽高
        WindowManager systemService = (WindowManager)getSystemService(WINDOW_SERVICE);
        width = systemService.getDefaultDisplay().getWidth();
        height = systemService.getDefaultDisplay().getHeight();
    }
    @Override

    public boolean onTouchEvent(MotionEvent event) {

//获取点击点的个数

        int pointerCount = event.getPointerCount();
        int action = event.getAction();
        if (pointerCount==1) {
            switch (action) {
            case MotionEvent.ACTION_DOWN:
                 x0 = (int)event.getX(0);
                 y0=(int)event.getY(0);

                System.out.println("ACTION_DOWN pointerCount="+pointerCount);
                break;
            case MotionEvent.ACTION_MOVE:
                System.out.println("ACTION_MOVE pointerCount="+pointerCount);
                break;
            case MotionEvent.ACTION_UP:
                System.out.println("ACTION_UP pointerCount="+pointerCount);
                break;

            default:
                break;
            }
        }
        
        if (pointerCount==2) {
             x0 = (int)event.getX(0);
             y0=(int)event.getY(0);
             x1 = (int)event.getX(1);
             y1 = (int)event.getY(1);
            switch (action) {
            case MotionEvent.ACTION_DOWN:
                System.out.println("ACTION_DOWN pointerCount="+pointerCount);
                break;
            case MotionEvent.ACTION_MOVE:
                System.out.println("ACTION_MOVE pointerCount="+pointerCount);
                break;
            case MotionEvent.ACTION_UP:
                System.out.println("ACTION_UP pointerCount="+pointerCount);
                break;
            case MotionEvent.ACTION_POINTER_1_DOWN:
                System.out.println("ACTION_POINTER_1_DOWN pointerCount="+pointerCount);
                break;
            case MotionEvent.ACTION_POINTER_1_UP:
                System.out.println("ACTION_POINTER_1_UP pointerCount="+pointerCount);
                break;
            case MotionEvent.ACTION_POINTER_2_DOWN:
                if (x0<width*0.5 && y0<height*0.5) {
                    if (x1>width*0.5 && y1>height*0.5) {
                        Toast.makeText(getApplicationContext(), "弹出窗口", Toast.LENGTH_SHORT).show();
                    }
                    
                }
                System.out.println("ACTION_POINTER_2_DOWN pointerCount="+pointerCount);
                
                break;
            case MotionEvent.ACTION_POINTER_2_UP:
                System.out.println("ACTION_POINTER_2_UP pointerCount="+pointerCount);
                break;

            default:
                break;
            }
            
        }
        return super.onTouchEvent(event);
    }
}

还有一种相对简单的方法就是在屏幕的对角个写以一个view给view写点击事件

代码如下

package com.example.touch;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{

    private View viewTop;
    private View viewBotton;
    private boolean isOnclickTop = false;
    private boolean isOnclickBottom = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        viewTop = findViewById(R.id.view_top);
        viewBotton = findViewById(R.id.view_bottom);
        
        initView();
    }

    private void initView() {
        // TODO Auto-generated method stub
        viewTop.setOnClickListener(this);
        viewBotton.setOnClickListener(this);
        
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.view_top:
            isOnclickTop=true;
            
            new CountDownTimer(100,100) {
                
                @Override
                public void onTick(long arg0) {
                    // TODO Auto-generated method stub
                    
                }
                
                @Override
                public void onFinish() {
                    // TODO Auto-generated method stub
                    isOnclickTop=false;
                }
            }.start();
            
            break;
        case R.id.view_bottom:
            isOnclickBottom=true;
            
            new Thread(new Runnable() {//也可以开启一个新的线程
                
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    isOnclickBottom=false;
                }
            }).start();
            
//            new CountDownTimer(100,100) {
//                
//                @Override
//                public void onTick(long arg0) {
//                    // TODO Auto-generated method stub
//                    
//                }
//                
//                @Override
//                public void onFinish() {
//                    // TODO Auto-generated method stub
//                    isOnclickBottom=false;
//                }
//            }.start();
            
            break;
            

        default:
            break;
        }
        if (isOnclickTop==true&&isOnclickBottom==true) {
            Toast.makeText(getApplicationContext(), "弹出窗口", Toast.LENGTH_SHORT).show();
        }
        
    }
}

布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >

    <View
        android:id="@+id/view_top"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#000000"
         />

    <View
        android:id="@+id/view_bottom"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:background="#ff0000" />

</RelativeLayout>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值