public final class FlingAnimation extends DynamicAnimation


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, or when the value of the animation has gone beyond the min or max value defined via setMinValue or setMaxValue. 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

FlingAnimation(FloatValueHolder floatValueHolder)

This creates a FlingAnimation that animates a FloatValueHolder instance.

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

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

Public methods

float

Returns the friction being set on the animation via setFriction.

FlingAnimation
setFriction(@FloatRange(from = 0.0, fromInclusive = false) 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 Constants

From androidx.dynamicanimation.animation.DynamicAnimation
static final DynamicAnimation.ViewProperty

View's alpha property.

static final float

The minimum visible change in alpha that can be visible to users.

static final float

The minimum visible change in pixels that can be visible to users.

static final float

The minimum visible change in degrees that can be visible to users.

static final float

The minimum visible change in scale that can be visible to users.

static final DynamicAnimation.ViewProperty

View's rotation property.

static final DynamicAnimation.ViewProperty

View's rotationX property.

static final DynamicAnimation.ViewProperty

View's rotationY property.

static final DynamicAnimation.ViewProperty

View's scaleX property.

static final DynamicAnimation.ViewProperty

View's scaleY property.

static final DynamicAnimation.ViewProperty

View's scrollX property.

static final DynamicAnimation.ViewProperty

View's scrollY property.

static final DynamicAnimation.ViewProperty

View's translationX property.

static final DynamicAnimation.ViewProperty

View's translationY property.

static final DynamicAnimation.ViewProperty

View's translationZ property.

static final DynamicAnimation.ViewProperty

View's x property.

static final DynamicAnimation.ViewProperty

View's y property.

static final DynamicAnimation.ViewProperty

View's z property.

Inherited methods

From androidx.dynamicanimation.animation.DynamicAnimation
T

Adds an end listener to the animation for receiving onAnimationEnd callbacks.

T

Adds an update listener to the animation for receiving per-frame animation update callbacks.

void

Cancels the on-going animation.

float

Returns the minimum change in the animation property that could be visibly different to users.

boolean

Returns whether the animation is currently running.

void

Removes the end listener from the animation, so as to stop receiving animation end callbacks.

void

Removes the update listener from the animation, so as to stop receiving animation update callbacks.

T
setMinimumVisibleChange(
    @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.

T
setStartValue(float startValue)

Sets the start value of the animation.

void

Starts an animation.

Public constructors

FlingAnimation

Added in 1.0.0
public FlingAnimation(FloatValueHolder floatValueHolder)

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

Note: changing the value in the FloatValueHolder via setValue 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 1.1.0-alpha04
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

FloatPropertyCompat<K> property

the property to be animated

Public methods

getFriction

Added in 1.0.0
public float getFriction()

Returns the friction being set on the animation via setFriction. 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
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
@FloatRange(from = 0.0, fromInclusive = false) float friction

the friction used in the animation

Returns
FlingAnimation

the animation whose friction will be scaled

Throws
java.lang.IllegalArgumentException

if the input friction is not positive

setMaxValue

Added in 1.1.0-alpha04
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
FlingAnimation

the Animation whose max value is being set

setMinValue

Added in 1.1.0-alpha04
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
FlingAnimation

the Animation whose min value is being set

setStartVelocity

Added in 1.1.0-alpha04
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 setStartVelocity, 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
FlingAnimation

the Animation whose start velocity is being set