一个用来判断是长按手势的辅助类

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_28656671/article/details/51471702

     一个用来判断是长按手势的辅助类



import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.view.MotionEvent;
import android.view.ViewConfiguration;

/**
 * Created by yyw on 2016/5/6.
 * 一个用来判断是长按手势的辅助类
 */
public class LongPressDetector {
    //开始位置
    private float srcX = 0;
    //开始位置
    private float srcY = 0;
    //被判断为滑动的最小距离
    private int minToScroll;
    //被判断为长按的最小时间
    private int minTimeToLongPress;
    //当前的触摸事件
    private MotionEvent currentEvent;
    private DetectorHandler mHandler;
    //回调接口
    private OnLongPressListener longPressListener;
    public LongPressDetector(Context context, OnLongPressListener longPressListener) {
        minTimeToLongPress = ViewConfiguration.getLongPressTimeout();
        minToScroll = ViewConfiguration.get(context).getScaledTouchSlop();
        mHandler = new DetectorHandler(Looper.getMainLooper());
        this.longPressListener = longPressListener;
    }

    /**
     * 在{@link android.view.View#onTouchEvent(MotionEvent)}中调用该方法。
     * 当手指在一个地方停留一段时间后会调用{@link OnLongPressListener#onLongPress(MotionEvent)}方法
     * @param e MotionEvent
     */
    public void onTouchEvent(MotionEvent e) {

        switch (e.getAction()) {
            case MotionEvent.ACTION_DOWN:
                currentEvent = MotionEvent.obtain(e);
                mHandler.removeCallbacks(mLongPress);//清除动作
                srcX = e.getX();//初始化位置信息
                srcY = e.getY();
                mHandler.postDelayed(mLongPress, minTimeToLongPress);//开始一个延时请求
                break;
            case MotionEvent.ACTION_MOVE:
                float x = e.getX();
                float y = e.getY();
                if (Math.abs(srcX - x) > minToScroll || Math.abs(srcY - y) > minToScroll) {//判断当前的移动距离是否可以判断为滑动,如果是滑动就重置
                    mHandler.removeCallbacks(mLongPress);
                }
                break;
            case MotionEvent.ACTION_UP:
                mHandler.removeCallbacks(mLongPress);//当手指抬起就清除动作
                break;
            case MotionEvent.ACTION_CANCEL:
                mHandler.removeCallbacks(mLongPress);//当事件结束就清除动作
                break;
        }
    }

    private class DetectorHandler extends Handler {
        public DetectorHandler(Looper looper) {
            super(looper);
        }
    }

    private Runnable mLongPress = new Runnable() {
        @Override
        public void run() {
            if (longPressListener != null){
                longPressListener.onLongPress(currentEvent);
            }
        }
    };

    public OnLongPressListener getLongPressListener() {
        return longPressListener;
    }

    public void setLongPressListener(OnLongPressListener longPressListener) {
        this.longPressListener = longPressListener;
    }

    /**
     * 当手指在一个地方停留一段时间后会调用{@link OnLongPressListener#onLongPress(MotionEvent)}方法
     */
    public interface OnLongPressListener{
        /**
         * 当手指在一个地方停留一段时间后会调用{@link OnLongPressListener#onLongPress(MotionEvent)}方法
         * @param e MotionEvent 当前的点击事件
         */
        void onLongPress(MotionEvent e);
    }
}


猜你喜欢

转载自blog.csdn.net/qq_28656671/article/details/51471702