开发同学都知道自定义view的时候可以重写onTouch()方法,进而扩展按下、移动、松开这三个函数,这也是常用的形式。但是这个方法太过简单,如果需要处理一些复杂的手势,用这个接口就会很麻烦。Android其实有一个手势库——GestureDetector,已经为我们封装了一些常用的手势方法,接下来就总结一下GestureDetector的使用和总结。
GestureDetector使用:
1.实现方式
GestureDetector的实现方式根据类别分有两种:
- 实现两个接口的相关方法:OnGestureListener,OnDoubleTapListener
- 继承一个内部类进而重写其相关方法:SimpleOnGestureListener
其中OnGestureListener,OnDoubleTapListener,可视情况递增实现,即必须实现OnGestureListener接口,OnDoubleTapListener可根据情况追加。而SimpleOnGestureListener其实是OnGestureListener,OnDoubleTapListener这两个接口中所有函数的集成,它包含了这两个接口里所有必须要实现的函数。
2.创建GestureDetector实例
根据第一步的实现方式,创建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);
3.onTouch(View v, MotionEvent event)中拦截
public boolean onTouch(View v, MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}
4.控件绑定
假如我们在某个View上进行手势操作,那么需要绑定当前View的OnTouchListener监听。
TextView tv = (TextView)findViewById(R.id.tv);
tv.setOnTouchListener(this);
GestureDetector.OnGestureListener
基本方法
private class gesturelistener implements GestureDetector.OnGestureListener{
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
// TODO Auto-generated method stub
return false;
}
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;
}
}
这里总共重写了六个方法:
1.OnDown(MotionEvent e):
用户按下屏幕就会触发;
2.onShowPress(MotionEvent e):
如果是按下的时间超过瞬间,而且在按下的时候没有松开或者是拖动的,那么onShowPress就会执行。
3.onLongPress(MotionEvent e):
长按触摸屏,超过一定时长,就会触发这个事件。触发顺序:onDown->onShowPress->onLongPress
4.onSingleTapUp(MotionEvent e):
从名子也可以看出,一次单独的轻击抬起操作,也就是轻击一下屏幕,立刻抬起来,才会有这个触发,当然,如果除了Down以外还有其它操作,那就不再算是Single操作了,所以也就不会触发这个事件。触发顺序:
点击一下非常快的(不滑动)Touchup:onDown->onSingleTapUp->onSingleTapConfirmed
点击一下稍微慢点的(不滑动)Touchup:onDown->onShowPress->onSingleTapUp->onSingleTapConfirmed
5.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轴上的移动速度,像素/秒
6.onScroll(MotionEvent e1, MotionEvent e2,float distanceX, float distanceY):
在屏幕上拖动事件。无论是用手拖动view,或者是以抛的动作滚动,都会多次触发,这个方法在ACTION_MOVE动作发生时就会触发。
滑屏,手指触动屏幕后,稍微滑动后立即松开:onDown-----》onScroll----》onScroll----》onScroll----》………----->onFling
拖动:onDown------》onScroll----》onScroll------》onFiling
可见,无论是滑屏,还是拖动,影响的只是中间OnScroll触发的数量多少而已,最终都会触发onFling事件!
例子
结合我们开篇所讲的GestureDetector使用步骤,下边演示一个例子:
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);
tv.setFocusable(true);
tv.setClickable(true);
tv.setLongClickable(true);
}
/*
* 在onTouch()方法中,我们调用GestureDetector的onTouchEvent()方法,将捕捉到的MotionEvent交给GestureDetector
* 来分析是否有合适的callback函数来处理用户的手势
*/
public boolean onTouch(View v, MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}
private class gestureListener implements GestureDetector.OnGestureListener{
// 用户轻触触摸屏,由1个MotionEvent ACTION_DOWN触发
public boolean onDown(MotionEvent e) {
Log.i("MyGesture", "onDown");
Toast.makeText(MainActivity.this, "onDown", Toast.LENGTH_SHORT).show();
return false;
}
/*
* 用户轻触触摸屏,尚未松开或拖动,由一个1个MotionEvent ACTION_DOWN触发
* 注意和onDown()的区别,强调的是没有松开或者拖动的状态
*
* 而onDown也是由一个MotionEventACTION_DOWN触发的,但是他没有任何限制,
* 也就是说当用户点击的时候,首先MotionEventACTION_DOWN,onDown就会执行,
* 如果在按下的瞬间没有松开或者是拖动的时候onShowPress就会执行,如果是按下的时间超过瞬间
* (这块我也不太清楚瞬间的时间差是多少,一般情况下都会执行onShowPress),拖动了,就不执行onShowPress。
*/
public void onShowPress(MotionEvent e) {
Log.i("MyGesture", "onShowPress");
Toast.makeText(MainActivity.this, "onShowPress", Toast.LENGTH_SHORT).show();
}
// 用户(轻触触摸屏后)松开,由一个1个MotionEvent ACTION_UP触发
///轻击一下屏幕,立刻抬起来,才会有这个触发
//从名子也可以看出,一次单独的轻击抬起操作,当然,如果除了Down以外还有其它操作,那就不再算是Single操作了,所以这个事件 就不再响应
public boolean onSingleTapUp(MotionEvent e) {
Log.i("MyGesture", "onSingleTapUp");
Toast.makeText(MainActivity.this, "onSingleTapUp", Toast.LENGTH_SHORT).show();
return true;
}
// 用户按下触摸屏,并拖动,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE触发
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
Log.i("MyGesture22", "onScroll:"+(e2.getX()-e1.getX()) +" "+distanceX);
Toast.makeText(MainActivity.this, "onScroll", Toast.LENGTH_LONG).show();
return true;
}
// 用户长按触摸屏,由多个MotionEvent ACTION_DOWN触发
public void onLongPress(MotionEvent e) {
Log.i("MyGesture", "onLongPress");
Toast.makeText(MainActivity.this, "onLongPress", Toast.LENGTH_LONG).show();
}
// 用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE, 1个ACTION_UP触发
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
Log.i("MyGesture", "onFling");
Toast.makeText(MainActivity.this, "onFling", Toast.LENGTH_LONG).show();
return true;
}
};
}
GestureDetector.OnDoubleTapListener
OnDoubleTapListener接口主要用于实现双击手势拦截。
基本方法
private class doubleTapListener implements GestureDetector.OnDoubleTapListener{
public boolean onSingleTapConfirmed(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
public boolean onDoubleTap(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
public boolean onDoubleTapEvent(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
}
这里总共重写了三个方法:
1.onSingleTapConfirmed(MotionEvent e):
单击事件。用来判定该次点击是SingleTap而不是DoubleTap,如果连续点击两次就是DoubleTap手势,如果只点击一次,系统等待一段时间后没有收到第二次点击则判定该次点击为SingleTap而不是DoubleTap,然后触发SingleTapConfirmed事件。
触发顺序是:OnDown->OnsingleTapUp->OnsingleTapConfirmed
关于onSingleTapConfirmed和onSingleTapUp的一点区别:
OnGestureListener有这样的一个方法onSingleTapUp,和onSingleTapConfirmed容易混淆。二者的区别是:onSingleTapUp,只要手抬起就会执行,而对于onSingleTapConfirmed来说,如果双击的话,则onSingleTapConfirmed不会执行。
2.onDoubleTap(MotionEvent e):
双击事件
3.onDoubleTapEvent(MotionEvent e):
双击间隔中发生的动作。指触发onDoubleTap以后,在双击之间发生的其它动作,包含down、up和move事件;
两点总结:
- 在第二下点击时,先触发OnDoubleTap,然后再触发OnDown(第二次点击)
- 其次在触发OnDoubleTap以后,就开始触发onDoubleTapEvent了。
使用形式
开篇我们也提到要想使用OnDoubleTapListener必须要使用OnGestureListener,因为创建GestureDetector实例的三个构造方法的入参根本没有OnDoubleTapListener的形式。以下有两种方式设置双击监听:
1.新建一个类同时派生自OnGestureListener和OnDoubleTapListener
private class gestureListener implements GestureDetector.OnGestureListener,GestureDetector.OnDoubleTapListener{
}
2.使用GestureDetector::setOnDoubleTapListener()函数设置监听
//构建GestureDetector实例
mGestureDetector = new GestureDetector(new gestureListener()); //使用派生自OnGestureListener
private class gestureListener implements GestureDetector.OnGestureListener{
}
//设置双击监听器
mGestureDetector.setOnDoubleTapListener(new doubleTapListener());
private class doubleTapListener implements GestureDetector.OnDoubleTapListener{
}
可以看到,在构造函数中,除了后面要讲的SimpleOnGestureListener 以外的其它两个构造函数都必须是OnGestureListener的实例。所以要想使用OnDoubleTapListener的几个函数,就必须先实现OnGestureListener。
GestureDetector.SimpleOnGestureListener
它与前两个不同的是,这个是一个类,我们可以采用继承的形式,然后重写合适的方法,更加灵活。这个也是我们平时在使用GestureDetector最常用的一种实现形式。另外开篇也讲到SimpleOnGestureListener是OnGestureListener和OnDoubleTapListener所有方法的集合。
例子
下面利用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) {
// TODO Auto-generated method stub
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;
}
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
Log.i("MyGesture", "onScroll:" + (e2.getX() - e1.getX()) + " "
+ distanceX);
Toast.makeText(MainActivity.this, "onScroll", Toast.LENGTH_LONG)
.show();
return true;
}
public void onLongPress(MotionEvent e) {
Log.i("MyGesture", "onLongPress");
Toast.makeText(MainActivity.this, "onLongPress", Toast.LENGTH_LONG)
.show();
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
Log.i("MyGesture", "onFling");
Toast.makeText(MainActivity.this, "onFling", Toast.LENGTH_LONG)
.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;
}
public boolean onDoubleTapEvent(MotionEvent e) {
Log.i("MyGesture", "onDoubleTapEvent");
Toast.makeText(MainActivity.this, "onDoubleTapEvent",
Toast.LENGTH_LONG).show();
return true;
}
}
}
OnFling应用
OnFling是用户执行抛操作之后的回调,MOVE事件之后手松开(UP事件)那一瞬间的x或者y方向速度,如果达到一定数值(源码默认是每秒50px),就是抛操作(也就是快速滑动的时候松手会有这个回调,因此基本上有onFling必然有onScroll)。
下边就演示一个小例子,利用OnFling函数来识别当前用户是在向左滑还是向右滑,功能原理:
当用户向左滑动距离超过100px,且滑动速度超过100 px/s时,即判断为向左滑动;向右同理。
代码如下:
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) {
// TODO Auto-generated method stub
return mGestureDetector.onTouchEvent(event);
}
private class simpleGestureListener extends
GestureDetector.SimpleOnGestureListener {
/*****OnGestureListener的函数*****/
final int FLING_MIN_DISTANCE = 100, FLING_MIN_VELOCITY = 200;
// 触发条件 :
// X轴的坐标位移大于FLING_MIN_DISTANCE,且移动速度大于FLING_MIN_VELOCITY个像素/秒
// 参数解释:
// e1:第1个ACTION_DOWN MotionEvent
// e2:最后一个ACTION_MOVE MotionEvent
// velocityX:X轴上的移动速度,像素/秒
// velocityY:Y轴上的移动速度,像素/秒
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE
&& Math.abs(velocityX) > FLING_MIN_VELOCITY) {
// Fling left
Log.i("MyGesture", "Fling left");
Toast.makeText(MainActivity.this, "Fling Left", Toast.LENGTH_SHORT).show();
} else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE
&& Math.abs(velocityX) > FLING_MIN_VELOCITY) {
// Fling right
Log.i("MyGesture", "Fling right");
Toast.makeText(MainActivity.this, "Fling Right", Toast.LENGTH_SHORT).show();
}
return true;
}
}
}
手势相关事件的触发时机
下边是大神给出的一个手势相关事件的触发时机总结说明,再此记录一下有待以后验证:
从上面的分析可以看出,虽然GestureDetector能识别很多手势,但是也是不能满足所有的需求的,如滑动和长按之后松开没有回调(这个可以重写onTouch()捕捉UP事件实现)、多点触控缩放手势的实现(这个可以用ScaleGestureDetector)等。
参考
- https://blog.csdn.net/harvic880925/article/details/39520901
- https://blog.csdn.net/totond/article/details/77881180#commentBox