上一篇介绍的onTouch提供的事件还是相对较简单,如果需要处理一些复杂的手势,用这个接口就会很麻烦,因为我们要根据用户触摸的轨迹去判断是什么手势。幸好Android SDK给我们提供了GestureDetector类,通过这个类我们可以识别很多的手势,主要是通过他的onTouchEvent(event)方法完成了不同手势的识别。
GestureDetector这个类对外提供了两个接口和一个外部类:
OnGestureListener有下面的几个动作:
OnDoubleTapListener有下面的几个动作:
关于onSingleTapConfirmed和onSingleTapUp的区别:onSingleTapUp,只要手抬起就会执行,而对于onSingleTapConfirmed来说,如果双击的话,则onSingleTapConfirmed不会执行。
要使用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);
}
}
实现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);
}
}
使用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下载地址