added in version 25.4.0
belongs to Maven artifact com.android.support:support-dynamic-animation:28.0.0-alpha1

FlingAnimation

public final class FlingAnimation
extends DynamicAnimation<FlingAnimation>

java.lang.Object
   ↳ android.support.animation.DynamicAnimation<android.support.animation.FlingAnimation>
     ↳ android.support.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 setMinimumVisibleChange(float), or when the value of the animation has gone beyond the min or max value defined via setMinValue(float) or 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

Inherited constants

From class android.support.animation.DynamicAnimation

Inherited fields

From class android.support.animation.DynamicAnimation

Public constructors

FlingAnimation(FloatValueHolder floatValueHolder)

This creates a FlingAnimation that animates a FloatValueHolder instance.

FlingAnimation(K object, FloatPropertyCompat<K> property)

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

Public methods

float getFriction()

Returns the friction being set on the animation via setFriction(float).

FlingAnimation setFriction(float friction)

Sets the friction for the fling animation.

FlingAnimation setMaxValue(float maxValue)

Sets the max value of the animation.

FlingAnimation setMinValue(float minValue)

Sets the min value of the animation.

FlingAnimation setStartVelocity(float startVelocity)

Start velocity of the animation.

Inherited methods

From class android.support.animation.DynamicAnimation
From class java.lang.Object

Public constructors

FlingAnimation

added in version 25.4.0
FlingAnimation (FloatValueHolder floatValueHolder)

This creates a FlingAnimation that animates a FloatValueHolder instance. During the animation, the FloatValueHolder instance will be updated via setValue(float) each frame. The caller can obtain the up-to-date animation value via getValue().

Note: changing the value in the FloatValueHolder via setValue(float) outside of the animation during an animation run will not have any effect on the on-going animation.

Parameters
floatValueHolder FloatValueHolder: the property to be animated

FlingAnimation

added in version 25.4.0
FlingAnimation (K object, 
                FloatPropertyCompat<K> property)

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

Parameters
object K: the Object whose property will be animated

property FloatPropertyCompat: the property to be animated

Public methods

getFriction

added in version 25.4.0
float getFriction ()

Returns the friction being set on the animation via 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 version 25.4.0
FlingAnimation setFriction (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
friction float: the friction used in the animation

Value is 0.0 or greater.

Returns
FlingAnimation the animation whose friction will be scaled

Throws
IllegalArgumentException if the input friction is not positive

setMaxValue

added in version 25.4.0
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
maxValue float: maximum value of the property to be animated

Returns
FlingAnimation the Animation whose max value is being set

setMinValue

added in version 25.4.0
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
minValue float: minimum value of the property to be animated

Returns
FlingAnimation the Animation whose min value is being set

setStartVelocity

added in version 25.4.0
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 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
startVelocity float: start velocity of the animation in pixel/second

Returns
FlingAnimation the Animation whose start velocity is being set