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

VelocityTracker1D

public final class VelocityTracker1D


A velocity tracker calculating velocity in 1 dimension.

Add displacement data points using addDataPoint, and obtain velocity using calculateVelocity.

Note: for calculating touch-related or other 2 dimensional/planar velocities, please use VelocityTracker, which handles velocity tracking across both X and Y dimensions at once.

Summary

Public constructors

VelocityTracker1D(boolean isDataDifferential)

Constructor to create a new velocity tracker.

Public methods

final void
addDataPoint(long timeMillis, float dataPoint)

Adds a data point for velocity calculation at a given time, timeMillis.

final float

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

final boolean
final void

Clears data points added by addDataPoint.

Public constructors

VelocityTracker1D

public VelocityTracker1D(boolean isDataDifferential)

Constructor to create a new velocity tracker. It allows to specify whether or not the tracker should consider the data ponits provided via addDataPoint as differential or non-differential.

Differential data ponits represent change in displacement. For instance, differential data points of 2, -1, 5 represent: the object moved by "2" units, then by "-1" units, then by "5" units. An example use case for differential data points is when tracking velocity for an object whose displacements (or change in positions) over time are known.

Non-differential data ponits represent position of the object whose velocity is tracked. For instance, non-differential data points of 2, -1, 5 represent: the object was at position "2", then at position "-1", then at position "5". An example use case for non-differential data points is when tracking velocity for an object whose positions on a geometrical axis over different instances of time are known.

Parameters
boolean isDataDifferential

true if the data ponits provided to the constructed tracker are differential. false otherwise.

Public methods

addDataPoint

public final void addDataPoint(long timeMillis, float dataPoint)

Adds a data point for velocity calculation at a given time, timeMillis. The data ponit represents an amount of a change in position (for differential data points), or an absolute position (for non-differential data points). Whether or not the tracker handles differential data points is decided by isDataDifferential, which is set once and finally during the construction of the tracker.

Use the same units for the data points provided. For example, having some data points in cm and some in m will result in incorrect velocity calculations, as this method (and the tracker) has no knowledge of the units used.

calculateVelocity

public final float calculateVelocity()

Computes the estimated velocity at the time of the last provided data point. The units of velocity will be units/second, where units is the units of the data points provided via addDataPoint.

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

isDataDifferential

public final boolean isDataDifferential()

resetTracking

public final void resetTracking()

Clears data points added by addDataPoint.