page.title=Handling Multi-Touch Gestures parent.title=Using Touch Gestures parent.link=index.html trainingnavtop=true next.title=Dragging and Scaling next.link=scale.html @jd:body
InteractiveChart.zip
A multi-touch gesture is when multiple pointers (fingers) touch the screen at the same time. This lesson describes how to detect gestures that involve multiple pointers.
When multiple pointers touch the screen at the same time, the system generates the following touch events:
You keep track of individual pointers within a {@link android.view.MotionEvent} via each pointer's index and ID:
The order in which individual pointers appear within a motion event is undefined. Thus the index of a pointer can change from one event to the next, but the pointer ID of a pointer is guaranteed to remain constant as long as the pointer remains active. Use the {@link android.view.MotionEvent#getPointerId getPointerId()} method to obtain a pointer's ID to track the pointer across all subsequent motion events in a gesture. Then for successive motion events, use the {@link android.view.MotionEvent#findPointerIndex findPointerIndex()} method to obtain the pointer index for a given pointer ID in that motion event. For example:
private int mActivePointerId; public boolean onTouchEvent(MotionEvent event) { .... // Get the pointer ID mActivePointerId = event.getPointerId(0); // ... Many touch events later... // Use the pointer ID to find the index of the active pointer // and fetch its position int pointerIndex = event.findPointerIndex(mActivePointerId); // Get the pointer's current position float x = event.getX(pointerIndex); float y = event.getY(pointerIndex); }
You should always use the method {@link android.view.MotionEvent#getActionMasked getActionMasked()} (or better yet, the compatability version {@link android.support.v4.view.MotionEventCompat#getActionMasked MotionEventCompat.getActionMasked()}) to retrieve the action of a {@link android.view.MotionEvent}. Unlike the older {@link android.view.MotionEvent#getAction getAction()} method, {@link android.support.v4.view.MotionEventCompat#getActionMasked getActionMasked()} is designed to work with multiple pointers. It returns the masked action being performed, without including the pointer index bits. You can then use {@link android.support.v4.view.MotionEventCompat#getActionIndex getActionIndex()} to return the index of the pointer associated with the action. This is illustrated in the snippet below.
Note: This example uses the {@link android.support.v4.view.MotionEventCompat} class. This class is in the Support Library. You should use {@link android.support.v4.view.MotionEventCompat} to provide the best support for a wide range of platforms. Note that {@link android.support.v4.view.MotionEventCompat} is not a replacement for the {@link android.view.MotionEvent} class. Rather, it provides static utility methods to which you pass your {@link android.view.MotionEvent} object in order to receive the desired action associated with that event.
int action = MotionEventCompat.getActionMasked(event); // Get the index of the pointer associated with the action. int index = MotionEventCompat.getActionIndex(event); int xPos = -1; int yPos = -1; Log.d(DEBUG_TAG,"The action is " + actionToString(action)); if (event.getPointerCount() > 1) { Log.d(DEBUG_TAG,"Multitouch event"); // The coordinates of the current screen contact, relative to // the responding View or Activity. xPos = (int)MotionEventCompat.getX(event, index); yPos = (int)MotionEventCompat.getY(event, index); } else { // Single touch event Log.d(DEBUG_TAG,"Single touch event"); xPos = (int)MotionEventCompat.getX(event, index); yPos = (int)MotionEventCompat.getY(event, index); } ... // Given an action int, returns a string description public static String actionToString(int action) { switch (action) { case MotionEvent.ACTION_DOWN: return "Down"; case MotionEvent.ACTION_MOVE: return "Move"; case MotionEvent.ACTION_POINTER_DOWN: return "Pointer Down"; case MotionEvent.ACTION_UP: return "Up"; case MotionEvent.ACTION_POINTER_UP: return "Pointer Up"; case MotionEvent.ACTION_OUTSIDE: return "Outside"; case MotionEvent.ACTION_CANCEL: return "Cancel"; } return ""; }
For more discussion of multi-touch and some examples, see the lesson Dragging and Scaling.