VelocityTrackerCompat

Added in 1.1.0

class VelocityTrackerCompat


Helper for accessing features in VelocityTracker.

Summary

Public functions

java-static Unit

Add a user's movement to the tracker.

java-static Unit

Reset the velocity tracker back to its initial state.

java-static Unit

Equivalent to invoking computeCurrentVelocity with a maximum velocity of Float.MAX_VALUE.

java-static Unit
computeCurrentVelocity(
    tracker: VelocityTracker,
    units: Int,
    maxVelocity: Float
)

Compute the current velocity based on the points that have been collected.

java-static Float
getAxisVelocity(tracker: VelocityTracker, axis: Int)

Equivalent to calling getAxisVelocity for axis and the active pointer.

java-static Float
getAxisVelocity(tracker: VelocityTracker, axis: Int, pointerId: Int)

Retrieve the last computed velocity for a given motion axis.

java-static Float
@ReplaceWith(expression = "tracker.getXVelocity(pointerId)")
getXVelocity(tracker: VelocityTracker!, pointerId: Int)

This function is deprecated.

Use getXVelocity directly.

java-static Float
@ReplaceWith(expression = "tracker.getYVelocity(pointerId)")
getYVelocity(tracker: VelocityTracker!, pointerId: Int)

This function is deprecated.

Use getYVelocity directly.

java-static Boolean
isAxisSupported(tracker: VelocityTracker, axis: Int)

Checks whether a given velocity-trackable MotionEvent axis is supported for velocity tracking by this VelocityTracker instance (refer to getAxisVelocity for a list of potentially velocity-trackable axes).

java-static Unit

Return a VelocityTracker object back to be re-used by others.

Public functions

addMovement

Added in 1.13.0
java-static fun addMovement(tracker: VelocityTracker, event: MotionEvent): Unit

Add a user's movement to the tracker.

For pointer events, 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
tracker: VelocityTracker

The VelocityTracker to add the movement to.

event: MotionEvent

The MotionEvent you received and would like to track.

clear

Added in 1.13.0
java-static fun clear(tracker: VelocityTracker): Unit

Reset the velocity tracker back to its initial state.

computeCurrentVelocity

Added in 1.13.0
java-static fun computeCurrentVelocity(tracker: VelocityTracker, units: Int): Unit

Equivalent to invoking computeCurrentVelocity with a maximum velocity of Float.MAX_VALUE.

computeCurrentVelocity

Added in 1.13.0
java-static fun computeCurrentVelocity(
    tracker: VelocityTracker,
    units: Int,
    maxVelocity: Float
): Unit

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 getAxisVelocity ()}.

Parameters
tracker: VelocityTracker

The VelocityTracker for which to compute velocity.

units: Int

The units you would like the velocity in. A value of 1 provides units per millisecond, 1000 provides units per second, etc. Note that the units referred to here are the same units with which motion is reported. For axes X and Y, the units are pixels.

maxVelocity: Float

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.

getAxisVelocity

Added in 1.12.0
java-static fun getAxisVelocity(tracker: VelocityTracker, axis: Int): Float

Equivalent to calling getAxisVelocity for axis and the active pointer.

Parameters
tracker: VelocityTracker

The VelocityTracker from which to get axis velocity.

axis: Int

Which axis' velocity to return.

Returns
Float

The previously computed velocity for axis for the active pointer if axis is supported for velocity tracking, or 0 if velocity tracking is not supported for the axis.

getAxisVelocity

Added in 1.12.0
java-static fun getAxisVelocity(tracker: VelocityTracker, axis: Int, pointerId: Int): Float

Retrieve the last computed velocity for a given motion axis. You must first call computeCurrentVelocity or computeCurrentVelocity before calling this function.

In addition to AXIS_X and AXIS_Y which have been supported since the introduction of this class, the following axes can be candidates for this method:

  • AXIS_SCROLL: supported via the platform starting UPSIDE_DOWN_CAKE. Supported via a fallback logic at all platform levels for the active pointer only.

Before accessing velocities of an axis using this method, check that your VelocityTracker instance supports the axis by using isAxisSupported.

Parameters
tracker: VelocityTracker

The VelocityTracker from which to get axis velocity.

axis: Int

Which axis' velocity to return.

pointerId: Int

Which pointer's velocity to return.

Returns
Float

The previously computed velocity for axis for pointer ID of id if axis is supported for velocity tracking, or 0 if velocity tracking is not supported for the axis.

See also
isAxisSupported

getXVelocity

Added in 1.1.0
Deprecated in 1.1.0
@ReplaceWith(expression = "tracker.getXVelocity(pointerId)")
java-static fun getXVelocity(tracker: VelocityTracker!, pointerId: Int): Float

Call getXVelocity. If running on a pre-HONEYCOMB device, returns getXVelocity.

getYVelocity

Added in 1.1.0
Deprecated in 1.1.0
@ReplaceWith(expression = "tracker.getYVelocity(pointerId)")
java-static fun getYVelocity(tracker: VelocityTracker!, pointerId: Int): Float

Call getYVelocity. If running on a pre-HONEYCOMB device, returns getYVelocity.

isAxisSupported

Added in 1.12.0
java-static fun isAxisSupported(tracker: VelocityTracker, axis: Int): Boolean

Checks whether a given velocity-trackable MotionEvent axis is supported for velocity tracking by this VelocityTracker instance (refer to getAxisVelocity for a list of potentially velocity-trackable axes).

Note that the value returned from this method will stay the same for a given instance, so a single check for axis support is enough per a VelocityTracker instance.

Parameters
tracker: VelocityTracker

The VelocityTracker for which to check axis support.

axis: Int

The axis to check for velocity support.

Returns
Boolean

true if axis is supported for velocity tracking, or false otherwise.

recycle

Added in 1.13.0
java-static fun recycle(tracker: VelocityTracker): Unit

Return a VelocityTracker object back to be re-used by others.

Call this method for your VelocityTracker when you have finished tracking velocity for the use-case you created this tracker for and decided that you no longer need it. This allows it to be returned back to the pool of trackers to be re-used by others.

You must not touch the object after calling this function. That is, don't call any methods on it, or pass it as an input to any of this class' compat APIs, as the instance is no longer valid for velocity tracking.

See also
recycle