# 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)

*** ** * ** ***


```
public final class FlingAnimation extends DynamicAnimation<FlingAnimation>
```

<br />

|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object.html) |||
| ↳ | [androidx.dynamicanimation.animation.DynamicAnimation](https://developer.android.com/reference/androidx/dynamicanimation/animation/DynamicAnimation)\<[FlingAnimation](https://developer.android.com/reference/androidx/dynamicanimation/animation/FlingAnimation)\> ||
|   | ↳ | [androidx.dynamicanimation.animation.FlingAnimation](https://developer.android.com/reference/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/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/androidx/dynamicanimation/animation/DynamicAnimation#setMinValue(float)` or `https://developer.android.com/reference/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:

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

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

### Inherited Constants

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

View's alpha property.
`static final float` `https://developer.android.com/reference/androidx/dynamicanimation/animation/DynamicAnimation#MIN_VISIBLE_CHANGE_ALPHA() = 0.00390625f`

The minimum visible change in alpha that can be visible to users.
`static final float` `https://developer.android.com/reference/androidx/dynamicanimation/animation/DynamicAnimation#MIN_VISIBLE_CHANGE_PIXELS() = 1.0f`

The minimum visible change in pixels that can be visible to users.
`static final float` `https://developer.android.com/reference/androidx/dynamicanimation/animation/DynamicAnimation#MIN_VISIBLE_CHANGE_ROTATION_DEGREES() = 0.1f`

The minimum visible change in degrees that can be visible to users.
`static final float` `https://developer.android.com/reference/androidx/dynamicanimation/animation/DynamicAnimation#MIN_VISIBLE_CHANGE_SCALE() = 0.002f`

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

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

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

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

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

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

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

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

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

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

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

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

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

View's z property.

### Inherited methods

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

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

Adds an update listener to the animation for receiving per-frame animation update callbacks.
`void` `@https://developer.android.com/reference/androidx/annotation/MainThread
https://developer.android.com/reference/androidx/dynamicanimation/animation/DynamicAnimation#cancel()()`

Cancels the on-going animation.
`@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/dynamicanimation/animation/AnimationHandler` `@https://developer.android.com/reference/androidx/annotation/VisibleForTesting
https://developer.android.com/reference/androidx/dynamicanimation/animation/DynamicAnimation#getAnimationHandler()()`

Returns the `https://developer.android.com/reference/androidx/dynamicanimation/animation/AnimationHandler` used to schedule updates for this animator.
`float` `https://developer.android.com/reference/androidx/dynamicanimation/animation/DynamicAnimation#getMinimumVisibleChange()()`

Returns the minimum change in the animation property that could be visibly different to users.
`@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/dynamicanimation/animation/FrameCallbackScheduler` `https://developer.android.com/reference/androidx/dynamicanimation/animation/DynamicAnimation#getScheduler()()`

Returns the `https://developer.android.com/reference/androidx/dynamicanimation/animation/FrameCallbackScheduler` used to schedule updates for this animator.
`boolean` `https://developer.android.com/reference/androidx/dynamicanimation/animation/DynamicAnimation#isRunning()()`

Returns whether the animation is currently running.
`void` `https://developer.android.com/reference/androidx/dynamicanimation/animation/DynamicAnimation#removeEndListener(androidx.dynamicanimation.animation.DynamicAnimation.OnAnimationEndListener)(https://developer.android.com/reference/androidx/dynamicanimation/animation/DynamicAnimation.OnAnimationEndListener listener)`

Removes the end listener from the animation, so as to stop receiving animation end callbacks.
`void` `https://developer.android.com/reference/androidx/dynamicanimation/animation/DynamicAnimation#removeUpdateListener(androidx.dynamicanimation.animation.DynamicAnimation.OnAnimationUpdateListener)(
https://developer.android.com/reference/androidx/dynamicanimation/animation/DynamicAnimation.OnAnimationUpdateListener listener
)`

Removes the update listener from the animation, so as to stop receiving animation update callbacks.
`T` `https://developer.android.com/reference/androidx/dynamicanimation/animation/DynamicAnimation#setMinimumVisibleChange(float)(
@https://developer.android.com/reference/androidx/annotation/FloatRange(from = 0.0, fromInclusive = false) float minimumVisibleChange
)`

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.
`void` `https://developer.android.com/reference/androidx/dynamicanimation/animation/DynamicAnimation#setScheduler(androidx.dynamicanimation.animation.FrameCallbackScheduler)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/dynamicanimation/animation/FrameCallbackScheduler scheduler)`

Sets the frame scheduler used to schedule updates for this animator.
`T` `https://developer.android.com/reference/androidx/dynamicanimation/animation/DynamicAnimation#setStartValue(float)(float startValue)`

Sets the start value of the animation.
`void` `@https://developer.android.com/reference/androidx/annotation/MainThread
https://developer.android.com/reference/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)

```
public FlingAnimation(FloatValueHolder floatValueHolder)
```

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

**Note:** changing the value in the `https://developer.android.com/reference/androidx/dynamicanimation/animation/FloatValueHolder` via `https://developer.android.com/reference/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 |
|---|---|
| `https://developer.android.com/reference/androidx/dynamicanimation/animation/FloatValueHolder floatValueHolder` | the property to be animated |

### FlingAnimation

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

```
public <K> FlingAnimation(K object, FloatPropertyCompat<K> property)
```

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

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

## Public methods

### getFriction

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

```
public float getFriction()
```

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

| Returns |
|---|---|
| `float` | friction being used in the animation |

### setFriction

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

```
public FlingAnimation setFriction(@FloatRange(from = 0.0, fromInclusive = false) float friction)
```

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 |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/FloatRange(from = 0.0, fromInclusive = false) float friction` | the friction used in the animation |

| Returns |
|---|---|
| `https://developer.android.com/reference/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

```
public FlingAnimation setMaxValue(float maxValue)
```

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 |
|---|---|
| `float maxValue` | maximum value of the property to be animated |

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

### setMinValue

```
public FlingAnimation setMinValue(float minValue)
```

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 |
|---|---|
| `float minValue` | minimum value of the property to be animated |

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

### setStartVelocity

```
public FlingAnimation setStartVelocity(float startVelocity)
```

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/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:

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

| Parameters |
|---|---|
| `float startVelocity` | start velocity of the animation in pixel/second |

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