Android触控基础:MotionEvent

以前的文章层从Framework层介绍了Android Touch事件即(MotionEvent)的传递机制。本文将详细介绍MotionEvent的一些成员和方法。了解了MotionEvent对开发一些特效如拖动控件或多点缩放控件有很大的做用。同时,掌握MotionEvent类也是学好android触控技术的基础。java

1、一些常量android

常见的动做常量:ide

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

    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;
this

掩码常量spa

ACTION_MASK = 0X000000ff动做掩码
 ACTION_POINTER_INDEX_MASK = 0X0000ff00触摸点索引掩码
.net

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

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

2、相关方法blog

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.
相关文章
相关标签/搜索