VelocityTracker


Computes a pointer's velocity.

The input data is provided by calling addPosition. Adding data is cheap.

To obtain a velocity, call calculateVelocity. This will compute the velocity based on the data added so far. Only call this when you need to use the velocity, as it is comparatively expensive.

The quality of the velocity estimation will be better if more data points have been received.

Summary

Public constructors

Cmn

Public functions

Unit
addPosition(timeMillis: Long, position: Offset)

Adds a position at the given time to the tracker.

Cmn
Velocity

Computes the estimated velocity of the pointer at the time of the last provided data point.

Cmn
Velocity
calculateVelocity(maximumVelocity: Velocity)

Computes the estimated velocity of the pointer at the time of the last provided data point.

Cmn
Unit

Clears the tracked positions added by addPosition.

Cmn

Extension functions

Unit

Track the positions and timestamps inside this event change.

Cmn

Public constructors

VelocityTracker

VelocityTracker()

Public functions

addPosition

fun addPosition(timeMillis: Long, position: Offset): Unit

Adds a position at the given time to the tracker.

Call resetTracking to remove added Offsets.

See also
resetTracking

calculateVelocity

fun calculateVelocity(): Velocity

Computes the estimated velocity of the pointer at the time of the last provided data point.

The velocity calculated will not be limited. Unlike calculateVelocity(maximumVelocity) the resulting velocity won't be limited.

This can be expensive. Only call this when you need the velocity.

calculateVelocity

fun calculateVelocity(maximumVelocity: Velocity): Velocity

Computes the estimated velocity of the pointer at the time of the last provided data point.

The method allows specifying the maximum absolute value for the calculated velocity. If the absolute value of the calculated velocity exceeds the specified maximum, the return value will be clamped down to the maximum. For example, if the absolute maximum velocity is specified as "20", a calculated velocity of "25" will be returned as "20", and a velocity of "-30" will be returned as "-20".

Parameters
maximumVelocity: Velocity

the absolute values of the X and Y maximum velocities to be returned in units/second. units is the units of the positions provided to this VelocityTracker.

resetTracking

fun resetTracking(): Unit

Clears the tracked positions added by addPosition.

Extension functions

addPointerInputChange

fun VelocityTracker.addPointerInputChange(event: PointerInputChange): Unit

Track the positions and timestamps inside this event change.

For optimal tracking, this should be called for the DOWN event and all MOVE events, including any touch-slop-captured MOVE event.

Since Compose uses relative positions inside PointerInputChange, this should be taken into consideration when using this method. Right now, we use the first down to initialize an accumulator and use subsequent deltas to simulate an actual movement from relative positions in PointerInputChange. This is required because VelocityTracker requires data that can be fit into a curve, which might not happen with relative positions inside a moving target for instance.

Parameters
event: PointerInputChange

Pointer change to track.