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

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



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);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值