@Override public boolean onTouchEvent(MotionEvent event) { int action = event.getActionMasked(); int pointerIndex = event.findPointerIndex(pointerId); switch (action) { case MotionEvent.ACTION_MOVE: float x = event.getX(pointerIndex); float y = event.getY(pointerIndex); // Update the position of the pointer with the given pointerId break; case MotionEvent.ACTION_POINTER_UP: case MotionEvent.ACTION_UP: // Remove the pointer with the given pointerId from tracking pointerId = -1; break; case MotionEvent.ACTION_POINTER_DOWN: // Add the new pointer to tracking pointerId = event.getPointerId(pointerIndex); break; } return true; }
@Override public boolean onTouchEvent(MotionEvent event) { int action = event.getActionMasked(); int pointerIndex = event.findPointerIndex(pointerId); switch (action) { case MotionEvent.ACTION_DOWN: // Track the initial position of the pointer with the given pointerId initialX = event.getX(pointerIndex); initialY = event.getY(pointerIndex); break; case MotionEvent.ACTION_UP: // Track the final position of the pointer with the given pointerId float finalX = event.getX(pointerIndex); float finalY = event.getY(pointerIndex); // Determine if this was a swipe gesture if ((finalX - initialX) > MIN_SWIPE_DISTANCE) { // Move the character to the right } else if ((initialX - finalX) > MIN_SWIPE_DISTANCE) { // Move the character to the left } break; case MotionEvent.ACTION_POINTER_DOWN: // Add the new pointer to tracking pointerId = event.getPointerId(pointerIndex); break; } return true; }Both examples use the MotionEvent class from the android.view package, which is part of the Android framework.