Android手势识别之GestureDetector
最后更新于:2022-04-01 14:27:34
GestureDetector(手势识别器)
### (1)手势交互过程原理:
A.触屏一刹那,触发 MotionEvent事件;
B.上述事件被 OnTouchListenter 监听,在 nTouch() 中获得 MotionEvent对象;
C.GestureDetector 转发MotionEvent对象至 OnGestureListenter;
D.OnGestureListenter 获得该对象,根据该对象封装的信息作出合适的反馈;
### (2)MotionEvent:
1)、用于封装手势等动作事件;
2)、内部封装用于记录横轴和纵轴坐标的属性X和Y;
GestureDetector:
1)、识别各种手势;
OnGestureListenter:
1)、手势交互的监听接口,其提供多个抽象方法;
2)、根据GestureDetector的手势识别结果调用相对应的方法;
### (3)GestureDetector工作原理:
1)、GestureDetector.OnGestureListener接口
onDown(MotionEvent e):——单击
Notified when a tap occurs with the down MotionEvent that triggered it.
onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY):——滑动
Notified of a fling event when it occurs with the initial on down MotionEvent and the matching up MotionEvent.
onLongPress(MotionEvent e):——长按
Notified when a long press occurs with the initial on down MotionEvent that trigged it.
onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY):——滚动
Notified when a scroll occurs with the initial on down MotionEvent and the current move MotionEvent.
onShowPress(MotionEvent e):——短按
The user has performed a down MotionEvent and not performed a move or up yet.
onSingleTapUp(MotionEvent e):——抬起
Notified when a tap occurs with the up MotionEvent that triggered it.
2)、GestureDetector.OnDoubleTapListener接口
onDoubleTap(MotionEvent e):——双击**
Notified when a double-tap occurs.
onDoubleTapEvent(MotionEvent e):——双击按下和抬起各触发一次
Notified when an event within a double-tap gesture occurs, including the down, move, and up events.
onSingleTapConfirmed(MotionEvent e):——单击确认(即很快的按下并抬起,但并不连续点击第二下)
Notified when a single-tap occurs.
3)、GestureDetector.SimpleOnGestureListener类
其实我们并不需要处理上面所有手势。问了方便起见,Android提供了另外一个类SimpleOnGestureListener。它实现了如上接口,我们只需要继承SimpleOnGestureListener类,然后重载感兴趣的手势。 implements [GestureDetector.OnGestureListener](http://developer.android.com/reference/android/view/GestureDetector.OnGestureListener.html) [GestureDetector. OnDoubleTapListener](http://developer.android.com/reference/android/view/GestureDetector.OnDoubleTapListener.html)[GestureDetector. OnContextClickListener](http://developer.android.com/reference/android/view/GestureDetector.OnContextClickListener.html)。A convenience class to extend when you only want to listen for a subset of all the gestures. This implements all methods in the `[GestureDetector.OnGestureListener](http://developer.android.com/reference/android/view/GestureDetector.OnGestureListener.html)`, `[GestureDetector.OnDoubleTapListener](http://developer.android.com/reference/android/view/GestureDetector.OnDoubleTapListener.html)`, and`[GestureDetector.OnContextClickListener](http://developer.android.com/reference/android/view/GestureDetector.OnContextClickListener.html)` but does nothing and return`false` for all applicable methods.
~~~
public class MainActivity extends Activity {
private ImageView imageView;
private GestureDetector mygestureDetector;
// 继承SimpleOnGestureListener类,然后重载感兴趣的手势。
class MyGestureListenter extends SimpleOnGestureListener {
@Override
// 重载滑动手势
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if (e1.getX() - e2.getX() > 50) {
Toast.makeText(MainActivity.this, "从右往左滑动", Toast.LENGTH_SHORT)
.show();
} else if (e2.getX() - e1.getX() > 50) {
Toast.makeText(MainActivity.this, "从左往右滑动", Toast.LENGTH_SHORT)
.show();
}
return super.onFling(e1, e2, velocityX, velocityY);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
* GestureDetector工作原理
* 1、当接收到用户触摸消息时,将消息交给GestureDetector加工;
* 2、通过设置监听器获得GestureDetector处理后的手势;
* 3、GestureDetector提供两个监听器:
* A.OnGestureListenter:处理单击类消息;
* B.OnDoubleTapListenter:处理双击类消息;
*/
imageView = (ImageView) findViewById(R.id.img);
mygestureDetector = new GestureDetector(new MyGestureListenter());
// MotionEvent——》setOnTouchListener捕获
// ——》onTouch中GestureDetector对象将监听事件转发给MyGestureListenter(extends SimpleOnGestureListener)
// ——》MyGestureListenter类中实现了要重载的手势
imageView.setOnTouchListener(new OnTouchListener() {
@Override
// 可以捕获触摸屏幕发生的Event
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
mygestureDetector.onTouchEvent(event); // 转发event事件,转发给MyGestureListenter(extends SimpleOnGestureListener)
return false;
}
});
}
//消除冲突
/*@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
if(mygestureDetector!=null){
if(mygestureDetector.onTouchEvent(ev)){
return true;
}
}
return super.dispatchTouchEvent(ev);
}*/
//必须重写onTouchEvent方法,onFling才生效
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return mygestureDetector.onTouchEvent(event);
}
}
~~~
为了让onFling才生效,必须重写onTouchEvent方法!