# VelocityTrackerCompat

Artifact: [androidx.core:core](https://developer.android.com/jetpack/androidx/releases/core) [View Source](https://cs.android.com/search?q=file:androidx/core/view/VelocityTrackerCompat.java+class:androidx.core.view.VelocityTrackerCompat) Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

*** ** * ** ***


```
class VelocityTrackerCompat
```

<br />

*** ** * ** ***

Helper for accessing features in `https://developer.android.com/reference/android/view/VelocityTracker.html`.

## Summary

| ### Public functions |
|---|---|
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/core/view/VelocityTrackerCompat#addMovement(android.view.VelocityTracker,android.view.MotionEvent)(tracker: https://developer.android.com/reference/android/view/VelocityTracker.html, event: https://developer.android.com/reference/android/view/MotionEvent.html)` Add a user's movement to the tracker. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/core/view/VelocityTrackerCompat#clear(android.view.VelocityTracker)(tracker: https://developer.android.com/reference/android/view/VelocityTracker.html)` Reset the velocity tracker back to its initial state. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/core/view/VelocityTrackerCompat#computeCurrentVelocity(android.view.VelocityTracker,int)(tracker: https://developer.android.com/reference/android/view/VelocityTracker.html, units: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Equivalent to invoking `https://developer.android.com/reference/kotlin/androidx/core/view/VelocityTrackerCompat#computeCurrentVelocity(android.view.VelocityTracker,int,float)` with a maximum velocity of Float.MAX_VALUE. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/core/view/VelocityTrackerCompat#computeCurrentVelocity(android.view.VelocityTracker,int,float)( tracker: https://developer.android.com/reference/android/view/VelocityTracker.html, units: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, maxVelocity: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html )` Compute the current velocity based on the points that have been collected. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` | `https://developer.android.com/reference/kotlin/androidx/core/view/VelocityTrackerCompat#getAxisVelocity(android.view.VelocityTracker,int)(tracker: https://developer.android.com/reference/android/view/VelocityTracker.html, axis: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Equivalent to calling `https://developer.android.com/reference/kotlin/androidx/core/view/VelocityTrackerCompat#getAxisVelocity(android.view.VelocityTracker,int,int)` for `axis` and the active pointer. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` | `https://developer.android.com/reference/kotlin/androidx/core/view/VelocityTrackerCompat#getAxisVelocity(android.view.VelocityTracker,int,int)(tracker: https://developer.android.com/reference/android/view/VelocityTracker.html, axis: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, pointerId: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Retrieve the last computed velocity for a given motion axis. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/ReplaceWith(expression = "tracker.getXVelocity(pointerId)") [getXVelocity](https://developer.android.com/reference/kotlin/androidx/core/view/VelocityTrackerCompat#getXVelocity(android.view.VelocityTracker,int))(tracker: https://developer.android.com/reference/android/view/VelocityTracker.html!, pointerId: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` **This function is deprecated.** Use `https://developer.android.com/reference/android/view/VelocityTracker.html#getXVelocity(int)` directly. <br /> |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/ReplaceWith(expression = "tracker.getYVelocity(pointerId)") [getYVelocity](https://developer.android.com/reference/kotlin/androidx/core/view/VelocityTrackerCompat#getYVelocity(android.view.VelocityTracker,int))(tracker: https://developer.android.com/reference/android/view/VelocityTracker.html!, pointerId: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` **This function is deprecated.** Use `https://developer.android.com/reference/android/view/VelocityTracker.html#getYVelocity(int)` directly. <br /> |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/core/view/VelocityTrackerCompat#isAxisSupported(android.view.VelocityTracker,int)(tracker: https://developer.android.com/reference/android/view/VelocityTracker.html, axis: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Checks whether a given velocity-trackable `https://developer.android.com/reference/android/view/MotionEvent.html` axis is supported for velocity tracking by this `https://developer.android.com/reference/android/view/VelocityTracker.html` instance (refer to `https://developer.android.com/reference/kotlin/androidx/core/view/VelocityTrackerCompat#getAxisVelocity(android.view.VelocityTracker,int,int)` for a list of potentially velocity-trackable axes). |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/core/view/VelocityTrackerCompat#recycle(android.view.VelocityTracker)(tracker: https://developer.android.com/reference/android/view/VelocityTracker.html)` Return a `https://developer.android.com/reference/android/view/VelocityTracker.html` object back to be re-used by others. |

## Public functions

### addMovement

Added in [1.13.0](https://developer.android.com/jetpack/androidx/releases/core#1.13.0)

```
java-static fun addMovement(tracker: VelocityTracker, event: MotionEvent): Unit
```

Add a user's movement to the tracker.

For pointer events, you should call this for the initial `https://developer.android.com/reference/android/view/MotionEvent.html#ACTION_DOWN`, the following `https://developer.android.com/reference/android/view/MotionEvent.html#ACTION_MOVE` events that you receive, and the final `https://developer.android.com/reference/android/view/MotionEvent.html#ACTION_UP`. You can, however, call this for whichever events you desire.

| Parameters |
|---|---|
| `tracker: https://developer.android.com/reference/android/view/VelocityTracker.html` | The `https://developer.android.com/reference/android/view/VelocityTracker.html` to add the movement to. |
| `event: https://developer.android.com/reference/android/view/MotionEvent.html` | The MotionEvent you received and would like to track. |

### clear

Added in [1.13.0](https://developer.android.com/jetpack/androidx/releases/core#1.13.0)

```
java-static fun clear(tracker: VelocityTracker): Unit
```

Reset the velocity tracker back to its initial state.

### computeCurrentVelocity

Added in [1.13.0](https://developer.android.com/jetpack/androidx/releases/core#1.13.0)

```
java-static fun computeCurrentVelocity(tracker: VelocityTracker, units: Int): Unit
```

Equivalent to invoking `https://developer.android.com/reference/kotlin/androidx/core/view/VelocityTrackerCompat#computeCurrentVelocity(android.view.VelocityTracker,int,float)` with a maximum velocity of Float.MAX_VALUE.

### computeCurrentVelocity

Added in [1.13.0](https://developer.android.com/jetpack/androidx/releases/core#1.13.0)

```
java-static fun computeCurrentVelocity(
    tracker: VelocityTracker,
    units: Int,
    maxVelocity: Float
): Unit
```

Compute the current velocity based on the points that have been collected. Only call this when you actually want to retrieve velocity information, as it is relatively expensive. You can then retrieve the velocity with `https://developer.android.com/reference/kotlin/androidx/core/view/VelocityTrackerCompat#getAxisVelocity(android.view.VelocityTracker,int)` ()}.

| Parameters |
|---|---|
| `tracker: https://developer.android.com/reference/android/view/VelocityTracker.html` | The `https://developer.android.com/reference/android/view/VelocityTracker.html` for which to compute velocity. |
| `units: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The units you would like the velocity in. A value of 1 provides units per millisecond, 1000 provides units per second, etc. Note that the units referred to here are the same units with which motion is reported. For axes X and Y, the units are pixels. |
| `maxVelocity: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` | The maximum velocity that can be computed by this method. This value must be declared in the same unit as the units parameter. This value must be positive. |

### getAxisVelocity

Added in [1.12.0](https://developer.android.com/jetpack/androidx/releases/core#1.12.0)

```
java-static fun getAxisVelocity(tracker: VelocityTracker, axis: Int): Float
```

Equivalent to calling `https://developer.android.com/reference/kotlin/androidx/core/view/VelocityTrackerCompat#getAxisVelocity(android.view.VelocityTracker,int,int)` for `axis` and the active pointer.

| Parameters |
|---|---|
| `tracker: https://developer.android.com/reference/android/view/VelocityTracker.html` | The `https://developer.android.com/reference/android/view/VelocityTracker.html` from which to get axis velocity. |
| `axis: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | Which axis' velocity to return. Value is one of the following: - `MotionEvent.AXIS_X` - `MotionEvent.AXIS_Y` - `MotionEvent.AXIS_SCROLL` <br /> |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` | The previously computed velocity for `axis` for the active pointer if `axis` is supported for velocity tracking, or 0 if velocity tracking is not supported for the axis. |

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/core/view/VelocityTrackerCompat#isAxisSupported(android.view.VelocityTracker,int)` |   |
| `https://developer.android.com/reference/kotlin/androidx/core/view/VelocityTrackerCompat#getAxisVelocity(android.view.VelocityTracker,int,int)` |   |

### getAxisVelocity

Added in [1.12.0](https://developer.android.com/jetpack/androidx/releases/core#1.12.0)

```
java-static fun getAxisVelocity(tracker: VelocityTracker, axis: Int, pointerId: Int): Float
```

Retrieve the last computed velocity for a given motion axis. You must first call `https://developer.android.com/reference/android/view/VelocityTracker.html#computeCurrentVelocity(int)` or `https://developer.android.com/reference/android/view/VelocityTracker.html#computeCurrentVelocity(int, float)` before calling this function.

In addition to `https://developer.android.com/reference/android/view/MotionEvent.html#AXIS_X` and `https://developer.android.com/reference/android/view/MotionEvent.html#AXIS_Y` which have been supported since the introduction of this class, the following axes can be candidates for this method:

- `https://developer.android.com/reference/android/view/MotionEvent.html#AXIS_SCROLL`: supported via the platform starting `https://developer.android.com/reference/android/os/Build.VERSION_CODES.html#UPSIDE_DOWN_CAKE`. Supported via a fallback logic at all platform levels for the active pointer only.

Before accessing velocities of an axis using this method, check that your `https://developer.android.com/reference/android/view/VelocityTracker.html` instance supports the axis by using `https://developer.android.com/reference/kotlin/androidx/core/view/VelocityTrackerCompat#isAxisSupported(android.view.VelocityTracker,int)`.

| Parameters |
|---|---|
| `tracker: https://developer.android.com/reference/android/view/VelocityTracker.html` | The `https://developer.android.com/reference/android/view/VelocityTracker.html` from which to get axis velocity. |
| `axis: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | Which axis' velocity to return. Value is one of the following: - `MotionEvent.AXIS_X` - `MotionEvent.AXIS_Y` - `MotionEvent.AXIS_SCROLL` <br /> |
| `pointerId: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | Which pointer's velocity to return. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` | The previously computed velocity for `axis` for pointer ID of `id` if `axis` is supported for velocity tracking, or 0 if velocity tracking is not supported for the axis. |

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/core/view/VelocityTrackerCompat#isAxisSupported(android.view.VelocityTracker,int)` |   |

### getXVelocity

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0) Deprecated in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
@ReplaceWith(expression = "tracker.getXVelocity(pointerId)")
java-static fun [getXVelocity](https://developer.android.com/reference/kotlin/androidx/core/view/VelocityTrackerCompat#getXVelocity(android.view.VelocityTracker,int))(tracker: VelocityTracker!, pointerId: Int): Float
```

> [!CAUTION]
> **This function is deprecated.**   
>
> Use `https://developer.android.com/reference/android/view/VelocityTracker.html#getXVelocity(int)` directly.

Call `https://developer.android.com/reference/android/view/VelocityTracker.html#getXVelocity(int)`. If running on a pre-`https://developer.android.com/reference/android/os/Build.VERSION_CODES.html#HONEYCOMB` device, returns `https://developer.android.com/reference/android/view/VelocityTracker.html#getXVelocity()`.

### getYVelocity

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0) Deprecated in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
@ReplaceWith(expression = "tracker.getYVelocity(pointerId)")
java-static fun [getYVelocity](https://developer.android.com/reference/kotlin/androidx/core/view/VelocityTrackerCompat#getYVelocity(android.view.VelocityTracker,int))(tracker: VelocityTracker!, pointerId: Int): Float
```

> [!CAUTION]
> **This function is deprecated.**   
>
> Use `https://developer.android.com/reference/android/view/VelocityTracker.html#getYVelocity(int)` directly.

Call `https://developer.android.com/reference/android/view/VelocityTracker.html#getYVelocity(int)`. If running on a pre-`https://developer.android.com/reference/android/os/Build.VERSION_CODES.html#HONEYCOMB` device, returns `https://developer.android.com/reference/android/view/VelocityTracker.html#getYVelocity()`.

### isAxisSupported

Added in [1.12.0](https://developer.android.com/jetpack/androidx/releases/core#1.12.0)

```
java-static fun isAxisSupported(tracker: VelocityTracker, axis: Int): Boolean
```

Checks whether a given velocity-trackable `https://developer.android.com/reference/android/view/MotionEvent.html` axis is supported for velocity tracking by this `https://developer.android.com/reference/android/view/VelocityTracker.html` instance (refer to `https://developer.android.com/reference/kotlin/androidx/core/view/VelocityTrackerCompat#getAxisVelocity(android.view.VelocityTracker,int,int)` for a list of potentially velocity-trackable axes).

Note that the value returned from this method will stay the same for a given instance, so a single check for axis support is enough per a `https://developer.android.com/reference/android/view/VelocityTracker.html` instance.

| Parameters |
|---|---|
| `tracker: https://developer.android.com/reference/android/view/VelocityTracker.html` | The `https://developer.android.com/reference/android/view/VelocityTracker.html` for which to check axis support. |
| `axis: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The axis to check for velocity support. Value is one of the following: - `MotionEvent.AXIS_X` - `MotionEvent.AXIS_Y` - `MotionEvent.AXIS_SCROLL` <br /> |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `true` if `axis` is supported for velocity tracking, or `false` otherwise. |

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/core/view/VelocityTrackerCompat#getAxisVelocity(android.view.VelocityTracker,int,int)` |   |
| `https://developer.android.com/reference/kotlin/androidx/core/view/VelocityTrackerCompat#getAxisVelocity(android.view.VelocityTracker,int)` |   |

### recycle

Added in [1.13.0](https://developer.android.com/jetpack/androidx/releases/core#1.13.0)

```
java-static fun recycle(tracker: VelocityTracker): Unit
```

Return a `https://developer.android.com/reference/android/view/VelocityTracker.html` object back to be re-used by others.

Call this method for your `https://developer.android.com/reference/android/view/VelocityTracker.html` when you have finished tracking velocity for the use-case you created this tracker for and decided that you no longer need it. This allows it to be returned back to the pool of trackers to be re-used by others.

You must **not** touch the object after calling this function. That is, don't call any methods on it, or pass it as an input to any of this class' compat APIs, as the instance is no longer valid for velocity tracking.

| See also |
|---|---|
| `https://developer.android.com/reference/android/view/VelocityTracker.html#recycle()` |   |