Skip to content

Commit 8e66c03

Browse files
committed
FIX : Fixed a problem with the touch handler
1 parent 7da3645 commit 8e66c03

1 file changed

Lines changed: 17 additions & 12 deletions

File tree

library/src/main/java/com/matpag/clickdrawabletextview/CsDrawableViewManager.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import android.util.DisplayMetrics;
1010
import android.view.MotionEvent;
1111
import android.view.View;
12+
import android.view.ViewConfiguration;
1213
import android.view.ViewGroup;
1314
import android.view.inputmethod.InputMethodManager;
1415
import android.widget.TextView;
@@ -47,15 +48,18 @@ final class CsDrawableViewManager implements ClickableDrawable {
4748

4849
/**
4950
* Max allowed duration for a "click", in milliseconds.
51+
*
52+
* I've played a bit with the default android value for recognize a touch
53+
* at {@link ViewConfiguration#getTapTimeout()} but it seemed to me a little to small
54+
* for a normal touch, so i decided to double the amount
5055
*/
51-
private static final int MAX_CLICK_DURATION = 1500;
56+
private static final int MAX_CLICK_DURATION = ViewConfiguration.getTapTimeout() * 2;
5257

5358
/**
5459
* Max allowed distance to move during a "click", in DP.
5560
*/
5661
private static final int MAX_CLICK_DISTANCE = 15;
5762

58-
private long pressStartTime;
5963
private float pressedX;
6064
private float pressedY;
6165
private boolean stayedWithinClickDistance;
@@ -221,9 +225,8 @@ private void setViewOnTouchListener(){
221225
public boolean onTouch(View v, MotionEvent e) {
222226
switch (e.getAction()) {
223227
case MotionEvent.ACTION_DOWN: {
224-
//se ho cliccato uno dei drawable, allora me lo salvo
228+
//if the user clicked on one of the drawables, save some datas about it
225229
if (isOneOfDrawablesTouched(e)) {
226-
pressStartTime = System.currentTimeMillis();
227230
pressedX = e.getX();
228231
pressedY = e.getY();
229232
stayedWithinClickDistance = true;
@@ -234,26 +237,28 @@ public boolean onTouch(View v, MotionEvent e) {
234237
break;
235238
}
236239
case MotionEvent.ACTION_MOVE: {
240+
//if the user moved the finger to much far from the initial tap point,
241+
//we cancel the touch on the drawable
237242
if (stayedWithinClickDistance && distance(pressedX, pressedY, e.getX(),
238243
e.getY()) > MAX_CLICK_DISTANCE) {
239244
stayedWithinClickDistance = false;
240245
}
241246
break;
242247
}
243248
case MotionEvent.ACTION_UP: {
244-
long pressDuration = System.currentTimeMillis() - pressStartTime;
245-
if ((pressDuration < MAX_CLICK_DURATION) && stayedWithinClickDistance
246-
&& mTouchedPosition != null) {
247-
dispatchDrawableClickEvent();
249+
long eventDuration = e.getEventTime() - e.getDownTime();
250+
if (mTouchedPosition != null){
251+
if ((eventDuration < MAX_CLICK_DURATION) && stayedWithinClickDistance) {
252+
dispatchDrawableClickEvent();
253+
//dispatch accessibility events (even if the lint checker is still
254+
//complaining about this
255+
view.performClick();
256+
}
248257
resetTouchedDrawable();
249-
//dispatch accessibility events (even if the lint checker is still
250-
//complaining about this
251-
view.performClick();
252258
return true;
253259
}
254260
}
255261
}
256-
resetTouchedDrawable();
257262
return false;
258263
}
259264
});

0 commit comments

Comments
 (0)