im trying to detect when a user make a left or right swing in the activity.
Currently I’m using a OnGestureListener and the method onFling() and my problem is that I don’t achieve a nice way of detecting it.
I’d like a behaviour similar to the others applications and the “native” Android swing detection but with the code (appended below) I have a lot of wrong swing detections. If someone have already solved that I’d like a tip =)
Thats the code I’m using
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {
//Get Position
float ev1X = e1.getX();
float ev2X = e2.getX();
//Get distance of X (e1) to X (e2)
final float xdistance = Math.abs(ev1X - ev2X);
//Get velocity of cursor
final float xvelocity = Math.abs(velocityX);
if( (xvelocity > SWIPE_MIN_VELOCITY) && (xdistance > SWIPE_MIN_DISTANCE) )
{
if(ev1X > ev2X)//Switch Left
{
if (Manager.debug) Log.d(Manager.appname,"SWING_LEFT_EVENT");
moveToRight();
}
else//Switch Right
{
if (Manager.debug) Log.d(Manager.appname,"SWING_RIGHT_EVENT");
moveToLeft();
}
}
return false;
}
I am not sure how far this is useful, but this is how I do it.