多点触控事件处理 ev.getAction() & MotionEvent.ACTION_MASK

转自 http://www.glmei.cn/?p=3084


把之前学过的东西都打包,方便以后调用。
    今天来个多点触控的,其实挺简单。
    网上教程大把。不过自己能体会才是自己的。写写自己的体会。
    第一个主要的点是Activity中的onTouchEvent(MotionEvent event);
    Override onTouchEvent(…)为以下形式:
    onTouchEvent(MotionEvent event){
          return super.onTouchEvent(event);
    }
    默认的情况下super.onTouchEvent(…)返回false,所以只有一个触摸点被传送。当把return的值改成true的时候,系统在得到true指令后,获得触摸点的监听器并没有被置失效,多点触控就得以实现了。当把值改为true之后,在方法内加个输出语句标记一下,可以看见event在多点触控的情况下,提取event内触碰点个数,会有有相应改变。不断得到屏幕事件的各种值,通过switch语句的筛选,就可以得到屏幕受到的触摸的情况。下面贴代码。其实跟GestureDetector常用方法差不多。
import android.view.MotionEvent;

public class CMultiTouchManager {

    private OnCMultiTouchListener cOnCMultiTouchListener = null;

    public CMultiTouchManager(){}
    public void setOnCMultiTouchListener(OnCMultiTouchListener listener){
        if (listener == null) {
            throw new IllegalStateException(“Listener could be null !”);
        }
        this.cOnCMultiTouchListener=listener;
    }
    public CMultiTouchManager(OnCMultiTouchListener listener) {
        if (listener == null) {
            throw new IllegalStateException(“Listener could be null !”);
        }
        this.cOnCMultiTouchListener = listener;
    }

    public void putTouchEvent(MotionEvent event) {
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            CMultiTouchManager.this.cOnCMultiTouchListener.onDown(
                    event.getPointerId(event.getPointerCount() – 1),
                    event.getX(), event.getY());
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            CMultiTouchManager.this.cOnCMultiTouchListener.onDown(
                    event.getPointerId(event.getPointerCount() – 1),
                    event.getX(), event.getY());
            break;
        case MotionEvent.ACTION_UP:
            CMultiTouchManager.this.cOnCMultiTouchListener.onUp(
                    event.getPointerId(event.getPointerCount() – 1),
                    event.getX(), event.getY());
            break;
        case MotionEvent.ACTION_POINTER_UP:
            CMultiTouchManager.this.cOnCMultiTouchListener.onUp(
                    event.getPointerId(event.getPointerCount() – 1),
                    event.getX(), event.getY());
            break;
        case MotionEvent.ACTION_MOVE:
            for (int i = 0; i < event.getPointerCount(); i++) {
                CMultiTouchManager.this.cOnCMultiTouchListener.onMove(
                        event.getPointerId(i), event.getX(i), event.getY(i));
            }
            break;
        }
        //在以上筛选代码中,down和up条件都是简单的只要确定在哪里按下跟抬起就可以。而每次move的条件都需要得到所有触摸点的坐标,才能及时调整,例如几个点同时滑动,需同时知道这几个点的滑动路径。

    }
}

interface OnCMultiTouchListener {
    public void onDown(int id, float x, float y);

    public void onUp(int id, float x, float y);

    public void onMove(int id, float x, float y);
}

    写好后,可以看到代码中有一个与运算(红字体部分)。目的是什么呢?
    先看看各动作的全局变量的内容:                               
  public static final int ACTION_MASK = 255=0000000011111111;
  public static final int ACTION_DOWN = 0=00000000;
  public static final int ACTION_UP = 00000001;
  public static final int ACTION_MOVE = 00000010;
  public static final int ACTION_CANCEL = 00000011;
  public static final int ACTION_OUTSIDE = 00000100;
  public static final int ACTION_POINTER_DOWN = 00000101;
  public static final int ACTION_POINTER_1_DOWN = 00000101;
  public static final int ACTION_POINTER_2_DOWN = 100000101;
  public static final int ACTION_POINTER_3_DOWN = 1000000101;
  public static final int ACTION_POINTER_UP = 00000110;
  public static final int ACTION_POINTER_1_UP = 00000110;
  public static final int ACTION_POINTER_2_UP = 100000110;
  public static final int ACTION_POINTER_3_UP = 1000000110;
  由以上可知,当ACTION_MASK&ACTION_POINTER_1/2/3_DOWN/UP=ACTION_MASK&ACTION_POINTER_DOWN/UP;
  从而可将不同的触摸归类为一个类。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值