Android手势GestureDetector分析(二)——源码

GestureDetector是Android中用于处理触摸屏手势的类,它定义了如onDown、onSingleTapUp等接口,用于监听不同类型的手势。通过OnGestureListener和OnDoubleTapListener接口,可以实现单击、双击、长按等操作。同时,文章提到了构造器和设置监听器的方法,以及如何处理MotionEvent来触发相应的回调函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(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类型来触发不同的回调函数操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雪舞飞影

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值