VelocityTracker
class VelocityTracker
Calculates the velocity of a pointer based on tracked pointer events or positions and timestamps. VelocityTracker is platform-specific, so the output result may vary depending on the platform.
The input data is provided by calling addPosition
or addPointerInputChange
. 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) =
platformVelocityTracker.addPosition(timeMillis, position)
Adds a position at the given time to the tracker.
Call resetTracking
to remove added Offset
s.
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
maximumVelocity | 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. |
fun resetTracking() = platformVelocityTracker.resetTracking()
Clears the tracked positions added by addPosition
.