class FlingAnimation : 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(object: K!, property: FloatPropertyCompat<K!>!)

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

Public functions

Float

Returns the friction being set on the animation via setFriction.

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

Sets the friction for the fling animation.

FlingAnimation!
setMaxValue(maxValue: Float)

Sets the max value of the animation.

FlingAnimation!
setMinValue(minValue: Float)

Sets the min value of the animation.

FlingAnimation!
setStartVelocity(startVelocity: Float)

Start velocity of the animation.

Inherited Constants

From androidx.dynamicanimation.animation.DynamicAnimation
const DynamicAnimation.ViewProperty!

View's alpha property.

const Float

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

const Float

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

const Float

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

const Float

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

const DynamicAnimation.ViewProperty!

View's rotation property.

const DynamicAnimation.ViewProperty!

View's rotationX property.

const DynamicAnimation.ViewProperty!

View's rotationY property.

const DynamicAnimation.ViewProperty!

View's scaleX property.

const DynamicAnimation.ViewProperty!

View's scaleY property.

const DynamicAnimation.ViewProperty!

View's scrollX property.

const DynamicAnimation.ViewProperty!

View's scrollY property.

const DynamicAnimation.ViewProperty!

View's translationX property.

const DynamicAnimation.ViewProperty!

View's translationY property.

const DynamicAnimation.ViewProperty!

View's translationZ property.

const DynamicAnimation.ViewProperty!

View's x property.

const DynamicAnimation.ViewProperty!

View's y property.

const DynamicAnimation.ViewProperty!

View's z property.

Inherited functions

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.

Unit

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.

Unit

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

Unit

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

T!
setMinimumVisibleChange(
    minimumVisibleChange: @FloatRange(from = 0.0, fromInclusive = false) Float
)

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(startValue: Float)

Sets the start value of the animation.

Unit

Starts an animation.

Public constructors

FlingAnimation

Added in 1.0.0
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
<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: FloatPropertyCompat<K!>!

the property to be animated

Public functions

getFriction

Added in 1.0.0
fun getFriction(): Float

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
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: @FloatRange(from = 0.0, fromInclusive = false) Float

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

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

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
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 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
startVelocity: Float

start velocity of the animation in pixel/second

Returns
FlingAnimation!

the Animation whose start velocity is being set