VelocityTracker

Class

Common
class 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.

Functions

fun addPosition(timeMillis: Long, position: Offset)

Adds a position at the given time to the tracker.

Call resetTracking to remove added Offsets.

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.

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

maximumVelocitythe 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.
fun resetTracking()

Clears the tracked positions added by addPosition.