Gesture手势、触碰

Gesture 学习记录

  • gesture是什么
  • 有什么用途
  • 和touchEvent的区别

Gesture初探,和onTouch监听的区别

  1. 提供了众多的方法,程序员需要做的只是实现,不用像OntouchListener那样还需要区别Event的类型

使用方法

一。 在GestureDetector中有静态类SimpleOnGestureListener,在事件的处理实现在内置的重载方法中实现

    `GestureDetector.SimpleOnGestureListener SimpleOnGestureListener = new GestureDetector.SimpleOnGestureListener() {`

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            // TODO Auto-generated method stub
            android.util.Log.i("GESTure","onDoubleTap");
            return super.onDoubleTap(e);
        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {
            // TODO Auto-generated method stub
            android.util.Log.i("GESTure","onDoubleTapEvent");
            return super.onDoubleTapEvent(e);
        }

        @Override
        public boolean onDown(MotionEvent e) {
            // TODO Auto-generated method stub
            return super.onDown(e);
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2,
                float velocityX, float velocityY) {
            // TODO Auto-generated method stub
            android.util.Log.i("GESTure","onFling");
            return super.onFling(e1, e2, velocityX, velocityY);
        }

        @Override
        public void onLongPress(MotionEvent e) {
            // TODO Auto-generated method stub
            android.util.Log.i("GESTure","onLongPress");
            super.onLongPress(e);
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                float distanceX, float distanceY) {
            // TODO Auto-generated method stub
            android.util.Log.i("GESTure","onScroll");
            return super.onScroll(e1, e2, distanceX, distanceY);
        }

        @Override
        public void onShowPress(MotionEvent e) {
            // TODO Auto-generated method stub
            android.util.Log.i("GESTure","onShowPress");
            super.onShowPress(e);
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            // TODO Auto-generated method stub
            android.util.Log.i("GESTure","onSingleTapConfirmed");
            return super.onSingleTapConfirmed(e);
        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            // TODO Auto-generated method stub
            android.util.Log.i("GESTure","onSingleTapUp");
            return super.onSingleTapUp(e);
        }
    };

    final GestureDetector detector = new GestureDetector(this, SimpleOnGestureListener);
    findViewById(R.id.dummy_button).setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            // TODO Auto-generated method stub
            return detector.onTouchEvent(arg1);
        }
    });

一般情况下我们不需要关注所有的处理可以选择性的重载
选中SimpleOnGestureListener右键sourse

二 . 将SimpleOnGestureListener对象通过构造函数的参数的形式传入GestureDector中

        final GestureDetector detector = new GestureDetector(this, SimpleOnGestureListener);

三 . 设置控件的的OnTouchListener,在onTouch(View ,MotionEvent)方法中返回GestureDetector的onTouchEvent(MotionEvent);

 findViewById(R.id.dummy_button).setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                // TODO Auto-generated method stub
                return detector.onTouchEvent(arg1);
            }
        });

测试结果:方法的执行顺序

1 单击(快速)

06-02 11:49:23.098: I/GESTure(16339): onDown
06-02 11:49:23.188: I/GESTure(16339): onSingleTapUp
06-02 11:49:23.398: I/GESTure(16339): onSingleTapConfirmed

onSingleTapConfirmed只有单击的时候才会执行,意为单击事件确认

2 双击

06-02 11:50:18.248: I/GESTure(16339): onDown
06-02 11:50:18.328: I/GESTure(16339): onSingleTapUp
06-02 11:50:18.428: I/GESTure(16339): onDoubleTap
06-02 11:50:18.428: I/GESTure(16339): onDoubleTapEvent
06-02 11:50:18.428: I/GESTure(16339): onDown
06-02 11:50:18.518: I/GESTure(16339): onDoubleTapEvent

3 滑动

  06-02 11:50:43.358: I/GESTure(16339): onDown
06-02 11:50:43.478: I/GESTure(16339): onScroll
06-02 11:50:43.498: I/GESTure(16339): onScroll
06-02 11:50:43.508: I/GESTure(16339): onScroll
06-02 11:50:43.528: I/GESTure(16339): onScroll
06-02 11:50:43.548: I/GESTure(16339): onScroll
06-02 11:50:43.558: I/GESTure(16339): onScroll
06-02 11:50:43.578: I/GESTure(16339): onScroll
06-02 11:50:43.598: I/GESTure(16339): onFling

4 点击

06-02 11:51:07.158: I/GESTure(16339): onDown
06-02 11:51:07.338: I/GESTure(16339): onShowPress
06-02 11:51:07.428: I/GESTure(16339): onSingleTapUp
06-02 11:51:07.458: I/GESTure(16339): onSingleTapConfirmed

5 长按

06-02 11:51:41.888: I/GESTure(16339): onDown
06-02 11:51:42.068: I/GESTure(16339): onShowPress
06-02 11:51:42.568: I/GESTure(16339): onLongPress

10/13/2015 2:35:12 PM

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值