Android手势GestureDetector分析(二)——源码
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
1接口定义相关
//frameworks/base/core/java/android/view/GestureDetector.java
public class GestureDetector {
//OnGestureListener
public interface OnGestureListener {
//Notified when a tap occurs with the down that triggered it.
boolean onDown(@NonNull MotionEvent e);
//The user has performed a down and not performed a move or up yet.
void onShowPress(@NonNull MotionEvent e);
//Notified when a tap occurs with the up that triggered it.
boolean onSingleTapUp(@NonNull MotionEvent e);
//Notified when a scroll occurs with the initial on down and the current move.
boolean onScroll(@NonNull MotionEvent e1, @NonNull MotionEvent e2, float distanceX, float distanceY);
//Notified when a long press occurs with the initial on down that trigged it.
void onLongPress(@NonNull MotionEvent e);
//Notified of a fling event when it occurs with the initial on down and the matching up.
boolean onFling(@NonNull MotionEvent e1, @NonNull MotionEvent e2, float velocityX, float velocityY);
}
//OnDoubleTapListener
public interface OnDoubleTapListener {
//Notified when a single-tap occurs.
boolean onSingleTapConfirmed(@NonNull MotionEvent e);
//Notified when a double-tap occurs. Triggered on the down event of second tap.
boolean onDoubleTap(@NonNull MotionEvent e);
//Notified when an event within a double-tap gesture occurs, including the down, move, and up events.
boolean onDoubleTapEvent(@NonNull MotionEvent e);
}
//OnContextClickListener
public interface OnContextClickListener {
//Notified when a context click occurs.
boolean onContextClick(@NonNull MotionEvent e);
}
}
// A convenience class to extend when you only want to listen for a subset of all the gestures.
public static class SimpleOnGestureListener implements OnGestureListener, OnDoubleTapListener, OnContextClickListener {
public boolean onSingleTapUp(@NonNull MotionEvent e) {
return false;
}
public void onLongPress(@NonNull MotionEvent e) {
}
public boolean onScroll(@NonNull MotionEvent e1, @NonNull MotionEvent e2, float distanceX, float distanceY) {
return false;
}
public boolean onFling(@NonNull MotionEvent e1, @NonNull MotionEvent e2, float velocityX, float velocityY) {
return false;
}
public void onShowPress(@NonNull MotionEvent e) {
}
public boolean onDown(@NonNull MotionEvent e) {
return false;
}
public boolean onDoubleTap(@NonNull MotionEvent e) {
return false;
}
public boolean onDoubleTapEvent(@NonNull MotionEvent e) {
return false;
}
public boolean onSingleTapConfirmed(@NonNull MotionEvent e) {
return false;
}
public boolean onContextClick(@NonNull MotionEvent e) {
return false;
}
}
2构造器和监听器相关
private final OnGestureListener mListener;
private OnDoubleTapListener mDoubleTapListener;
private OnContextClickListener mContextClickListener;
public GestureDetector(@Nullable @UiContext Context context,
@NonNull OnGestureListener listener, @Nullable Handler handler) {
if (handler != null) {
mHandler = new GestureHandler(handler);
} else {
mHandler = new GestureHandler();
}
mListener = listener;
if (listener instanceof OnDoubleTapListener) {
setOnDoubleTapListener((OnDoubleTapListener) listener);
}
if (listener instanceof OnContextClickListener) {
setContextClickListener((OnContextClickListener) listener);
}
init(context);
}
public void setOnDoubleTapListener(@Nullable OnDoubleTapListener onDoubleTapListener) {
mDoubleTapListener = onDoubleTapListener;
}
public void setContextClickListener(@Nullable OnContextClickListener onContextClickListener) {
mContextClickListener = onContextClickListener;
}
3触发函数
public boolean onTouchEvent(@NonNull MotionEvent ev) {
final int action = ev.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_POINTER_DOWN:
//...
break;
case MotionEvent.ACTION_POINTER_UP:
//...
break;
case MotionEvent.ACTION_DOWN:
//...
break;
case MotionEvent.ACTION_MOVE:
//...
break;
case MotionEvent.ACTION_UP:
//...
break;
case MotionEvent.ACTION_CANCEL:
//...
break;
}
return handled;
}
通过不同的MotionEvent类型来触发不同的回调函数操作。