Game Development Reference
In-Depth Information
Adding Swipe and Multi-Touch Pinch for Zooming
As a bonus, this section describes how to use the Android multi-touch APIs to increase the
rotation speed of the icosahedron by the following:
Increasing the speed whenever finger-swiped to the right or decreasing
it when swiping to the left. The rotation switches from left to right
whenever a threshold value is reached.
Zooming the shape in or out whenever pinching inward or outward with
two fingers.
Listing 4-14 describes the additions to the ShadersActivity class to perform such tasks.
Listing 4-14. Swipe and Pinch Zooming with Multi-Touch
// default rotation speed
int speed = 10;
// pointer 1,2 XY coords
float p1X, p1Y, p2X, p2Y;
// deltas
float DX1, DX2;
// # of fingers
int fingers = 0;
@Override
public boolean onTouchEvent(MotionEvent e) {
int count = e.getPointerCount();
int action = e.getAction();
float X1 = 0f, Y1 = 0f, X2 = 0f, Y2 = 0f;
// finger 1 down
if (action == MotionEvent.ACTION_DOWN) {
p1X = e.getX(0);
p1Y = e.getY(0);
fingers = 1;
}
// finger 2 down
if (action == MotionEvent.ACTION_POINTER_2_DOWN) {
p2X = e.getX(1);
p2Y = e.getY(1);
fingers = 2;
}
// pointer 1 up
if (action == MotionEvent.ACTION_UP) {
X1 = e.getX(0);
Y1 = e.getY(0);
DX1 = X1 - p1X;
 
Search WWH ::




Custom Search