# FlingAnimation

Artifact: [androidx.dynamicanimation:dynamicanimation](https://developer.android.com/jetpack/androidx/releases/dynamicanimation) [View Source](https://cs.android.com/search?q=file:androidx/dynamicanimation/animation/FlingAnimation.java+class:androidx.dynamicanimation.animation.FlingAnimation) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/dynamicanimation#1.0.0)

*** ** * ** ***


```
class FlingAnimation : DynamicAnimation<FlingAnimation!>
```

<br />

|---|---|---|
| [kotlin.Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html) |||
| ↳ | [androidx.dynamicanimation.animation.DynamicAnimation](https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation)\<[FlingAnimation](https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/FlingAnimation)!\> ||
|   | ↳ | [androidx.dynamicanimation.animation.FlingAnimation](https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/FlingAnimation) |

*** ** * ** ***

Fling animation is an animation that continues an initial momentum (most often from gesture velocity) and gradually slows down. The fling animation will come to a stop when the velocity of the animation is below the threshold derived from `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation#setMinimumVisibleChange(float)`, or when the value of the animation has gone beyond the min or max value defined via `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation#setMinValue(float)` or `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation#setMaxValue(float)`. It is recommended to restrict the fling animation with min and/or max value, such that the animation can end when it goes beyond screen bounds, thus preserving CPU cycles and resources.

For example, you can create a fling animation that animates the translationX of a view:

```kotlin
FlingAnimation flingAnim = new FlingAnimation(view, DynamicAnimation.TRANSLATION_X)
        // Sets the start velocity to -2000 (pixel/s)
        .setStartVelocity(-2000)
        // Optional but recommended to set a reasonable min and max range for the animation.
        // In this particular case, we set the min and max to -200 and 2000 respectively.
        .setMinValue(-200).setMaxValue(2000);
flingAnim.start();
```

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/FlingAnimation#FlingAnimation(androidx.dynamicanimation.animation.FloatValueHolder)(floatValueHolder: https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/FloatValueHolder!)` This creates a FlingAnimation that animates a `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/FloatValueHolder` instance. |
| `<K> https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/FlingAnimation#FlingAnimation(K,androidx.dynamicanimation.animation.FloatPropertyCompat<K>)(object: K!, property: https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/FloatPropertyCompat<K!>!)` This creates a FlingAnimation that animates the property of the given object. |

| ### Public functions |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` | `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/FlingAnimation#getFriction()()` Returns the friction being set on the animation via `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/FlingAnimation#setFriction(float)`. |
| `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/FlingAnimation!` | `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/FlingAnimation#setFriction(float)(friction: @https://developer.android.com/reference/kotlin/androidx/annotation/FloatRange(from = 0.0, fromInclusive = false) https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)` Sets the friction for the fling animation. |
| `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/FlingAnimation!` | `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/FlingAnimation#setMaxValue(float)(maxValue: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)` Sets the max value of the animation. |
| `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/FlingAnimation!` | `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/FlingAnimation#setMinValue(float)(minValue: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)` Sets the min value of the animation. |
| `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/FlingAnimation!` | `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/FlingAnimation#setStartVelocity(float)(startVelocity: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)` Start velocity of the animation. |

### Inherited Constants

From [androidx.dynamicanimation.animation.DynamicAnimation](https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation) `const https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation.ViewProperty!` `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation#ALPHA()`

View's alpha property.
`const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation#MIN_VISIBLE_CHANGE_ALPHA() = 0.00390625f`

The minimum visible change in alpha that can be visible to users.
`const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation#MIN_VISIBLE_CHANGE_PIXELS() = 1.0f`

The minimum visible change in pixels that can be visible to users.
`const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation#MIN_VISIBLE_CHANGE_ROTATION_DEGREES() = 0.1f`

The minimum visible change in degrees that can be visible to users.
`const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation#MIN_VISIBLE_CHANGE_SCALE() = 0.002f`

The minimum visible change in scale that can be visible to users.
`const https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation.ViewProperty!` `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation#ROTATION()`

View's rotation property.
`const https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation.ViewProperty!` `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation#ROTATION_X()`

View's rotationX property.
`const https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation.ViewProperty!` `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation#ROTATION_Y()`

View's rotationY property.
`const https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation.ViewProperty!` `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation#SCALE_X()`

View's scaleX property.
`const https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation.ViewProperty!` `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation#SCALE_Y()`

View's scaleY property.
`const https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation.ViewProperty!` `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation#SCROLL_X()`

View's scrollX property.
`const https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation.ViewProperty!` `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation#SCROLL_Y()`

View's scrollY property.
`const https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation.ViewProperty!` `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation#TRANSLATION_X()`

View's translationX property.
`const https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation.ViewProperty!` `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation#TRANSLATION_Y()`

View's translationY property.
`const https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation.ViewProperty!` `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation#TRANSLATION_Z()`

View's translationZ property.
`const https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation.ViewProperty!` `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation#X()`

View's x property.
`const https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation.ViewProperty!` `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation#Y()`

View's y property.
`const https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation.ViewProperty!` `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation#Z()`

View's z property.

### Inherited functions

From [androidx.dynamicanimation.animation.DynamicAnimation](https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation) `T!` `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation#addEndListener(androidx.dynamicanimation.animation.DynamicAnimation.OnAnimationEndListener)(listener: https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation.OnAnimationEndListener!)`

Adds an end listener to the animation for receiving onAnimationEnd callbacks.
`T!` `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation#addUpdateListener(androidx.dynamicanimation.animation.DynamicAnimation.OnAnimationUpdateListener)(listener: https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation.OnAnimationUpdateListener!)`

Adds an update listener to the animation for receiving per-frame animation update callbacks.
`https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread
https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation#cancel()()`

Cancels the on-going animation.
`https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/AnimationHandler` `@https://developer.android.com/reference/kotlin/androidx/annotation/VisibleForTesting
https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation#getAnimationHandler()()`

Returns the `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/AnimationHandler` used to schedule updates for this animator.
`https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation#getMinimumVisibleChange()()`

Returns the minimum change in the animation property that could be visibly different to users.
`https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/FrameCallbackScheduler` `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation#getScheduler()()`

Returns the `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/FrameCallbackScheduler` used to schedule updates for this animator.
`https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation#isRunning()()`

Returns whether the animation is currently running.
`https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation#removeEndListener(androidx.dynamicanimation.animation.DynamicAnimation.OnAnimationEndListener)(listener: https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation.OnAnimationEndListener!)`

Removes the end listener from the animation, so as to stop receiving animation end callbacks.
`https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation#removeUpdateListener(androidx.dynamicanimation.animation.DynamicAnimation.OnAnimationUpdateListener)(
listener: https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation.OnAnimationUpdateListener!
)`

Removes the update listener from the animation, so as to stop receiving animation update callbacks.
`T!` `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation#setMinimumVisibleChange(float)(
minimumVisibleChange: @https://developer.android.com/reference/kotlin/androidx/annotation/FloatRange(from = 0.0, fromInclusive = false) https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html
)`

This method sets the minimal change of animation value that is visible to users, which helps determine a reasonable threshold for the animation's termination condition.
`https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation#setScheduler(androidx.dynamicanimation.animation.FrameCallbackScheduler)(scheduler: https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/FrameCallbackScheduler)`

Sets the frame scheduler used to schedule updates for this animator.
`T!` `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation#setStartValue(float)(startValue: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)`

Sets the start value of the animation.
`https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread
https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/DynamicAnimation#start()()`

Starts an animation.

## Public constructors

### FlingAnimation

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

```
FlingAnimation(floatValueHolder: FloatValueHolder!)
```

This creates a FlingAnimation that animates a `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/FloatValueHolder` instance. During the animation, the `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/FloatValueHolder` instance will be updated via `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/FloatValueHolder#setValue(float)` each frame. The caller can obtain the up-to-date animation value via `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/FloatValueHolder#getValue()`.

**Note:** changing the value in the `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/FloatValueHolder` via `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/FloatValueHolder#setValue(float)` outside of the animation during an animation run will not have any effect on the on-going animation.

| Parameters |
|---|---|
| `floatValueHolder: https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/FloatValueHolder!` | the property to be animated |

### FlingAnimation

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

```
<K> FlingAnimation(object: K!, property: FloatPropertyCompat<K!>!)
```

This creates a FlingAnimation that animates the property of the given object.

| Parameters |
|---|---|
| `<K>` | the class on which the property is declared |
| `object: K!` | the Object whose property will be animated |
| `property: https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/FloatPropertyCompat<K!>!` | the property to be animated |

## Public functions

### getFriction

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

```
fun getFriction(): Float
```

Returns the friction being set on the animation via `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/FlingAnimation#setFriction(float)`. If the friction has not been set, the default friction of 1 will be returned.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` | friction being used in the animation |

### setFriction

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

```
fun setFriction(friction: @FloatRange(from = 0.0, fromInclusive = false) Float): FlingAnimation!
```

Sets the friction for the fling animation. The greater the friction is, the sooner the animation will slow down. When not set, the friction defaults to 1.

| Parameters |
|---|---|
| `friction: @https://developer.android.com/reference/kotlin/androidx/annotation/FloatRange(from = 0.0, fromInclusive = false) https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` | the friction used in the animation |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/FlingAnimation!` | the animation whose friction will be scaled |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException.html` | if the input friction is not positive |

### setMaxValue

```
fun setMaxValue(maxValue: Float): FlingAnimation!
```

Sets the max value of the animation. When a fling animation reaches the max value, the animation will end immediately. Animations will not animate beyond the max value.

| Parameters |
|---|---|
| `maxValue: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` | maximum value of the property to be animated |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/FlingAnimation!` | the Animation whose max value is being set |

### setMinValue

```
fun setMinValue(minValue: Float): FlingAnimation!
```

Sets the min value of the animation. When a fling animation reaches the min value, the animation will end immediately. Animations will not animate beyond the min value.

| Parameters |
|---|---|
| `minValue: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` | minimum value of the property to be animated |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/FlingAnimation!` | the Animation whose min value is being set |

### setStartVelocity

```
fun setStartVelocity(startVelocity: Float): FlingAnimation!
```

Start velocity of the animation. Default velocity is 0. Unit: pixel/second

A **non-zero** start velocity is required for a FlingAnimation. If no start velocity is set through `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/FlingAnimation#setStartVelocity(float)`, the start velocity defaults to 0. In that case, the fling animation will consider itself done in the next frame.

Note when using a fixed value as the start velocity (as opposed to getting the velocity through touch events), it is recommended to define such a value in dp/second and convert it to pixel/second based on the density of the screen to achieve a consistent look across different screens.

To convert from dp/second to pixel/second:

```kotlin
float pixelPerSecond = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpPerSecond,
        getResources().getDisplayMetrics());
```

| Parameters |
|---|---|
| `startVelocity: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` | start velocity of the animation in pixel/second |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/dynamicanimation/animation/FlingAnimation!` | the Animation whose start velocity is being set |