对于触摸屏,其原生的消息无非按下、抬起、移动这几种,我们只需要简单重载onTouch或者设置触摸侦听器setOnTouchListener即可进行处理。不过,为了提高我们的APP的用户体验,有时候我们需要识别用户的手势,Android给我们提供的手势识别工具GestureDetector就可以帮上大忙了。
- 基础
GestureDetector的工作原理是,当我们接收到用户触摸消息时,将这个消息交给GestureDetector去加工,我们通过设置侦听器获得GestureDetector处理后的手势。
GestureDetector提供了两个侦听器接口,OnGestureListener处理单击类消息,OnDoubleTapListener处理双击类消息。
OnGestureListener的接口有这几个:
- // 单击,触摸屏按下时立刻触发
- abstract boolean onDown(MotionEvent e);
- // 抬起,手指离开触摸屏时触发(长按、滚动、滑动时,不会触发这个手势)
- abstract boolean onSingleTapUp(MotionEvent e);
- // 短按,触摸屏按下后片刻后抬起,会触发这个手势,如果迅速抬起则不会
- abstract void onShowPress(MotionEvent e);
- // 长按,触摸屏按下后既不抬起也不移动,过一段时间后触发
- abstract void onLongPress(MotionEvent e);
- // 滚动,触摸屏按下后移动
- abstract boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY);
- // 滑动,触摸屏按下后快速移动并抬起,会先触发滚动手势,跟着触发一个滑动手势
- abstract boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY);
OnDoubleTapListener的接口有这几个:
- // 双击,手指在触摸屏上迅速点击第二下时触发
- abstract boolean onDoubleTap(MotionEvent e);
- // 双击的按下跟抬起各触发一次
- abstract boolean onDoubleTapEvent(MotionEvent e);
- // 单击确认,即很快的按下并抬起,但并不连续点击第二下
- abstract boolean onSingleTapConfirmed(MotionEvent e);
有时候我们并不需要处理上面所有手势,方便起见,Android提供了另外一个类SimpleOnGestureListener实现了如上接口,我们只需要继承SimpleOnGestureListener然后重载感兴趣的手势即可。
- 应用
STEP 1: 创建手势侦听对象
- package noodies.blog.csdn.net;
- import android.content.Context;
- import android.view.MotionEvent;
- import android.view.GestureDetector.SimpleOnGestureListener;
- import android.widget.Toast;
- public class MyGestureListener extends SimpleOnGestureListener {
- private Context mContext;
- MyGestureListener(Context context) {
- mContext = context;
- }
- @Override
- public boolean onDown(MotionEvent e) {
- Toast.makeText(mContext, "DOWN " + e.getAction(), Toast.LENGTH_SHORT).show();
- return false;
- }
- @Override
- public void onShowPress(MotionEvent e) {
- Toast.makeText(mContext, "SHOW " + e.getAction(), Toast.LENGTH_SHORT).show();
- }
- @Override
- public boolean onSingleTapUp(MotionEvent e) {
- Toast.makeText(mContext, "SINGLE UP " + e.getAction(), Toast.LENGTH_SHORT).show();
- return false;
- }
- @Override
- public boolean onScroll(MotionEvent e1, MotionEvent e2,
- float distanceX, float distanceY) {
- Toast.makeText(mContext, "SCROLL " + e2.getAction(), Toast.LENGTH_SHORT).show();
- return false;
- }
- @Override
- public void onLongPress(MotionEvent e) {
- Toast.makeText(mContext, "LONG " + e.getAction(), Toast.LENGTH_SHORT).show();
- }
- @Override
- public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
- float velocityY) {
- Toast.makeText(mContext, "FLING " + e2.getAction(), Toast.LENGTH_SHORT).show();
- return false;
- }
- @Override
- public boolean onDoubleTap(MotionEvent e) {
- Toast.makeText(mContext, "DOUBLE " + e.getAction(), Toast.LENGTH_SHORT).show();
- return false;
- }
- @Override
- public boolean onDoubleTapEvent(MotionEvent e) {
- Toast.makeText(mContext, "DOUBLE EVENT " + e.getAction(), Toast.LENGTH_SHORT).show();
- return false;
- }
- @Override
- public boolean onSingleTapConfirmed(MotionEvent e) {
- Toast.makeText(mContext, "SINGLE CONF " + e.getAction(), Toast.LENGTH_SHORT).show();
- return false;
- }
- }
STEP 2: 设置手势识别
我们可以在Activity里设置手势识别:
- package noodies.blog.csdn.net;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.GestureDetector;
- import android.view.MotionEvent;
- public class GestureTestActivity extends Activity {
- private GestureDetector mGestureDetector;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mGestureDetector = new GestureDetector(this, new MyGestureListener(this));
- }
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- return mGestureDetector.onTouchEvent(event);
- }
- }
也可以在自定义的View里面设置手势识别:
- package noodies.blog.csdn.net;
- import android.content.Context;
- import android.util.AttributeSet;
- import android.view.GestureDetector;
- import android.view.MotionEvent;
- import android.view.View;
- public class MyView extends View {
- private GestureDetector mGestureDetector;
- public MyView(Context context, AttributeSet attrs) {
- super(context, attrs);
- mGestureDetector = new GestureDetector(context, new MyGestureListener(context));
- setLongClickable(true);
- this.setOnTouchListener(new OnTouchListener() {
- public boolean onTouch(View v, MotionEvent event) {
- return mGestureDetector.onTouchEvent(event);
- }
- });
- }
- }
- 陷阱
对于自定义View,使用手势识别有两处陷阱可能会浪费你的不少时间。
1:View必须设置longClickable为true,否则手势识别无法正确工作,只会返回Down, Show, Long三种手势
2:必须在View的onTouchListener中调用手势识别,而不能像Activity一样重载onTouchEvent,否则同样手势识别无法正确工作
- 测试结果
下面是各种操作返回的手势序列,数值0表示触摸屏按下,1表示抬起
- 单击:down 0, single up 1, single conf 0
- 短按:down 0, show 0, single up 1
- 长按:down 0, show 0, long 0
- 双击:down 0, single up 1, double 0, double event 0, down 0, double event 1
- 滚动:down 0, (show 0), scrool 2...
- 滑动:down 0, (show 0), scrool 2..., fling 1