Android的手势操作(Gesture)

上一篇介绍的onTouch提供的事件还是相对较简单,如果需要处理一些复杂的手势,用这个接口就会很麻烦,因为我们要根据用户触摸的轨迹去判断是什么手势。幸好Android SDK给我们提供了GestureDetector类,通过这个类我们可以识别很多的手势,主要是通过他的onTouchEvent(event)方法完成了不同手势的识别。

介绍

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

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

OnGestureListener有下面的几个动作:

  • onDown(MotionEvent e):用户按下屏幕就会触发。
  • onShowPress(MotionEvent e):如果按下的时间超过瞬间,而且在按下的时候没有松开或者是拖动的,那么onShowPress就会执行,具体这个瞬间是多久,我也不清楚…
  • onLongPress(MotionEvent e):长按触摸屏,超过一定时长,就会触发这个事件。
    触发顺序:onDown->onShowPress->onLongPress
  • onSingleTapUp(MotionEvent e):一次单独的轻击抬起操作,也就是轻击一下屏幕,立刻抬起来,才会有这个触发,当然,如果除了Down以外还有其它操作,那就不再是Single操作了,所以也就不会触发这个事件。
    触发顺序:
    点击一下非常快的(不滑动)Touchup:
    onDown->onSingleTapUp->onSingleTapConfirmed
    点击一下稍微慢点的(不滑动)Touchup:
    onDown->onShowPress->onSingleTapUp->onSingleTapConfirmed
  • onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) :滑屏,用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE, 1个ACTION_UP触发。
    参数解释:
    e1:第1个ACTION_DOWN MotionEvent
    e2:最后一个ACTION_MOVE MotionEvent
    velocityX:X轴上的移动速度,像素/秒
    velocityY:Y轴上的移动速度,像素/秒
  • onScroll(MotionEvent e1, MotionEvent e2,float distanceX, float distanceY):在屏幕上拖动事件。无论是用手拖动view,或者是以抛的动作滚动,都会多次触发,这个方法在ACTION_MOVE动作发生时就会触发。

OnDoubleTapListener有下面的几个动作:

  • onSingleTapConfirmed(MotionEvent e):单击事件。用来判定该次点击是SingleTap而不是DoubleTap,如果连续点击两次就是DoubleTap手势,如果只点击一次,系统等待一段时间后没有收到第二次点击则判定该次点击为SingleTap而不是DoubleTap,然后触发SingleTapConfirmed事件。
    触发顺序是:OnDown->OnsingleTapUp->OnsingleTapConfirmed
  • onDoubleTap(MotionEvent e):双击事件。
  • onDoubleTapEvent(MotionEvent e):双击间隔中发生的动作。指触发onDoubleTap以后,在双击之间发生的其它动作,包含down、up和move事件。

关于onSingleTapConfirmed和onSingleTapUp的区别:onSingleTapUp,只要手抬起就会执行,而对于onSingleTapConfirmed来说,如果双击的话,则onSingleTapConfirmed不会执行。

使用Gesture

使用GestureDetector.OnGestureListener接口

要使用OnGestureListener接口,大致有几步要走:
1、创建OnGestureListener监听函数:

private class gestureListener implements GestureDetector.OnGestureListener{  

}

2、创建GestureDetector实例:
构造函数有下面三个,根据需要选择:

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

注:GestureDetector现在已经是deprecation状态,现在推荐GestureDetectorCompat

GestureDetectorCompat gestureDetectorCompat=new GestureDetectorCompat(Context context,GestureDetector.OnGestureListener listener);
GestureDetectorCompat gestureDetectorCompat=new GestureDetectorCompat(Context context,GestureDetector.OnGestureListener listener, Handler handler); 

3、onTouch(View v, MotionEvent event)中拦截,在onTouch()方法中,我们调用GestureDetector的onTouchEvent()方法,将捕捉到的MotionEvent交给GestureDetector来分析是否有合适的callback函数来处理用户的手势

public boolean onTouch(View v, MotionEvent event) {  
    return gestureDetectorCompat.onTouchEvent(event);     
}  

4、控件绑定

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

实现代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent">  

    <TextView  
        android:id="@+id/tv"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
        android:layout_margin="50dip"  
        android:background="#76EE00"  
        android:text="Gesture Detector" />    
</RelativeLayout> 
public class MainActivity extends Activity implements OnTouchListener{        
    private GestureDetectorCompat mGestureDetectorCompat;     

    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
      super.onCreate(savedInstanceState);  
      setContentView(R.layout.activity_main);            
      mGestureDetectorCompat = new GestureDetectorCompat(this, new gestureListener());           
      TextView tv = (TextView)findViewById(R.id.tv);  
      tv.setOnTouchListener(this);  
      tv.setFocusable(true);     
      tv.setClickable(true);     
      tv.setLongClickable(true);   
    }  

    public boolean onTouch(View v, MotionEvent event) {  
        return mGestureDetectorCompat.onTouchEvent(event);     
    }  

    private class gestureListener implements GestureDetector.OnGestureListener{   
        public boolean onDown(MotionEvent e) {  
            showlog("onDown");     
            Toast.makeText(MainActivity.this, "onDown", Toast.LENGTH_SHORT).show();     
            return false;  
        }  

        public void onShowPress(MotionEvent e) {
            showlog("onShowPress");     
            Toast.makeText(MainActivity.this, "onShowPress", Toast.LENGTH_SHORT).show();     
        }  

        public boolean onSingleTapUp(MotionEvent e) {  
            showlog("onSingleTapUp");     
            Toast.makeText(MainActivity.this, "onSingleTapUp", Toast.LENGTH_SHORT).show();     
            return true;     
        }  

        public boolean onScroll(MotionEvent e1, MotionEvent e2,  
                float distanceX, float distanceY) {  
            showlog("onScroll:"+(e2.getX()-e1.getX()) +"   "+distanceX);     
            Toast.makeText(MainActivity.this, "onScroll", Toast.LENGTH_LONG).show();           
            return true;     
        }  

        public void onLongPress(MotionEvent e) {  
             showlog("onLongPress");     
             Toast.makeText(MainActivity.this, "onLongPress", Toast.LENGTH_LONG).show();     
        }  

        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
                float velocityY) {  
            showlog("onFling");     
            Toast.makeText(MainActivity.this, "onFling", Toast.LENGTH_LONG).show();     
            return true;  
        }  
    };  

    public void showlog(String info) {
        System.out.print("GestureDetector "+info);
    }  
}  

使用GestureDetector.OnDoubleTapListener接口

实现OnDoubleTapListener接口的前提是先实现OnGestureListener接口,其实除了第1步,2、3、4步和上面完全一样,不再赘述,下面看下第一步:

同时创建OnGestureListener和OnDoubleTapListener监听函数:
方法一:新建一个类同时派生自OnGestureListener和OnDoubleTapListener:

private class gestureListener implements GestureDetector.OnGestureListener,GestureDetector.OnDoubleTapListener{  
    }

方法二:使用GestureDetector.setOnDoubleTapListener();函数设置监听:

/构建GestureDetector实例     
mGestureDetector = new GestureDetector(new gestureListener()); 
private class gestureListener implements GestureDetector.OnGestureListener{        
}  

//设置双击监听器  
mGestureDetector.setOnDoubleTapListener(new doubleTapListener());  
private class doubleTapListener implements GestureDetector.OnDoubleTapListener{      
}  

注:大家可以看到无论在方法一还是在方法二中,都需要派生自GestureDetector.OnGestureListener,前面我们说过GestureDetectorCompat 的构造函数,如下:

GestureDetectorCompat gestureDetectorCompat=new GestureDetectorCompat(Context context,GestureDetector.OnGestureListener listener);
GestureDetectorCompat gestureDetectorCompat=new GestureDetectorCompat(Context context,GestureDetector.OnGestureListener listener, Handler handler);  

可以看到,它的两个构造函数参数都必须是OnGestureListener的实例。所以要想使用OnDoubleTapListener的几个函数,就必须先实现OnGestureListener。
实现代码:

public class MainActivity extends Activity implements OnTouchListener{        
    private GestureDetectorCompat mGestureDetectorCompat;     

    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
      super.onCreate(savedInstanceState);  
      setContentView(R.layout.activity_main);            
      mGestureDetectorCompat = new GestureDetectorCompat(this, new gestureListener());           
      TextView tv = (TextView)findViewById(R.id.tv);  
      tv.setOnTouchListener(this);  
      tv.setFocusable(true);     
      tv.setClickable(true);     
      tv.setLongClickable(true);   
    }  

    public boolean onTouch(View v, MotionEvent event) {  
        return mGestureDetectorCompat.onTouchEvent(event);     
    }  

    private class gestureListener implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener{   
        public boolean onDown(MotionEvent e) {  
            showlog("onDown");     
            Toast.makeText(MainActivity.this, "onDown", Toast.LENGTH_SHORT).show();     
            return false;  
        }  

        public void onShowPress(MotionEvent e) {
            showlog("onShowPress");     
            Toast.makeText(MainActivity.this, "onShowPress", Toast.LENGTH_SHORT).show();     
        }  

        public boolean onSingleTapUp(MotionEvent e) {  
            showlog("onSingleTapUp");     
            Toast.makeText(MainActivity.this, "onSingleTapUp", Toast.LENGTH_SHORT).show();     
            return true;     
        }  

        public boolean onScroll(MotionEvent e1, MotionEvent e2,  
                float distanceX, float distanceY) {  
            showlog("onScroll:"+(e2.getX()-e1.getX()) +"   "+distanceX);     
            Toast.makeText(MainActivity.this, "onScroll", Toast.LENGTH_LONG).show();           
            return true;     
        }  

        public void onLongPress(MotionEvent e) {  
             showlog("onLongPress");     
             Toast.makeText(MainActivity.this, "onLongPress", Toast.LENGTH_LONG).show();     
        }  

        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
                float velocityY) {  
            showlog("onFling");     
            Toast.makeText(MainActivity.this, "onFling", Toast.LENGTH_LONG).show();     
            return true;  
        }

        public boolean onSingleTapConfirmed(MotionEvent e) {  
            showlog("onSingleTapConfirmed");  
            Toast.makeText(MainActivity.this, "onSingleTapConfirmed",Toast.LENGTH_LONG).show();  
            return true;  
        }  

        public boolean onDoubleTap(MotionEvent e) {  
            showlog("onDoubleTap");  
            Toast.makeText(MainActivity.this, "onDoubleTap", Toast.LENGTH_LONG).show();  
            return true;  
        }

        public boolean onDoubleTapEvent(MotionEvent e) {  
            showlog("onDoubleTapEvent");  
            Toast.makeText(MainActivity.this, "onDoubleTapEvent", Toast.LENGTH_LONG).show();  
            return true;  
        }

    public void showlog(String info) {
        System.out.print("GestureDetector "+info);
    }  
}  

使用GestureDetector.SimpleOnGestureListener类

使用OnGestureListener和OnDoubleTapListener接口,这样需要重载接口所有的方法,适合监听所有的手势,如果我们只想监听某个手势或某几个手势呢,这时候就可以使用SimpleOnGestureListener类了。
它与前两个不同的是:
1、这是一个类,在它基础上新建类的话,要用extends派生而不是用implements继承!
2、OnGestureListener和OnDoubleTapListener接口里的函数都是强制必须重写的,即使用不到也要重写出来一个空函数但在SimpleOnGestureListener类的实例或派生类中不必如此,可以根据情况,用到哪个函数就重写哪个函数,因为SimpleOnGestureListener类本身已经实现了这两个接口的所有函数,只是里面全是空的而已。

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 simpleGestureListener());            
        TextView tv = (TextView)findViewById(R.id.tv);  
        tv.setOnTouchListener(this);  
        tv.setFocusable(true);     
        tv.setClickable(true);     
        tv.setLongClickable(true);   
    }  

    public boolean onTouch(View v, MotionEvent event) {  
        return mGestureDetector.onTouchEvent(event);     
    }  

    private class simpleGestureListener extends GestureDetector.SimpleOnGestureListener {  

        /*****OnGestureListener的函数*****/  
        public boolean onDown(MotionEvent e) {  
            Log.i("MyGesture", "onDown");  
            Toast.makeText(MainActivity.this, "onDown", Toast.LENGTH_SHORT).show();  
            return false;  
        }  

        public void onShowPress(MotionEvent e) {  
            Log.i("MyGesture", "onShowPress");  
            Toast.makeText(MainActivity.this, "onShowPress", Toast.LENGTH_SHORT).show();  
        }  

        public boolean onSingleTapUp(MotionEvent e) {  
            Log.i("MyGesture", "onSingleTapUp");  
            Toast.makeText(MainActivity.this, "onSingleTapUp",  
                    Toast.LENGTH_SHORT).show();  
            return true;  
        }  

        /*****OnDoubleTapListener的函数*****/  
        public boolean onSingleTapConfirmed(MotionEvent e) {  
            Log.i("MyGesture", "onSingleTapConfirmed");  
            Toast.makeText(MainActivity.this, "onSingleTapConfirmed",Toast.LENGTH_LONG).show();  
            return true;  
        }  

        public boolean onDoubleTap(MotionEvent e) {  
            Log.i("MyGesture", "onDoubleTap");  
            Toast.makeText(MainActivity.this, "onDoubleTap", Toast.LENGTH_LONG).show();  
            return true;  
        }  
    }  
}  

Demo下载地址

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值