(1)首先是MotionEvent 中getAction()与getActionMasked()的区别:
public static final int ACTION_MASK = 0xff;
/**
Return the kind of action being performed. Consider using getActionMasked() and getActionIndex() to retrieve the separate masked action and pointer index.
Returns:
The action, such as ACTION_DOWN or the combination of ACTION_POINTER_DOWN with a shifted pointer index.
*/
public final int getAction() {
return nativeGetAction(mNativePtr);
}
/**
Return the masked action being performed, without pointer index information. Use getActionIndex() to return the index associated with pointer actions.
Returns:
The action, such as ACTION_DOWN or ACTION_POINTER_DOWN.
*/
public final int getActionMasked() {
return nativeGetAction(mNativePtr) & ACTION_MASK;
}
代码摘自[android 4.3] [设定 nativeGetAction(mNativePtr)的返回值为 mAction]
他们有什么区别呢?如果nativeGetAction(mNativePtr)的值是在0x00到0xff之间的话。getAction()返回的值,和
getActionMasked()的返回的值是一样的。
(Q1)那什么时候返回的值是一样的呢?即当nativeGetAction(mNativePtr)值大于0xff时,那什么时候会大于0xff呢?
这就是是当有多点触控时。当有多点触控时nativeGetAction(mNativePtr)返回值的低8位即0x00到0xff用来表示动作的类型信息。
例如:MotionEvent#ACTION_DOWN的值是 0,即0x00。
MotionEvent#ACTION_UP的值是 1,即0x01。
等等。
但是,我们知道Android是支持多点触控的,那么怎么知道这个一个MotionEvent是哪一个触控点触发的呢?那么就还需要MotionEvent带有触控点索引信息。
Android的解决方案时在nativeGetAction(mNativePtr)返回值的后8位中存储。
例如,如果mAction的值是0x0000,则表示是第一个触控点的ACTION_DOWN操作。
如果mAction的值是0x0100呢,则表示是第二个触控点的ACTION_DOWN操作。
第三个的ACTION_DOWN呢?相信你可以推出来是0x0200。
总而言之,mAction时的低8位(也就是0-7位)是动作类型信息。 mAction的8-15位呢,是触控点的索引信息。(即表示是哪一个触控点的事件)。
摘自: http://my.oschina.net/banxi/blog/56421