在View被触摸时,会收到Framework传来的MotionEvent实例,封装了输入事件种类,坐标等信息,因此,要熟悉View事件的处理,首先要熟悉MotionEvent的一些常用方法和属性。
输入事件的种类
输入事件的种类比较多,一般常用的也就下面几种:
ACTION_DOWN:按压动作的开始,是整个动作的开始
ACTION_UP:按压动作的结束,是整个动作的结束
ACTION_MOVE:手指在按压动作开始到结束有移动的动作。介于ACTION_DOWN和ACTION_UP之间的一个动作
ACTION_CANCEL:当前手势被终止,不会再收到任何位置信息
ACTION_POINTER_DOWN:另外的手指(不是第一个)开始按压动作
ACTION_POINTER_UP:另外的手指(不是第一个)按压动作结束
从MotionEvent中得到输入事件的种类
private static native int nativeGetAction(long nativePtr);
MotionEvent是通过native方法获取输入事件的种类信息,不过,返回的int类型的数据包含两个信息:手指的索引和动作的种类。
调用getAction方法,会获取包含Action的种类和手指的索引的信息
/**
* 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.
*/
//包含Action的种类和手指的索引
public