您的位置:首页 > 移动开发 > Android开发

android MotionEvent getAction,getActionMasked,getActionIndex区别

2017-03-20 14:18 357 查看
        开发屏幕事件冲突时,比如上层View只需要用到Move事件,内部View需要用到cancel,down等其他事件,这样的话就涉及到哪个View处理哪个事件。然后就需要用到getAction,getActionMasked,getActionIndex。       看源码:       getAction:   /**
* Return the kind of action being performed.
* Consider using {@link #getActionMasked} and {@link #getActionIndex} to retrieve
* the separate masked action and pointer index.
* @return The action, such as {@link #ACTION_DOWN} or
* the combination of {@link #ACTION_POINTER_DOWN} with a shifted pointer index.
*/
 public final int getAction() {
return nativeGetAction(mNativePtr);
}
意思就是说:返回的是响应的事件,考虑使用getActionMasked或者getActionIndex来得到分隔开的动作和手指的下标。返回的一般是ACTION_DOWN,合并起来的已经转移的手指下标的ACTION_POINTER_DOWN。
getActionMasked:
/**
* Return the masked action being performed, without pointer index information.
* Use {@link #getActionIndex} to return the index associated with pointer actions.
* @return The action, such as {@link #ACTION_DOWN} or {@link #ACTION_POINTER_DOWN}.
*/
 public final int getActionMasked() {
return nativeGetAction(mNativePtr) & ACTION_MASK;
}
意思就是说:返回的是具体的响应事件,没有手指下标的信息。可以使用getActionIndex来返回和手指动作相关的下标,返回的值:比如:ACTION_DOWN,ACTION_POINTER_DOWN。
getActionIndex:
/**
* For {@link #ACTION_POINTER_DOWN} or {@link #ACTION_POINTER_UP}
* as returned by {@link #getActionMasked}, this returns the associated
* pointer index.
* The index may be used with {@link #getPointerId(int)},
* {@link #getX(int)}, {@link #getY(int)}, {@link #getPressure(int)},
* and {@link #getSize(int)} to get information about the pointer that has
* gone down or up.
* @return The index associated with the action.
*/
 public final int getActionIndex() {
return (nativeGetAction(mNativePtr) & ACTION_POINTER_INDEX_MASK)
>> ACTION_POINTER_INDEX_SHIFT;
}
意思就是说:返回的是如果调用getActionMasked返回ACTION_POINTER_DOWN或者ACTION_POINTER_UP,这个方法返回的是ACTION_POINTER_DOWN,ACTION_POINTER_UP相关的下标,这个下标可能会在getPointerId,getX,getY,getPressed,getSize中用到来获取手指的关于按下或者抬起的信息。
这中间用的比较多的就是位操作。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android开发