-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathClickUtils.java
More file actions
311 lines (266 loc) · 10.1 KB
/
ClickUtils.java
File metadata and controls
311 lines (266 loc) · 10.1 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package com.xzy.utils.click;
import android.view.MotionEvent;
import android.view.View;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
/**
* 点击相关工具类。
* 参考 https://github.com/Blankj/AndroidUtilCode/blob/master/lib/utilcode/src/main/
* java/com/blankj/utilcode/util/ClickUtils.java
*
* @author xzy
*/
@SuppressWarnings("unused")
public class ClickUtils {
private static final int SCALE = -1;
private static final float SCALE_DEFAULT_FACTOR = -0.06f;
private static final long DEBOUNCING_DEFAULT_DURATION = 200;
/**
* Apply scale animation for the views' click.
*
* @param views The views.
*/
public static void applyScale(final View... views) {
applyScale(views, null);
}
/**
* Apply scale animation for the views' click.
*
* @param views The views.
* @param scaleFactors The factors of scale for the views.
*/
public static void applyScale(final View[] views, final float[] scaleFactors) {
if (views == null || views.length == 0) return;
for (int i = 0; i < views.length; i++) {
if (views[i] == null) continue;
if (scaleFactors == null || i >= scaleFactors.length) {
views[i].setTag(SCALE, SCALE_DEFAULT_FACTOR);
} else {
views[i].setTag(SCALE, scaleFactors[i]);
}
views[i].setClickable(true);
views[i].setOnTouchListener(OnUtilsTouchListener.getInstance());
}
}
/**
* Apply single debouncing for the view's click.
*
* @param view The view.
* @param listener The listener.
*/
public static void applySingleDebouncing(final View view, final View.OnClickListener listener) {
applySingleDebouncing(new View[]{view}, listener);
}
/**
* Apply single debouncing for the view's click.
*
* @param view The view.
* @param duration The duration of debouncing.
* @param listener The listener.
*/
public static void applySingleDebouncing(final View view, @IntRange(from = 0) long duration,
final View.OnClickListener listener) {
applySingleDebouncing(new View[]{view}, duration, listener);
}
/**
* Apply single debouncing for the views' click.
*
* @param views The views.
* @param listener The listener.
*/
public static void applySingleDebouncing(final View[] views,
final View.OnClickListener listener) {
applySingleDebouncing(views, DEBOUNCING_DEFAULT_DURATION, listener);
}
/**
* Apply single debouncing for the views' click.
*
* @param views The views.
* @param duration The duration of debouncing.
* @param listener The listener.
*/
public static void applySingleDebouncing(final View[] views,
@IntRange(from = 0) long duration,
final View.OnClickListener listener) {
applyDebouncing(views, false, duration, listener);
}
/**
* Apply global debouncing for the view's click.
*
* @param view The view.
* @param listener The listener.
*/
public static void applyGlobalDebouncing(final View view,
final View.OnClickListener listener) {
applyGlobalDebouncing(new View[]{view}, listener);
}
/**
* Apply global debouncing for the view's click.
*
* @param view The view.
* @param duration The duration of debouncing.
* @param listener The listener.
*/
public static void applyGlobalDebouncing(final View view, @IntRange(from = 0) long duration,
final View.OnClickListener listener) {
applyGlobalDebouncing(new View[]{view}, duration, listener);
}
/**
* Apply global debouncing for the views' click.
*
* @param views The views.
* @param listener The listener.
*/
public static void applyGlobalDebouncing(final View[] views,
final View.OnClickListener listener) {
applyGlobalDebouncing(views, DEBOUNCING_DEFAULT_DURATION, listener);
}
/**
* Apply global debouncing for the views' click.
*
* @param views The views.
* @param duration The duration of debouncing.
* @param listener The listener.
*/
public static void applyGlobalDebouncing(final View[] views,
@IntRange(from = 0) long duration,
final View.OnClickListener listener) {
applyDebouncing(views, true, duration, listener);
}
private static void applyDebouncing(final View[] views,
final boolean isGlobal,
@IntRange(from = 0) long duration,
final View.OnClickListener listener) {
if (views == null || views.length == 0 || listener == null) return;
for (View view : views) {
if (view == null) continue;
view.setOnClickListener(new OnDebouncingClickListener(isGlobal, duration) {
@Override
public void onDebouncingClick(View v) {
listener.onClick(v);
}
});
}
}
public static abstract class OnDebouncingClickListener implements View.OnClickListener {
private static final int TAG_KEY = 0x7EFFFFFF;
private static boolean mEnabled = true;
private static final Runnable ENABLE_AGAIN = new Runnable() {
@Override
public void run() {
mEnabled = true;
}
};
private static boolean isValid(@NonNull final View view, final long duration) {
long curTime = System.currentTimeMillis();
Object tag = view.getTag(TAG_KEY);
if (!(tag instanceof Long)) {
view.setTag(TAG_KEY, curTime);
return true;
}
long preTime = (Long) tag;
if (curTime - preTime <= duration) return false;
view.setTag(TAG_KEY, curTime);
return true;
}
private long mDuration;
private boolean mIsGlobal;
public OnDebouncingClickListener() {
this(true, DEBOUNCING_DEFAULT_DURATION);
}
public OnDebouncingClickListener(final boolean isGlobal) {
this(isGlobal, DEBOUNCING_DEFAULT_DURATION);
}
public OnDebouncingClickListener(final long duration) {
this(true, duration);
}
public OnDebouncingClickListener(final boolean isGlobal, final long duration) {
mIsGlobal = isGlobal;
mDuration = duration;
}
public abstract void onDebouncingClick(View v);
@Override
public final void onClick(View v) {
if (mIsGlobal) {
if (mEnabled) {
mEnabled = false;
v.postDelayed(ENABLE_AGAIN, mDuration);
onDebouncingClick(v);
}
} else {
if (isValid(v, mDuration)) {
onDebouncingClick(v);
}
}
}
}
public static abstract class OnMultiClickListener implements View.OnClickListener {
private static final long DEFAULT_INTERVAL = 666;
private final int mTriggerClickCount;
private final long mClickInterval;
private long mLastClickTime;
private int mClickCount;
public OnMultiClickListener(int triggerClickCount) {
this(triggerClickCount, DEFAULT_INTERVAL);
}
public OnMultiClickListener(int triggerClickCount, long clickInterval) {
this.mTriggerClickCount = triggerClickCount;
this.mClickInterval = clickInterval;
}
public abstract void onTriggerClick(View v);
public abstract void onBeforeTriggerClick(View v, int count);
@Override
public void onClick(View v) {
if (mTriggerClickCount <= 1) {
onTriggerClick(v);
return;
}
long curTime = System.currentTimeMillis();
if (curTime - mLastClickTime < mClickInterval) {
mClickCount++;
if (mClickCount == mTriggerClickCount) {
onTriggerClick(v);
} else if (mClickCount < mTriggerClickCount) {
onBeforeTriggerClick(v, mClickCount);
} else {
mClickCount = 1;
onBeforeTriggerClick(v, mClickCount);
}
} else {
mClickCount = 1;
onBeforeTriggerClick(v, mClickCount);
}
mLastClickTime = curTime;
}
}
private static class OnUtilsTouchListener implements View.OnTouchListener {
public static OnUtilsTouchListener getInstance() {
return LazyHolder.INSTANCE;
}
private OnUtilsTouchListener() {/**/}
@Override
public boolean onTouch(final View v, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
processScale(v, true);
} else if (action == MotionEvent.ACTION_UP
|| action == MotionEvent.ACTION_CANCEL) {
processScale(v, false);
}
return false;
}
private void processScale(final View view, boolean isDown) {
Object tag = view.getTag(SCALE);
if (!(tag instanceof Float)) return;
float value = isDown ? 1 + (Float) tag : 1;
view.animate()
.scaleX(value)
.scaleY(value)
.setDuration(100)
.start();
}
}
private static class LazyHolder {
private static final OnUtilsTouchListener INSTANCE = new OnUtilsTouchListener();
}
}