Android触控基础 MotionEvent

转自:http://www.cnphp6.com/archives/59948?utm_source=tuicool&utm_medium=referral

一、一些常量

常见的动作常量:

    public static final int ACTION_DOWN             = 0;单点触摸动作

    public static final int ACTION_UP               = 1;单点触摸离开动作
    public static final int ACTION_MOVE             = 2;触摸点移动动作
    public static final int ACTION_CANCEL           = 3;触摸动作取消
     public static final int ACTION_OUTSIDE          = 4;触摸动作超出边界
    public static final int ACTION_POINTER_DOWN     = 5;多点触摸动作
    public static final int ACTION_POINTER_UP       = 6;多点离开动作

   以下是一些非touch事件
    public static final int ACTION_HOVER_MOVE       = 7;
    public static final int ACTION_SCROLL           = 8;
    public static final int ACTION_HOVER_ENTER      = 9;
    public static final int ACTION_HOVER_EXIT       = 10;

掩码常量

ACTION_MASK = 0X000000ff动作掩码
 ACTION_POINTER_INDEX_MASK = 0X0000ff00触摸点索引掩码

ACTION_POINTER_INDEX_SHIFT = 8 获取触摸点索引需要移动的位数

另注:对于TextView来说,只有Action_DOWN能被检测,Action_up, action_cancel都检测不到,讲TextView控件换成Button则都能检测到MotionEvent事件。一般action_cancel事件必须实现,因为当快速滑过button时,action_up则不被检测执行,需要action_cancel来执行事件。

二、相关方法

getAction()方法返回的是int类型,用到的只有低16位,其中:低八位是动作的类型,高8位是触摸点索引值的表示(单点为0,双点为1)

获得动作类型: int action = event.getAction() & ACTION_MASK 或者使用 getActionMasked()

获得触摸点索引类型: int pointerIndex = (event.getAction() & ACTION_POINTER_INDEX_MASK ) >> ACTION_POINTER_INDEX_SHIFT 

或者使用 getActionIndex()

为什么要有索引信息?

有了索引信息,我们可以在onTOuchEvent事件中判断传进来的MotionEvent对象对应的是单点信息还是多点信息。

下面的代码段能使用户在屏幕上拖动一个对象。它记录了初始点的位置,计算点移动的距离,并将对象移动到新的位置。它正确的处理了这种情况:当第一个手指把控件拖到一个位置,然后按下第二个手指,且第二个手指与同一个控件上。当用户抬起第一个手指时,控件不会跑到第二个手指的位置同时第二个手指可以继续拖动控件。

[java] view plaincopy

  1. // The ‘active pointer’ is the one currently moving our object.  

  2. private int mActivePointerId = INVALID_POINTER_ID;  

  3.   

  4. @Override  

  5. public boolean onTouchEvent(MotionEvent ev) {  

  6.     // Let the ScaleGestureDetector inspect all events.  

  7.     mScaleDetector.onTouchEvent(ev);  

  8.                

  9.     final int action = MotionEventCompat.getActionMasked(ev);   

  10.           

  11.     switch (action) {   

  12.     case MotionEvent.ACTION_DOWN: {  

  13.         final int pointerIndex = MotionEventCompat.getActionIndex(ev);   

  14.         final float x = MotionEventCompat.getX(ev, pointerIndex);   

  15.         final float y = MotionEventCompat.getY(ev, pointerIndex);   

  16.               

  17.         // Remember where we started (for dragging)  

  18.         mLastTouchX = x;  

  19.         mLastTouchY = y;  

  20.         // Save the ID of this pointer (for dragging)  

  21.         mActivePointerId = MotionEventCompat.getPointerId(ev, 0);  

  22.         break;  

  23.     }  

  24.               

  25.     case MotionEvent.ACTION_MOVE: {  

  26.         // Find the index of the active pointer and fetch its position  

  27.         final int pointerIndex =   

  28.                 MotionEventCompat.findPointerIndex(ev, mActivePointerId);    

  29.               

  30.         final float x = MotionEventCompat.getX(ev, pointerIndex);  

  31.         final float y = MotionEventCompat.getY(ev, pointerIndex);  

  32.               

  33.         // Only move if the ScaleGestureDetector isn’t processing a gesture.  

  34.         if (!mScaleDetector.isInProgress()) {  

  35.             // Calculate the distance moved  

  36.             final float dx = x – mLastTouchX;  

  37.             final float dy = y – mLastTouchY;  

  38.   

  39.             mPosX += dx;  

  40.             mPosY += dy;  

  41.   

  42.             invalidate();  

  43.         }  

  44.         // Remember this touch position for the next move event  

  45.         mLastTouchX = x;  

  46.         mLastTouchY = y;  

  47.   

  48.         break;  

  49.     }  

  50.               

  51.     case MotionEvent.ACTION_UP: {  

  52.         mActivePointerId = INVALID_POINTER_ID;  

  53.         break;  

  54.     }  

  55.               

  56.     case MotionEvent.ACTION_CANCEL: {  

  57.         mActivePointerId = INVALID_POINTER_ID;  

  58.         break;  

  59.     }  

  60.           

  61.     case MotionEvent.ACTION_POINTER_UP: {  

  62.               

  63.         final int pointerIndex = MotionEventCompat.getActionIndex(ev);   

  64.         final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);   

  65.   

  66.         if (pointerId == mActivePointerId) {  

  67.             // This was our active pointer going up. Choose a new  

  68.             // active pointer and adjust accordingly.  

  69.             final int newPointerIndex = pointerIndex == 0 ? 1 : 0;  

  70.             mLastTouchX = MotionEventCompat.getX(ev, newPointerIndex);   

  71.             mLastTouchY = MotionEventCompat.getY(ev, newPointerIndex);   

  72.             mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);  

  73.         }  

  74.         break;  

  75.     }  

  76.     }         

  77.     return true;  

  78. }  

MotionEvent还包含了移动操作中其它历史移动数据以方便处理触控的移动操作.

android sdk对于这个类的描述中就有这么一句:

For efficiency, motion events with ACTION_MOVE may batch together multiple movement samples within a single object.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值