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 the pointer index out:

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. I 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 will still produce a MotionEvent.ACTION_DOWN event.

MotionEvent.ACTION_POINTER_UP: This is analogous the previous action. This gets fired when a finger is lifted up from the screen and more than one finger is touching the screen. The last finger on the screen to go up will produce a MotionEvent.ACTION_UP event. This finger doesn't necessarily have to be the first finger that touched the screen.

Luckily we can just pretend that those two new event types are the same as the old MotionEvent.ACTION_UP and MotionEvent.ACTION_DOWN events.

The last difference is the fact that a single MotionEvent can have data for multiple events. Yes, you read that right. For this to happen, the merged events have to have the same type. In reality this will only happen for the MotionEvent.ACTION_MOVE event, so we only have to deal with this fact when processing said event type. To check how many events are contained in a single MotionEvent, we use the

MotionEvent.getPointerCount() method, which tells us for how many fingers the MotionEvent contains coordinates for. We then can fetch the pointer identifier and coordinates for the pointer indices 0 to MotionEvent.getPointerCount() - 1 via the MotionEvent.getX(), MotionEvent.getY(), and MotionEvent.getPointerId() methods.

+1 0

Post a comment