一. 前言

1. 介绍

Android手势识别类:GestureDetector类是 Android 提供的一个手势识别工具类。

作用:识别用户输入的手势,从而做出对应的响应。

2. 使用

主要分为两个大部分来使用:

① 接口使用

OnGestureListener 和 OnDoubleTapListener

② 类使用

SimpleOnGestureListener

3. 本文源码

GestureDetector

二. OnGestureListener

1. 监听的手势类型

按下的瞬间,按压,长按,轻击,快速滑屏,快速拖动

2. 使用步骤

① 创建手势检测器实例

常用的手势检测器构造方法是第二个

1
2
3
4
5
6
//构造函数1
GestureDetector gestureDetector = new GestureDetector(GestureDetector.OnGestureListener listener);
//构造函数2
GestureDetector gestureDetector = new GestureDetector(Context context,GestureDetector.OnGestureListener listener);
//构造函数3
GestureDetector gestureDetector = new GestureDetector(Context context,GestureDetector.SimpleOnGestureListener listener);

② 让某个View或者Activity检测手势

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//1.让某个View检测手势
//重写View的onTouch函数,将View的触屏事件交给GestureDetector处理,从而对用户手势作出响应
View.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mGestureDetector.onTouchEvent(event);

//注:返回true才能完整接收触摸事件
return true;
}
});

//2.让某个Activity检测手势
//重写Activity的dispatchTouchEvent函数,将触屏事件交给GestureDetector处理,从而对用户手势作出响应
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
//让GestureDetector响应触碰事件
mGestureDetector.onTouchEvent(event);

//让Activity响应触碰事件
super.dispatchTouchEvent(event);

return false;
}

3. 实际例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
public class MainActivity extends AppCompatActivity {

public static final String TAG = MainActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//1.创建手势检测器实例
final GestureDetector gestureDetector = new GestureDetector(this, new GestureDetector.OnGestureListener() {

//用户轻触触摸屏
@Override
public boolean onDown(MotionEvent e) {
Log.d(TAG,"down");
return false;
}

//用户轻触触摸屏,尚未松开或拖动
@Override
public void onShowPress(MotionEvent e) {
Log.d(TAG,"show_press");
}

//用户轻击屏幕后抬起
@Override
public boolean onSingleTapUp(MotionEvent e) {
Log.d(TAG,"single_tap_up");
return true;
}

//用户按下触摸屏或者拖动
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Log.d(TAG,"scroll");
return true;
}

//用户长按触摸屏
@Override
public void onLongPress(MotionEvent e) {
Log.d(TAG,"long_press");
}

//用户按下触摸屏,快速移动后松开
//e1:第一个ACTION_DOWN MotionEvent
//e2:最后一个ACTION_MOVE MotionEvent
//velocityX:X轴上的移动速度,像素/秒
//velocityY:Y轴上的移动速度,像素/秒
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.d(TAG,"fling");
return true;
}
});

//2.让某个View检测手势
findViewById(R.id.view).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//将View的触摸事件交给GestureDetector处理
gestureDetector.onTouchEvent(event);

return true;
}
});
}
}

① 轻触立即拿开

1
2
2020-05-17 15:21:50.769 21623-21623/? D/MainActivity: down
2020-05-17 15:21:50.838 21623-21623/? D/MainActivity: single_tap_up

② 长按后拿开

1
2
3
2020-05-17 15:22:55.952 21623-21623/? D/MainActivity: down
2020-05-17 15:22:56.051 21623-21623/? D/MainActivity: show_press
2020-05-17 15:22:56.351 21623-21623/? D/MainActivity: long_press

③ 按住左右滑动后拿开

1
2
3
4
2020-05-17 15:23:42.591 21623-21623/? D/MainActivity: down
2020-05-17 15:23:42.669 21623-21623/? D/MainActivity: scroll
2020-05-17 15:23:43.081 21623-21623/? D/MainActivity: scroll
2020-05-17 15:23:43.082 21623-21623/? D/MainActivity: fling

三. OnDoubleTapListener

1. 监听的手势类型

单机,双击

2. 使用步骤

使用OnDoubleTapListener接口时,需要使用GestureDetector,而GestureDetector的创建则必须传入OnGestureListener接口,所以在使用OnDoubleTapListener接口时,也必须实现OnGestureListener接口。

① 创建手势检测器实例

常用的手势检测器构造方法是第二个

1
2
3
4
5
6
//构造函数1
GestureDetector gestureDetector = new GestureDetector(GestureDetector.OnGestureListener listener);
//构造函数2
GestureDetector gestureDetector = new GestureDetector(Context context,GestureDetector.OnGestureListener listener);
//构造函数3
GestureDetector gestureDetector = new GestureDetector(Context context,GestureDetector.SimpleOnGestureListener listener);

② 设置OnDoubleTapListener接口实现类

1
public void setOnDoubleTapListener(OnDoubleTapListener onDoubleTapListener);

③ 让某个View或者Activity检测手势

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//1.让某个View检测手势
//重写View的onTouch函数,将View的触屏事件交给GestureDetector处理,从而对用户手势作出响应
View.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mGestureDetector.onTouchEvent(event);

//注:返回true才能完整接收触摸事件
return true;
}
});

//2.让某个Activity检测手势
//重写Activity的dispatchTouchEvent函数,将触屏事件交给GestureDetector处理,从而对用户手势作出响应
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
//让GestureDetector响应触碰事件
mGestureDetector.onTouchEvent(event);

//让Activity响应触碰事件
super.dispatchTouchEvent(event);

return false;
}

3. 实际例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
public class MainActivity extends AppCompatActivity {

public static final String TAG = MainActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//1.创建手势检测器实例
final GestureDetector gestureDetector = new GestureDetector(this, new GestureDetector.OnGestureListener() {

//用户轻触触摸屏
@Override
public boolean onDown(MotionEvent e) {
Log.d(TAG,"down");
return false;
}

//用户轻触触摸屏,尚未松开或拖动
@Override
public void onShowPress(MotionEvent e) {
Log.d(TAG,"show_press");
}

//用户轻击屏幕后抬起
@Override
public boolean onSingleTapUp(MotionEvent e) {
Log.d(TAG,"single_tap_up");
return true;
}

//用户按下触摸屏或者拖动
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Log.d(TAG,"scroll");
return true;
}

//用户长按触摸屏
@Override
public void onLongPress(MotionEvent e) {
Log.d(TAG,"long_press");
}

//用户按下触摸屏,快速移动后松开
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.d(TAG,"fling");
return true;
}
});

//2. 设置OnDoubleTapListener接口实现类
gestureDetector.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener() {

//1.单机事件
//onSingleTapUp:手抬起就会执行
//onSingleTapConfirmed:双击不会执行
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
Log.d(TAG,"single_tap_confirmed");

return true;
}

//双击事件
@Override
public boolean onDoubleTap(MotionEvent e) {
Log.d(TAG,"double_tap");

return true;
}

//双击间隔发生的动作
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
Log.d(TAG,"double_tap_event");

return false;
}
});

//2.让某个View检测手势
findViewById(R.id.view).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//将View的触摸事件交给GestureDetector处理
gestureDetector.onTouchEvent(event);

return true;
}
});
}
}

① 单击

1
2
3
2020-05-17 15:55:03.708 10178-10178/swu.xl.ondoubletaplistener D/MainActivity: down
2020-05-17 15:55:03.801 10178-10178/swu.xl.ondoubletaplistener D/MainActivity: single_tap_up
2020-05-17 15:55:04.008 10178-10178/swu.xl.ondoubletaplistener D/MainActivity: single_tap_confirmed

② 双击

1
2
3
4
5
6
7
2020-05-17 15:55:25.868 10178-10178/swu.xl.ondoubletaplistener D/MainActivity: down
2020-05-17 15:55:25.938 10178-10178/swu.xl.ondoubletaplistener D/MainActivity: single_tap_up
2020-05-17 15:55:26.137 10178-10178/swu.xl.ondoubletaplistener D/MainActivity: double_tap
2020-05-17 15:55:26.137 10178-10178/swu.xl.ondoubletaplistener D/MainActivity: double_tap_event
2020-05-17 15:55:26.137 10178-10178/swu.xl.ondoubletaplistener D/MainActivity: down
2020-05-17 15:55:26.183 10178-10178/swu.xl.ondoubletaplistener D/MainActivity: double_tap_event

四. SimpleOnGestureListener

1. 监听的手势类型

集成了两个接口的功能

2. 区别

① OnGestureListener和OnDoubleTapListener接口里的函数都是强制必须重写的。

② SimpleOnGestureListener类的函数则可根据需要选择性复写,因为SimpleOnGestureListener类本身已经实现了这两个接口的所有函数,只是里面全是空的而已。

3. 使用步骤

① 创建手势检测器实例

这里使用第三个构造方法。

1
2
3
4
5
6
//构造函数1
GestureDetector gestureDetector = new GestureDetector(GestureDetector.OnGestureListener listener);
//构造函数2
GestureDetector gestureDetector = new GestureDetector(Context context,GestureDetector.OnGestureListener listener);
//构造函数3
GestureDetector gestureDetector = new GestureDetector(Context context,GestureDetector.SimpleOnGestureListener listener);

② 让某个View或者Activity检测手势

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//1.让某个View检测手势
//重写View的onTouch函数,将View的触屏事件交给GestureDetector处理,从而对用户手势作出响应
View.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mGestureDetector.onTouchEvent(event);

//注:返回true才能完整接收触摸事件
return true;
}
});

//2.让某个Activity检测手势
//重写Activity的dispatchTouchEvent函数,将触屏事件交给GestureDetector处理,从而对用户手势作出响应
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
//让GestureDetector响应触碰事件
mGestureDetector.onTouchEvent(event);

//让Activity响应触碰事件
super.dispatchTouchEvent(event);

return false;
}

4. 实际例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
public class MainActivity extends AppCompatActivity {

public static final String TAG = MainActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//1.创建手势检测器实例
final GestureDetector gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener(){

public boolean onDown(MotionEvent e) {
Log.d(TAG,"down");
return false;
}

public void onShowPress(MotionEvent e) {
Log.d(TAG,"show_press");
}

public void onLongPress(MotionEvent e) {
Log.d(TAG,"long_press");
}

public boolean onSingleTapUp(MotionEvent e) {
Log.d(TAG,"single_tap_up");
return true;
}

public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
Log.d(TAG,"scroll");
return true;
}

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
Log.d(TAG,"fling");
return true;
}

public boolean onSingleTapConfirmed(MotionEvent e) {
Log.d(TAG,"single_tap_confirmed");
return false;
}

public boolean onDoubleTap(MotionEvent e) {
Log.d(TAG,"double_tap");
return false;
}

public boolean onDoubleTapEvent(MotionEvent e) {
Log.d(TAG,"double_tap_event");
return false;
}
});

//2.让某个View检测手势
findViewById(R.id.view).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//将View的触摸事件交给GestureDetector处理
gestureDetector.onTouchEvent(event);

return true;
}
});
}
}

① 单击

1
2
3
2020-05-17 15:55:03.708 10178-10178/swu.xl.ondoubletaplistener D/MainActivity: down
2020-05-17 15:55:03.801 10178-10178/swu.xl.ondoubletaplistener D/MainActivity: single_tap_up
2020-05-17 15:55:04.008 10178-10178/swu.xl.ondoubletaplistener D/MainActivity: single_tap_confirmed

② 双击

1
2
3
4
5
6
7
2020-05-17 15:55:25.868 10178-10178/swu.xl.ondoubletaplistener D/MainActivity: down
2020-05-17 15:55:25.938 10178-10178/swu.xl.ondoubletaplistener D/MainActivity: single_tap_up
2020-05-17 15:55:26.137 10178-10178/swu.xl.ondoubletaplistener D/MainActivity: double_tap
2020-05-17 15:55:26.137 10178-10178/swu.xl.ondoubletaplistener D/MainActivity: double_tap_event
2020-05-17 15:55:26.137 10178-10178/swu.xl.ondoubletaplistener D/MainActivity: down
2020-05-17 15:55:26.183 10178-10178/swu.xl.ondoubletaplistener D/MainActivity: double_tap_event

参考文章

Android GestureDetector:那些你一定要学会的手势识别应用(含实例讲解)