Game Development Reference
In-Depth Information
MotionEvent.getPointerIdentifier(int pointerIndex) that returns the pointer identifier based on
a pointer index. A pointer identifier will stay the same for a single finger as long as it touches the
screen. This is not necessarily true for the pointer index. It's important to understand the distinction
between the two and understand that you can't rely on the first touch to be index 0, ID 0,
because on some devices, notably the first version of the Xperia Play, the pointer ID would always
increment to 15 and then start over at 0, rather than reuse the lowest available number for an ID.
Let's start by examining how we can get to the pointer index of an event. We'll ignore the event
type for now.
int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >>
MotionEvent.ACTION_POINTER_ID_SHIFT;
You probably have the same thoughts that we had when we first implemented this. Before we
lose all faith in humanity, let's try to decipher what's happening here. We fetch the event type
from the MotionEvent via MotionEvent.getAction() . Good, we've done that before. Next we
perform a bitwise AND operation using the integer we get from the MotionEvent.getAction()
method and a constant called MotionEvent.ACTION_POINTER_ID_MASK . Now the fun begins.
That constant has a value of 0xff00 , so we essentially make all bits 0, other than bits 8 to 15,
which hold the pointer index of the event. The lower 8 bits of the integer returned by
event.getAction() hold the value of the event type, such as MotionEvent.ACTION_DOWN and its
siblings. We essentially throw away the event type by this bitwise operation. The shift should make
a bit more sense now. We shift by MotionEvent.ACTION_POINTER_ID_SHIFT , which has a value of 8,
so we basically move bits 8 through 15 to bits 0 through 7, arriving at the actual pointer index of
the event. With this, we can then get the coordinates of the event, as well as the pointer identifier.
Notice that our magic constants are called XXX_POINTER_ID_XXX instead of XXX_POINTER_INDEX_XXX
(which would make more sense, as we actually want to extract the pointer index, not the pointer
identifier). Well, the Android engineers must have been confused as well. In SDK version 8, they
deprecated those constants and introduced new constants called XXX_POINTER_INDEX_XXX ,
which have the exact same values as the deprecated ones. In order for legacy applications
that are written against SDK version 5 to continue working on newer Android versions, the old
constants are still made available.
So we now know how to get that mysterious pointer index that we can use to query for the
coordinates and the pointer identifier of the event.
The Action Mask and More Event Types
Next, we have to get the pure event type minus the additional pointer index that is encoded in
the integer returned by MotionEvent.getAction() . We just need to mask out the pointer index:
int action = event.getAction() & MotionEvent.ACTION_MASK;
OK, that was easy. Sadly, you'll only understand it if you know what that pointer index is, and
that it is actually encoded in the action.
What's left is to decode the event type as we did before. We already said that there are a few
new event types, so let's go through them:
ï?® MotionEvent.ACTION_POINTER_DOWN : This event happens for any additional
finger that touches the screen after the first finger touches. The first finger
still produces a MotionEvent.ACTION_DOWN event.
 
Search WWH ::




Custom Search