GestureDetector手势初识

本文详细介绍了GestureDetector类在手势识别中的应用,包括其提供的接口OnGestureListener和OnDoubleTapListener,以及如何通过SimpleOnGestureListener实现手势操作。文章还提供了具体的实现代码示例。

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

关于手势,大神的博客:http://blog.youkuaiyun.com/harvic880925/article/details/39520901
用于处理一些复杂的手势活动。

//GestureDetector 手势

GestureDetector 这个类对外提供了两个接口和一个外部类

    接口:OnGestureListener   OnDoubleTapListener(双击的监听)
    外部类:SimpleOnGestureListener 
        这个外部类,其实是两个接口中所有函数的集成,它包含了这两个接口里所有必须要实现的函数而且都已经重写,但所有方法体都是空的;不同点在于:该类是static class,程序员可以在外部继承这个类,重写里面的手势处理方法。

    private class gesturelistener implements GestureDetector.OnGestureListener{

        //手指轻轻的触摸屏幕的一瞬间,由1个ACTION_DOWN触发
        public boolean onDown(MotionEvent e) {
            // TODO Auto-generated method stub
            return false;
        }
        //手指轻轻触摸屏幕,尚未松开或拖动   注意和onDown()的区别,没有松开或拖动的状态
        public void onShowPress(MotionEvent e) {
            // TODO Auto-generated method stub

        }
        //单击行为
            /*
                触发顺序:
                    点击一下非常快的(不滑动)Touchup:
                        onDown()->onSingleTapUp()->onSingleTapConfirmed()
                    点击一下稍微慢点的(不滑动)Touchup:
                        onDown()->onShowPress()->onSingleTapUp()->onSingleTapConfirmed()
            */
        public boolean onSingleTapUp(MotionEvent e) {
            // TODO Auto-generated method stub
            return false;
        }
        //手指按下屏幕并拖动
            /*
                滑屏:
                    onDown()->onScroll()->onScroll()->...->onFling()
                拖动:
                    onDown()->onScroll()->onScroll()->...->onFling()
            */
        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                float distanceX, float distanceY) {
            // TODO Auto-generated method stub
            return false;
        }
        //用户长久地按着屏幕不放,即长按
            //触发的顺序是onDown()->onShowPress()->onLongPress()
        public void onLongPress(MotionEvent e) {
            // TODO Auto-generated method stub

        }
        //用户按下触摸屏,快速滑动后松开
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            // TODO Auto-generated method stub
            return false;
        }

    }       

//使用GestureDetector

    /*
        分三步走:
            1、 创建OnGestureListener监听函数
                GestureDetector.OnGestureListener listener=new GestureDetector.OnGestureListener(){
                    //实现抽象方法
                };
            2、创建GestureDetector实例mGestureDetector
                构造函数有三个:
                    GestureDetector gestureDetector=new GestureDetector(GestureDetector.OnGestureListener listener);

                    GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.OnGestureListener listener); 

                    GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.SimpleOnGestureListener listener); 

            3、onTouch(View v,MotionEvent event)中拦截: 

                    public boolean onTouch(View v,MotionEvent event){
                        return mGestureDetector.onTouchEvent(event);
                    }
            4、绑定控件
                TextView tv=(TextView)findViewById(R.id.tv);
                tv.setOnTouchListener(this);
    */

    //最简单的代码
    public class MainActivity extends Activity implements OnTouchListener{

        private GestureDetector mGestureDetector;   
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            mGestureDetector = new GestureDetector(new gestureListener()); //使用派生自OnGestureListener

            TextView tv = (TextView)findViewById(R.id.tv);
            tv.setOnTouchListener(this);
        }


        /* 
            在onTouch()方法中,我们调用GestureDetector的onTouchEvent()方法,将捕捉到的MotionEvent交给GestureDetector 来分析是否有合适的callback函数来处理用户的手势 
        */  
        public boolean onTouch(View v, MotionEvent event) {
            return mGestureDetector.onTouchEvent(event);   
        }

        private class gestureListener implements GestureDetector.OnGestureListener{
            //实现抽象方法
        };
    }

//GestureDetector.OnDoubleTapListener--接口

待续~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值