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, float maxVelocity)
Compute the current velocity based on the points that have been collected.
void computeCurrentVelocity(int units)
Equivalent to invoking computeCurrentVelocity(int, float) with a maximum velocity of Float.MAX_VALUE.
abstract T getNextPoolable()
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.
abstract void setNextPoolable(T element)
[Expand]
Inherited Methods
From class java.lang.Object

Public Methods

public void addMovement (MotionEvent ev)

Since: API Level 1

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 ()

Since: API Level 1

Reset the velocity tracker back to its initial state.

public void computeCurrentVelocity (int units, float maxVelocity)

Since: API Level 4

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.
maxVelocity The maximum velocity that can be computed by this method. This value must be declared in the same unit as the units parameter. This value must be positive.

public void computeCurrentVelocity (int units)

Since: API Level 1

Equivalent to invoking computeCurrentVelocity(int, float) with a maximum velocity of Float.MAX_VALUE.

public abstract T getNextPoolable ()

Since: API Level

public float getXVelocity ()

Since: API Level 1

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 ()

Since: API Level 1

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 ()

Since: API Level 1

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 ()

Since: API Level 1

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

public abstract void setNextPoolable (T element)

Since: API Level