public final class

VelocityTracker

extends Object
java.lang.Object
   ↳ android.view.VelocityTracker

Class Overview

Helper for tracking the velocity of touch events, for implementing flinging and other such gestures. Use obtain() to retrieve a new instance of the class when you are going to begin tracking, put the motion events you receive into it with addMovement(MotionEvent), and when you want to determine the velocity call computeCurrentVelocity(int) and then getXVelocity() and getXVelocity().

Summary

Public Methods
void addMovement(MotionEvent ev)
Add a user's movement to the tracker.
void clear()
Reset the velocity tracker back to its initial state.
void computeCurrentVelocity(int units)
Compute the current velocity based on the points that have been collected.
float getXVelocity()
Retrieve the last computed X velocity.
float getYVelocity()
Retrieve the last computed Y velocity.
static VelocityTracker obtain()
Retrieve a new VelocityTracker object to watch the velocity of a motion.
void recycle()
Return a VelocityTracker object back to be re-used by others.
[Expand]
Inherited Methods
From class java.lang.Object

Public Methods

public void addMovement (MotionEvent ev)

Add a user's movement to the tracker. You should call this for the initial ACTION_DOWN, the following ACTION_MOVE events that you receive, and the final ACTION_UP. You can, however, call this for whichever events you desire.

Parameters
ev The MotionEvent you received and would like to track.

public void clear ()

Reset the velocity tracker back to its initial state.

public void computeCurrentVelocity (int units)

Compute the current velocity based on the points that have been collected. Only call this when you actually want to retrieve velocity information, as it is relatively expensive. You can then retrieve the velocity with getXVelocity() and getYVelocity().

Parameters
units The units you would like the velocity in. A value of 1 provides pixels per millisecond, 1000 provides pixels per second, etc.

public float getXVelocity ()

Retrieve the last computed X velocity. You must first call computeCurrentVelocity(int) before calling this function.

Returns
  • The previously computed X velocity.

public float getYVelocity ()

Retrieve the last computed Y velocity. You must first call computeCurrentVelocity(int) before calling this function.

Returns
  • The previously computed Y velocity.

public static VelocityTracker obtain ()

Retrieve a new VelocityTracker object to watch the velocity of a motion. Be sure to call recycle() when done. You should generally only maintain an active object while tracking a movement, so that the VelocityTracker can be re-used elsewhere.

Returns
  • Returns a new VelocityTracker.

public void recycle ()

Return a VelocityTracker object back to be re-used by others. You must not touch the object after calling this function.