-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathandroid中根据touch事件判断单击及双击
105 lines (97 loc) · 3.99 KB
/
android中根据touch事件判断单击及双击
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
96
97
98
99
100
101
102
103
104
105
private static final int MAX_INTERVAL_FOR_CLICK = 250;
private static final int MAX_DISTANCE_FOR_CLICK = 100;
private static final int MAX_DOUBLE_CLICK_INTERVAL = 500;
int mDownX = 0;
int mDownY = 0;
int mTempX = 0;
int mTempY = 0;
boolean mIsWaitUpEvent = false;
boolean mIsWaitDoubleClick = false;
Runnable mTimerForUpEvent = new Runnable() {
public void run() {
if (mIsWaitUpEvent) {
Log.d(LOG_TAG,
"The mTimerForUpEvent has executed, so set the mIsWaitUpEvent as false");
mIsWaitUpEvent = false;
} else {
Log.d(LOG_TAG,
"The mTimerForUpEvent has executed, mIsWaitUpEvent is false,so do nothing");
}
}
};
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!mIsWaitUpEvent && event.getAction() != MotionEvent.ACTION_DOWN) {
return super.onTouchEvent(event);
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mDownX = (int) event.getX();
mDownY = (int) event.getY();
mIsWaitUpEvent = true;
postDelayed(mTimerForUpEvent, MAX_INTERVAL_FOR_CLICK);
break;
case MotionEvent.ACTION_MOVE:
mTempX = (int) event.getX();
mTempY = (int) event.getY();
if (Math.abs(mTempX - mDownX) > MAX_DISTANCE_FOR_CLICK
|| Math.abs(mTempY - mDownY) > MAX_DISTANCE_FOR_CLICK) {
mIsWaitUpEvent = false;
removeCallbacks(mTimerForUpEvent);
Log.d(LOG_TAG, "The move distance too far:cancel the click");
}
break;
case MotionEvent.ACTION_UP:
mTempX = (int) event.getX();
mTempY = (int) event.getY();
if (Math.abs(mTempX - mDownX) > MAX_DISTANCE_FOR_CLICK
|| Math.abs(mTempY - mDownY) > MAX_DISTANCE_FOR_CLICK) {
mIsWaitUpEvent = false;
removeCallbacks(mTimerForUpEvent);
Log.d(LOG_TAG,
"The touch down and up distance too far:cancel the click");
break;
} else {
mIsWaitUpEvent = false;
removeCallbacks(mTimerForUpEvent);
onSingleClick();
return super.onTouchEvent(event);
}
case MotionEvent.ACTION_CANCEL:
mIsWaitUpEvent = false;
removeCallbacks(mTimerForUpEvent);
Log.d(LOG_TAG, "The touch cancel state:cancel the click");
break;
default:
Log.d(LOG_TAG, "irrelevant MotionEvent state:" + event.getAction());
}
return super.onTouchEvent(event);
}
Runnable mTimerForSecondClick = new Runnable() {
@Override
public void run() {
if (mIsWaitDoubleClick) {
Log.d(LOG_TAG,
"The mTimerForSecondClick has executed,so as a singleClick");
mIsWaitDoubleClick = false;
// at here can do something for singleClick!!
} else {
Log.d(LOG_TAG,
"The mTimerForSecondClick has executed, the doubleclick has executed ,so do thing");
}
}
};
public void onSingleClick() {
if (mIsWaitDoubleClick) {
onDoubleClick();
mIsWaitDoubleClick = false;
removeCallbacks(mTimerForSecondClick);
} else {
mIsWaitDoubleClick = true;
postDelayed(mTimerForSecondClick, MAX_DOUBLE_CLICK_INTERVAL);
}
}
public void onDoubleClick() {
Log.d(LOG_TAG,"we can do sth for double click here");
}
come from http://blog.csdn.net/mydreamongo/article/details/38405659