Android手势识别

GestureDetector —— 手势识别类
sdk源代码:

public class GestureDetector {

    public interface OnGestureListener {

        boolean onDown(MotionEvent e);

        void onShowPress(MotionEvent e);

        boolean onSingleTapUp(MotionEvent e);

        boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY);

        void onLongPress(MotionEvent e);

        boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY);
    }

    public interface OnDoubleTapListener {

        boolean onSingleTapConfirmed(MotionEvent e);

        boolean onDoubleTap(MotionEvent e);

        boolean onDoubleTapEvent(MotionEvent e);
    }

SimpleOnGestureListener

 /**
     * A convenience class to extend when you only want to listen for a subset
     * of all the gestures. This implements all methods in the
     * {@link OnGestureListener} and {@link OnDoubleTapListener} but does
     * nothing and return {@code false} for all applicable methods.
     */

        //touch down后又没有滑动(onScroll),又没有长按(onLongPress),然后Touchup时触发
        public boolean onSingleTapUp(MotionEvent e) {
            return false;
        }

        public void onLongPress(MotionEvent e) {
        }

        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                float distanceX, float distanceY) {
            return false;
        }

        //Touch了滑动一点距离后,up时触发
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            return false;
        }

        //Touch了还没有滑动时触发 
        public void onShowPress(MotionEvent e) {
        }

        //Touch down时触发 
        public boolean onDown(MotionEvent e) {
            return false;
        }

        //双击的第二下Touch down时触发 
        public boolean onDoubleTap(MotionEvent e) {
            return false;
        }

        //双击的第二下Touch down和up都会触发,可用e.getAction()区分。 
        public boolean onDoubleTapEvent(MotionEvent e) {
            return false;
        }

        //touch down后又没有滑动(onScroll),又没有长按(onLongPress),然后Touchup时触发
        public boolean onSingleTapConfirmed(MotionEvent e) {
            return false;
        }
    }

点击一下非常快的(不滑动)Touchup:
onDown->onSingleTapUp->onSingleTapConfirmed
点击一下稍微慢点的(不滑动)Touchup:
onDown->onShowPress->onSingleTapUp->onSingleTapConfirmed

部分示例代码:

GestureDetector detector new GestureDetector(this, new SimpleOnGestureListener() {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2,
                    float velocityX, float velocityY) {
        if(Math.abs(e2.getRawY() - e1.getRawY()) > 100) {
            return true;
        }

        if(e2.getRawX() - e1.getRawX() > 200) {
        //显示上一个页面,从左往右滑动
        showLast();
        return true;

        }
        if(e1.getRawX() - e2.getRawX() > 200) {
            //显示下一个页面,从右往左滑动
            showNext();
            return true;
        }
        return super.onFling(e1, e2, velocityX, velocityY);

    }

});

使用GestureDetector需要在View中重写onTouchEvent事件。

@Override
    public boolean onTouchEvent(MotionEvent event) {
        detector.onTouchEvent(event);
        return super.onTouchEvent(event);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值