Error In Implementing Android Double Tap
I implemented OnDoubleTapListener in my Activity class and override three methods as follow. @Override public boolean onDoubleTap(MotionEvent e) { // TODO Auto-generat
Solution 1:
Try out this:
publicclassMyViewextendsView {
GestureDetector gestureDetector;
publicMyView(Context context, AttributeSet attrs) {
super(context, attrs);
// creating new gesture detector
gestureDetector = newGestureDetector(context, newGestureListener());
}
// skipping measure calculation and drawing// delegate the event to the gesture detector@OverridepublicbooleanonTouchEvent(MotionEvent e) {
return gestureDetector.onTouchEvent(e);
}
privateclassGestureListenerextendsGestureDetector.SimpleOnGestureListener {
@OverridepublicbooleanonDown(MotionEvent e) {
returntrue;
}
// event when double tap occurs@OverridepublicbooleanonDoubleTap(MotionEvent e) {
floatx= e.getX();
floaty= e.getY();
Log.d("Double Tap", "Tapped at: (" + x + "," + y + ")");
returntrue;
}
}
}
Solution 2:
GestureDetector will consume the touch events you already have on children for example, if you do not want that you can use the following code instead, I do not use ACTION_UP
so I do not need to consume the event (only the second tap will be consumed)
publicclassDoubleTapDetector {
publicinterfaceOnDoubleTapListener {
booleanonDoubleTap(MotionEvent e);
booleanonDoubleTapEvent(MotionEvent e);
}
privateint mDoubleTapSlopSquare;
privatestaticfinalintDOUBLE_TAP_TIMEOUT= ViewConfiguration.getDoubleTapTimeout();
privatestaticfinalintDOUBLE_TAP_MIN_TIME=40;
privatestaticfinalintDOUBLE_TAP_SLOP=100;
private OnDoubleTapListener mDoubleTapListener;
private MotionEvent mCurrentDownEvent;
publicDoubleTapDetector(Context context, OnDoubleTapListener listener) {
mDoubleTapListener = listener;
init(context);
}
privatevoidinit(Context context) {
if (mDoubleTapListener == null) {
thrownewNullPointerException("OnDoubleTapListener must not be null");
}
int doubleTapSlop;
if (context == null) {
doubleTapSlop = DOUBLE_TAP_SLOP;
} else {
finalViewConfigurationconfiguration= ViewConfiguration.get(context);
doubleTapSlop = configuration.getScaledDoubleTapSlop();
}
mDoubleTapSlopSquare = doubleTapSlop * doubleTapSlop;
}
publicbooleanonTouchEvent(MotionEvent ev) {
finalintaction= ev.getAction();
booleanhandled=false;
switch (action) {
case MotionEvent.ACTION_DOWN:
if ((mCurrentDownEvent != null) &&
isConsideredDoubleTap(mCurrentDownEvent, ev)) {
// This is a second tap// Give a callback with the first tap of the double-tap
handled |= mDoubleTapListener.onDoubleTap(mCurrentDownEvent);
// Give a callback with down event of the double-tap
handled |= mDoubleTapListener.onDoubleTapEvent(ev);
} else {
// This is a first tap
}
if (mCurrentDownEvent != null) {
mCurrentDownEvent.recycle();
}
mCurrentDownEvent = MotionEvent.obtain(ev);
break;
}
return handled;
}
privatebooleanisConsideredDoubleTap(MotionEvent firstDown, MotionEvent secondDown) {
finallongdeltaTime= secondDown.getEventTime() - firstDown.getEventTime();
if (deltaTime > DOUBLE_TAP_TIMEOUT || deltaTime < DOUBLE_TAP_MIN_TIME) {
returnfalse;
}
intdeltaX= (int) firstDown.getX() - (int) secondDown.getX();
intdeltaY= (int) firstDown.getY() - (int) secondDown.getY();
return (deltaX * deltaX + deltaY * deltaY < mDoubleTapSlopSquare);
}
}
Usage:
DoubleTapDetector doubleTapDetector = newDoubleTapDetector(conversationWindow, newDoubleTapDetector.OnDoubleTapListener() {
@OverridepublicbooleanonDoubleTap(MotionEvent e) {
// Double tap detected.returnfalse;
}
@OverridepublicbooleanonDoubleTapEvent(MotionEvent e) {
returnfalse;
}
});
someView.setOnTouchListener(newView.OnTouchListener() {
@OverridepublicbooleanonTouch(View view, MotionEvent e) {
return doubleTapDetector.onTouchEvent(e);
}
});
Post a Comment for "Error In Implementing Android Double Tap"