Ir al contenido

Más visitados

Visitados recientemente

navigation

Tracking Touch and Pointer Movements

This lesson teaches you to

  1. Track velocity
  2. Using pointer capture

You should also read

Try it out

Download the sample

InteractiveChart.zip

This lesson describes how to track movement in touch events.

A new onTouchEvent() is triggered with an ACTION_MOVE event whenever the current touch contact position, pressure, or size changes. As described in Detecting Common Gestures, all of these events are recorded in the MotionEvent parameter of onTouchEvent().

Because finger-based touch isn't always the most precise form of interaction, detecting touch events is often based more on movement than on simple contact. To help apps distinguish between movement-based gestures (such as a swipe) and non-movement gestures (such as a single tap), Android includes the notion of "touch slop". Touch slop refers to the distance in pixels a user's touch can wander before the gesture is interpreted as a movement-based gesture. For more discussion of this topic, see Managing Touch Events in a ViewGroup.

There are several different ways to track movement in a gesture, depending on the needs of your application. For example:

Track velocity

You could have a movement-based gesture that is simply based on the distance and/or direction the pointer traveled. But velocity often is a determining factor in tracking a gesture's characteristics or even deciding whether the gesture occurred. To make velocity calculation easier, Android provides the VelocityTracker class. VelocityTracker helps you track the velocity of touch events. This is useful for gestures in which velocity is part of the criteria for the gesture, such as a fling.

Here is a simple example that illustrates the purpose of the methods in the VelocityTracker API:

public class MainActivity extends Activity {
    private static final String DEBUG_TAG = "Velocity";
        ...
    private VelocityTracker mVelocityTracker = null;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int index = event.getActionIndex();
        int action = event.getActionMasked();
        int pointerId = event.getPointerId(index);

        switch(action) {
            case MotionEvent.ACTION_DOWN:
                if(mVelocityTracker == null) {
                    // Retrieve a new VelocityTracker object to watch the
                    // velocity of a motion.
                    mVelocityTracker = VelocityTracker.obtain();
                }
                else {
                    // Reset the velocity tracker back to its initial state.
                    mVelocityTracker.clear();
                }
                // Add a user's movement to the tracker.
                mVelocityTracker.addMovement(event);
                break;
            case MotionEvent.ACTION_MOVE:
                mVelocityTracker.addMovement(event);
                // When you want to determine the velocity, call
                // computeCurrentVelocity(). Then call getXVelocity()
                // and getYVelocity() to retrieve the velocity for each pointer ID.
                mVelocityTracker.computeCurrentVelocity(1000);
                // Log velocity of pixels per second
                // Best practice to use VelocityTrackerCompat where possible.
                Log.d("", "X velocity: " +
                        VelocityTrackerCompat.getXVelocity(mVelocityTracker,
                        pointerId));
                Log.d("", "Y velocity: " +
                        VelocityTrackerCompat.getYVelocity(mVelocityTracker,
                        pointerId));
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                // Return a VelocityTracker object back to be re-used by others.
                mVelocityTracker.recycle();
                break;
        }
        return true;
    }
}

Using pointer capture

Some apps, such as games, remote desktop, and virtualization clients, greatly benefit from getting control over the mouse pointer. Pointer capture is a feature available in Android 8.0 (API level 26) and later that provides such control by delivering all mouse events to a focused view in your app.

Requesting pointer capture

A view in your app can request pointer capture only when the view hierarchy that contains it has focus. For this reason, you should request pointer capture when there's a specific user action on the view, such as during an onClick() event, or in the onWindowFocusChanged() event handler of your activity.

To request pointer capture, call the requestPointerCapture() method on the view. The following code example shows how to request pointer capture when the user clicks a view:

@Override
public void onClick(View view) {
    view.requestPointerCapture();
}

Once the request to capture the pointer is successful, Android calls onPointerCaptureChange(true). The system delivers the mouse events to the focused view in your app as long as it's in the same view hierarchy as the view that requested the capture. Other apps stop receiving mouse events until the capture is released, including ACTION_OUTSIDE events. Android delivers pointer events from sources other than the mouse normally, but the mouse pointer is not visible anymore.

Handling captured pointer events

Once a view has successfully acquired the pointer capture, Android starts delivering the mouse events. Your focused view can handle the events by performing one of the following tasks:

  1. If you're using a custom view, override onCapturedPointerEvent(MotionEvent).
  2. Otherwise, register an OnCapturedPointerListener.

The following code example shows how to implement onCapturedPointerEvent(MotionEvent):

@Override
public boolean onCapturedPointerEvent(MotionEvent motionEvent) {
  // Get the coordinates required by your app
  float verticalOffset = motionEvent.getY();
  // Use the coordinates to update your view and return true if the event was
  // successfully processed
  return true;
}

The following code example shows how to register an OnCapturedPointerListener:

myView.setOnCapturedPointerListener(new View.OnCapturedPointerListener() {
  @Override
  public boolean onCapturedPointer (View view, MotionEvent motionEvent) {
    // Get the coordinates required by your app
    float horizontalOffset = motionEvent.getX();
    // Use the coordinates to update your view and return true if the event was
    // successfully processed
    return true;
  }
});

Whether you use a custom view or register a listener, your view receives a MotionEvent with pointer coordinates that specify relative movements such as X/Y deltas, similar to the coordinates delivered by a trackball device. You can retrieve the coordinates by using getX() and getY().

Releasing pointer capture

The view in your app can release the pointer capture by calling releasePointerCapture(), as shown in the following code example:

@Override
public void onClick(View view) {
    view.releasePointerCapture();
}

The system can take the capture away from the view without you explicitly calling releasePointerCapture(), most commonly because the view hierarchy that contains the view that requested capture has lost focus.

Este sitio usa cookies para almacenar tus preferencias específicas de idioma y visualización.

Obtén las noticias y sugerencias más recientes para programadores de Android, que te ayudarán a lograr el éxito en Google Play.

* Campos obligatorios

¡Muy bien!

Sigue a Google Developers en WeChat

¿Deseas explorar este sitio en ?

Solicitaste ver la página en , pero tu preferencia de idioma actual para este sitio es .

¿Quieres cambiar tu preferencia de idioma y explorar este sitio en ? Si deseas cambiarla en otro momento, usa el menú de idioma que se encuentra en la parte inferior de cada página.

Esta clase requiere un nivel de API igual o superior a

El documento está oculto porque el nivel de API que seleccionaste para la documentación es . Puedes cambiar el nivel de API de la documentación con el selector que está sobre el menú de navegación izquierdo.

Para obtener más información sobre cómo especificar el nivel de API que requiere tu app, consulta el artículo sobre Compatibilidad con diferentes versiones de la plataforma.

Take a short survey?
Help us improve the Android developer experience. (April 2018 — Developer Survey)