{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

VelocityTracker

public final 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.

Summary

Public constructors

Public methods

final void
addPosition(long timeMillis, @NonNull Offset position)

Adds a position at the given time to the tracker.

final @NonNull Velocity

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

final void

Clears the tracked positions added by addPosition.

Extension functions

final void

Track the positions and timestamps inside this event change.

Public constructors

VelocityTracker

public VelocityTracker()

Public methods

addPosition

public final void addPosition(long timeMillis, @NonNull Offset position)

Adds a position at the given time to the tracker.

Call resetTracking to remove added Offsets.

See also
resetTracking

calculateVelocity

public final @NonNull Velocity calculateVelocity()

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

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

resetTracking

public final void resetTracking()

Clears the tracked positions added by addPosition.

Extension functions

VelocityTrackerKt.addPointerInputChange

public final void VelocityTrackerKt.addPointerInputChange(
    @NonNull VelocityTracker receiver,
    @NonNull PointerInputChange event
)

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
@NonNull PointerInputChange event

Pointer change to track.