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
//定义类的全局变量
private float downX, downY, upX, upY; //用来判断手势滑动的方向

private int mDirection; //当前的方向
private int mNextDirection; //即将变换的方向
private static final int WEST = 90001;
private static final int EAST = 90002;

@Override
public boolean onTouchEvent(MotionEvent event) { //点击屏幕事件
float X = event.getX(); // 触控的 X 轴位置
float Y = event.getY(); // 触控的 Y 轴位置

switch (event.getAction()) { // 判断触控的动作

case MotionEvent.ACTION_DOWN: // 按下
downX = event.getX();
downY = event.getY();

return true;
case MotionEvent.ACTION_MOVE: // 拖曳

return true;
case MotionEvent.ACTION_UP: // 松开
Log.d("onTouchEvent-ACTION_UP", "UP");
upX = event.getX();
upY = event.getY();
float x = Math.abs(upX - downX);
float y = Math.abs(upY - downY);
double z = Math.sqrt(x * x + y * y);
int jiaodu = Math.round((float) (Math.asin(y / z) / Math.PI * 180));//角度

if (upY < downY && jiaodu > 45) {//上
Log.d("onTouchEvent-ACTION_UP", "角度:" + jiaodu + ", 动作:上");
} else if (upY > downY && jiaodu > 45) {//下
Log.d("onTouchEvent-ACTION_UP", "角度:" + jiaodu + ", 动作:下");
} else if (upX < downX && jiaodu <= 45) {//左
Log.d("onTouchEvent-ACTION_UP", "角度:" + jiaodu + ", 动作:左");
// 原方向不是向右时,方向转右
if (mDirection != EAST) {
mNextDirection = WEST;
}
} else if (upX > downX && jiaodu <= 45) {//右
Log.d("onTouchEvent-ACTION_UP", "角度:" + jiaodu + ", 动作:右");
// 原方向不是向左时,方向转右
if (mDirection != WEST) {
mNextDirection = EAST;
}
}else {
//除了手势,就是点击屏幕

}
return true;
}
return super.onTouchEvent(event);
}