GestureDetector
概述
该类主要是用于识别一些特定的手势,我们只需要调用GestureDetector.onTouchEvent(),并把MotionEvent传递进去即可。对于各种手势的回调,可以通过GestureDetector中的接口OnGestureListener来完成。
只需要在View#onTouchEvent()中调用GestureDetector#onTouchEvent()即可。
OnGestureListener
onDown
/**
* Notified when a tap occurs with the down {@link MotionEvent}
* that triggered it. This will be triggered immediately for
* every down event. All other events should be preceded by this.
*
* @param e The down motion event.
*/
boolean onDown(MotionEvent e);
在GestureDetector.onTouchEvent()中,当action_down发生时,该方法肯定会立即被调用。源码为:
case MotionEvent.ACTION_DOWN:
……
if (mIsLongpressEnabled) {
mHandler.removeMessages(LONG_PRESS);
mHandler.sendEmptyMessageAtTime(LONG_PRESS, mCurrentDownEvent.getDownTime()
+ TAP_TIMEOUT + LONGPRESS_TIMEOUT);
}
mHandler.sendEmptyMessageAtTime(SHOW_PRESS, mCurrentDownEvent.getDownTime() + TAP_TIMEOUT);
handled |= mListener.onDown(ev);
break;
从中可以看出action_down事件发生时,一定会触发onDown()事件。而隔了TAG_TIMEOUT后才会触发onShowPress();隔TAP_TIMEOUT+LONGPRESS_TIMEOUT后才会触发onLongPress()。
GestureDetector.onTouchEvent()的返回值就是最后一句中的handled,而在每一次调用该方法时handled都会被初始化成false。因此,该方法最终会影响onTouchEvent()的返回值,如果onDown()返回true,那么onTouchEvent()一定会返回true;反之不一定返回false(因为省略的部分对handled进行了修改)。
onShowPress
/**
* The user has performed a down {@link MotionEvent} and not performed
* a move or up yet. This event is commonly used to provide visual
* feedback to the user to let them know that their action has been
* recognized i.e. highlight an element.
*
* @param e The down motion event
*/
void onShowPress(MotionEvent e);
在action_down时,会通过handler发送一个消息(SHOW_PRESS),代码为:
mHandler.sendEmptyMessageAtTime(SHOW_PRESS, mCurrentDownEvent.getDownTime() + TAP_TIMEOUT);
从这行代码可以看出,在down事件发生时,并不会立即执行onShowPress(),会等待一段时间后才执行。并且如果在该时间段内,发生了action_move,action_up或者ACTION_CANCEL,还会取消执行该方法。因此,该方法就是在用户真正进行按下事件时才执行,并不是ACTION_DOWN时。因为ACTION_DOWN后,用户可能进行move也可能进行press。
onDown()会在action_down事件时就执行,无论该次触摸会发展成press还是scroll。而onShowPress只有在事件是press(按下足够长的时间,并且没有大范围移动)时才会执行,在该方法中一般用来提示用户你已按下,而不能用来单击事件的操作,因为有可能会发现成onLongPress。
onLongPress
与onShowPress()类似,只不过按的时候长时执行onLongPress,时间短时执行onShowPress()。从onDown中的源码可以看出,当mIsLongpressEnabled=true时,才有可能执行onLongPress()。
在new GestureDetector时,该值默认设置为true。可以通过setIsLongpressEnabled()进行设置。