Had some issues last night trying to get my Android game to detect the fling gesture, basically a click and drag in mouse world.
I had the following code in my little View object.
private GestureDetector gestureDetector;
private View.OnTouchListener gestureListener;// Gesture detection
gestureDetector = new GestureDetector(new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
};
this.setOnTouchListener(gestureListener);
* Handle any fling action here
* @author dracox
*
*/
protected class MyGestureDetector extends SimpleOnGestureListener {
/**
* We need to capture any user line drawn request here.
*/
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
//This should only be caring about flings after init
gameWalls.add(new FireWall(new Point((int)e1.getRawX(), (int)e1.getRawY()), new Point((int)e2.getRawX(), (int)e2.getRawY())));
return true;
}
}
I fired up my emulator, but was not getting any fling action logged. However the onTouch events were been fired off.
Initially I though maybe the emulator couldn’t handle fling event, but after much searching I found the root of the problem.
Apparently, the onDown event was fired off first, which was consumed before it became a “fling”, so I had to override that too on my custom Gesture Detector class.
I was a little puzzled about returning true for the onDown handler, as the API stated return true to consume the event and false to let it pass.
/**
* Need to let onDown pass through, otherwise it will block the onFling event
*/
@Override
public boolean onDown(MotionEvent event) {
return true;
}
Now we’re flinging!