I developed an Android application that handles multitouch in a view. I mainly track several MotionEvents, which can occur as ACTION_UP, ACTION_MOVE, ... My rewritten onTouch method in the View class looks like this:
public boolean onTouch(View view, MotionEvent event) { int action = event.getAction() & MotionEvent.ACTION_MASK; if (action == MotionEvent.ACTION_DOWN) { float x = event.getX(); handleTouchStart(view, x); } else if (action == MotionEvent.ACTION_UP) { float x = event.getX(); handleTouchEnded(view, x); } else if (action == MotionEvent.ACTION_POINTER_DOWN) { int pointer = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT; float x = event.getX(pointer); handleTouchStart(view, x); } else if (action == MotionEvent.ACTION_POINTER_UP) { int pointer = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT; float x = event.getX(pointer); handleTouchEnded(view, x); } else if (action == MotionEvent.ACTION_MOVE) { int pointer = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT; float x = event.getX(pointer); handleTouchMoved(view, x); } return true; }
On my phone (Samsung Galaxy S) it works flawlessly. For each handleTouchStart (), the corresponding handleTouchEnded () is called. This is very important for my application. This is a two-dimensional scroller with a steering zone at the bottom left of the screen. If for some reason the handleTouchEnded () method is not called when the user lifts a finger from the touch screen, the player symbol continues to work.
I received reports from players on other devices (for example, HTC Desire) that in rare cases, the character really continues to work in the direction, even if they raised their finger from the steering area on the screen. I came to the conclusion that this can only happen if handleTouchEnded () is not called, which in turn means that the ACTION_UP (or ACTION_POINTER_UP) event is not generated.
Are there specific implementations of multi-touch device support? Is there no guarantee that for each ACTION_DOWN (or ACTION_POINTER_DOWN) MotionEvent, the corresponding ActionTION_UP (or ACTION_POINTER_UP) MotionEvent is called? Or am I missing something? Is my onTouch implementation even correct?
My broader question would be: are there better ways to handle multi-touch input for action-games? I basically simulate the left and right controls of the cross-pad of a gamepad. I found that the Android UI buttons do not provide enough flexibility because they cannot track onPress and onRelease events ... or are they?
Maximilian csuk
source share