Game Development Reference
In-Depth Information
case MotionEvent. ACTION_UP :
case MotionEvent. ACTION_POINTER_UP :
case MotionEvent. ACTION_OUTSIDE :
case MotionEvent. ACTION_CANCEL :
touched[i] = false ;
id[i] = -1;
x[i] = ( int ) event.getX(i);
y[i] = ( int ) event.getY(i);
break ;
case MotionEvent. ACTION_MOVE :
touched[i] = true;
id[i] = pointerId;
x[i] = ( int ) event.getX(i);
y[i] = ( int ) event.getY(i);
break ;
}
}
updateTextView();
return true ;
}
}
Note the TargetApi annotation at the top of the class definition. This is necessary as we access
APIs that are not part of the minimum SDK we specified when creating the project (Android 1.5).
Every time we use APIs that are not part of that minimum SDK, we need to put that annotation
on top of the class using those APIs!
We implement the OnTouchListener interface as before. To keep track of the coordinates and
touch state of the ten fingers, we add three new member arrays that will hold that information
for us. The arrays x and y hold the coordinates for each pointer ID, and the array touched stores
whether the finger with that pointer ID is down.
Next we took the freedom to create a little helper method that will output the current state of
the fingers to the TextView . The method simply iterates through all the ten finger states and
concatenates them via a StringBuilder . The final text is set to the TextView .
The onCreate() method sets up our activity and registers it as an OnTouchListener with the
TextView . We already know that part by heart.
Now for the scary part: the onTouch() method.
We start off by getting the event type by masking the integer returned by event.getAction() .
Next, we extract the pointer index and fetch the corresponding pointer identifier from the
MotionEvent , as discussed earlier.
The heart of the onTouch() method is that big nasty switch statement, which we already used in
a reduced form to process single-touch events. We group all the events into three categories on
a high level:
ï?® A touch - down event happened ( MotionEvent.ACTION_DOWN or
MotionEvent.ACTION_PONTER_DOWN ): We set the touch state for the pointer
identifier to true , and we also save the current coordinates of that pointer.
Search WWH ::




Custom Search