手势交互之GestureDetector

GsetureDetector

一.交互过程

  1. 触屏的一刹那,触发MotionEvent事件
  2. 被OnTouchListener监听,在onTouch()中获得MotionEvent对象
  3. GestureDetector转发MotionEvent对象到OnGestureListener
  4. OnGestureListener获得该对象,根据该对象封装的信息做出合适的反馈

二.内部类

1.OnGestureListener:单击类

方法
  1. 单击onDown(MotionEvent e)
  2. 抬起onSingleTapUp(MotionEvent e)
  3. 短按onShowPress(MotionEvent e)
  4. 长按onLongPress(MotionEvent e)
  5. 滚动onScroll(MotionEvent e1,MotionEvent e2,float distanceX,float distanceY)
  6. 滑动onFling(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY)

2.OnDoubleTapListener:双击

方法
  1. 双击OnDoubleTap(MotionEvent e)
  2. 双击按下和抬起各触发一次:onDoubleTapEvent(MotionEvent e)?应用例子?
  3. 单击确定onSingleTapConfirmed(MotionEvent e)?

3.SimpleOnGestureListener

概述: SimpleOnGestureListener实现了上面的连个接口 OnGestureListener and OnDoubleTapListener,可以通过继承这个类来实现你所想实现的手势交互动作。需要的动作可以在接口里找对应的方法,如果方法返回false就是什么都不做。

三.运用例子

效果

这个例子在主界面加载一个图片,然后左右滑动提示相应的文字。

过程
  1. OnTouchListener监听,在onTouch中获得MotionEvent对象
img.setOnTouchListener(new OnTouchListener() {            
            @Override//捕获的触摸屏发生的event事件
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                return true;//记得这里改为ture
            }
        });
  1. 在Activity建立一个继承于SimpleOnGestureListener的内部类
class MyGestureListener extends SimpleOnGestureListener{
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            // TODO Auto-generated method stub
            //e1和e2分别是开始和结束的动作对象
            //通过e1和e2位置的比较判断手势的动作
            if (e1.getX() - e2.getX() > 50) {
                    Toast.makeText(MainActivity.this, "从右向左滑动",0).show();
            }else if(e2.getX() - e1.getX() >50){
                Toast.makeText(MainActivity.this, "从左向右滑动",0).show();
            }
            return super.onFling(e1, e2, velocityX, velocityY);
        }
    }


      3.在onCreate方法中获得一个GestureDetextor对象(声明省略了)

    gestureDetector = new GestureDetector(MainActivity.this, new MyGestureListener());

      4.在onTouch方法中gestureDetector转发MotionEvent对象到OnGestureListener(这里是MyGestureListener)

    gestureDetector.onTouchEvent(event);

      5.MyGestureListener获得该对象,根据该对象封装的信息做出合适的反馈(在图片上左右滑动提示对应的文字)

 完整代码

public class MainActivity extends Activity {
    ImageView img;
    GestureDetector gestureDetector;
    //3.这就是继承于一个SimpleOnGestureListener来利用对应的方法
    //这里演示了一个滑动的手势效果
    class MyGestureListener extends SimpleOnGestureListener{
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            // TODO Auto-generated method stub
            //e1和e2分别是开始和结束的动作对象
            //通过e1和e2位置的比较判断手势的动作
            if (e1.getX() - e2.getX() > 50) {
                Toast.makeText(MainActivity.this, "从右向左滑动",0).show();
            }else if(e2.getX() - e1.getX() >50){
                Toast.makeText(MainActivity.this, "从左向右滑动",0).show();
            }
            return super.onFling(e1, e2, velocityX, velocityY);
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        img = (ImageView) findViewById(R.id.img);
        //4.获得gestureDetector对象
        gestureDetector = new GestureDetector(MainActivity.this, new MyGestureListener());
        //1.交互过程第一部是用户点击触发了MotionEvent时间
        //2.下面是第二步,OnTouchListener监听,在onTouch中获得MotionEvent对象
        img.setOnTouchListener(new OnTouchListener() {
            
            @Override//捕获的触摸屏发生的event事件
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                //5.gestureDetector转发MotionEvent对象到OnGestureListener(这里是MyGestureListener)
                gestureDetector.onTouchEvent(event);
                //这里要改为true
                return true;
            }
        }); 
        
            
        }
    }
View Code
原文地址:https://www.cnblogs.com/Mihai/p/5577876.html