Game Development Reference
In-Depth Information
Processing multi-touch events on Android
Until now, we have not handled any user interaction except the BACK button on Android.
In this recipe, we show how to process multi-touch events on Android.
Getting ready
You should be familiar with the concepts of multi-touch input handling. In Java, Android multi-
touch events are delivered inside the MotionEvent class, an instance of which is passed as
a parameter to the onTouchEvent() method of your Activity class. The MotionEvent
class contains all the information of the currently active and released touches. In order to
pass this information to our native code, we convert a single event carrying multiple touches
into a series of events holding data for a single touch. This keeps the JNI interoperation
simple and enables easy porting of our code.
How to do it...
Each Android activity supports multi-touch event handling. All we have to do is override
the onTouchEvent() method of the Activity class:
1.
First, we declare some internal constants to events related to individual touch points:
private static final int MOTION_MOVE = 0;
private static final int MOTION_UP = 1;
private static final int MOTION_DOWN = 2;
private static final int MOTION_START = -1;
private static final int MOTION_END = -2;
2.
The event handler uses the MotionEvent structure and extracts information about
individual touches. The SendMotion() function is declared in the native code and
contains the gesture decoding we call the via JNI from onTouchEvent() :
@Override public boolean onTouchEvent( MotionEvent event )
{
3.
Tell our native code we are going to send a series of events:
SendMotion( MOTION_START, 0, 0, false, MOTION_MOVE );
4.
Determine the event code and the ID of the irst touch:
int E = event.getAction() & MotionEvent.ACTION_MASK;
int nPointerID = event.getPointerId(
(event.getAction() &
MotionEvent.ACTION_POINTER_INDEX_MASK) >>
MotionEvent.ACTION_POINTER_INDEX_SHIFT );
try
{
 
Search WWH ::




Custom Search