SpringAnimation


public final class SpringAnimation extends DynamicAnimation


SpringAnimation is an animation that is driven by a SpringForce. The spring force defines the spring's stiffness, damping ratio, as well as the rest position. Once the SpringAnimation is started, on each frame the spring force will update the animation's value and velocity. The animation will continue to run until the spring force reaches equilibrium. If the spring used in the animation is undamped, the animation will never reach equilibrium. Instead, it will oscillate forever.

To create a simple SpringAnimation that uses the default SpringForce:

// Create an animation to animate view's X property, set the rest position of the
// default spring to 0, and start the animation with a starting velocity of 5000 (pixel/s).
final SpringAnimation anim = new SpringAnimation(view, DynamicAnimation.X, 0)
        .setStartVelocity(5000);
anim.start();

Alternatively, a SpringAnimation can take a pre-configured SpringForce, and use that to drive the animation.

// Create a low stiffness, low bounce spring at position 0.
SpringForce spring = new SpringForce(0)
        .setDampingRatio(SpringForce.DAMPING_RATIO_LOW_BOUNCY)
        .setStiffness(SpringForce.STIFFNESS_LOW);
// Create an animation to animate view's scaleY property, and start the animation using
// the spring above and a starting value of 0.5. Additionally, constrain the range of value for
// the animation to be non-negative, effectively preventing any spring overshoot.
final SpringAnimation anim = new SpringAnimation(view, DynamicAnimation.SCALE_Y)
        .setMinValue(0).setSpring(spring).setStartValue(1);
anim.start();

Summary

Public constructors

This creates a SpringAnimation that animates a FloatValueHolder instance.

SpringAnimation(FloatValueHolder floatValueHolder, float finalPosition)

This creates a SpringAnimation that animates a FloatValueHolder instance.

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

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

<K> SpringAnimation(
    K object,
    FloatPropertyCompat<K> property,
    float finalPosition
)

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

Public methods

void
animateToFinalPosition(float finalPosition)

Updates the final position of the spring.

boolean

Queries whether the spring can eventually come to the rest position.

void

Cancels the on-going animation.

SpringForce

Returns the spring that the animation uses for animations.

SpringAnimation

Uses the given spring as the force that drives this animation.

void

Skips to the end of the animation.

void

Starts an animation.

Extension functions

final @NonNull SpringAnimation

Updates or applies spring force properties like SpringForce.mDampingRatio, SpringForce.mFinalPosition and stiffness on SpringAnimation.

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.

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
setMaxValue(float max)

Sets the max value of the animation.

T
setMinValue(float min)

Sets the min value of the animation.

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.

T
setStartVelocity(float startVelocity)

Start velocity of the animation.

Public constructors

SpringAnimation

Added in 1.0.0
public SpringAnimation(FloatValueHolder floatValueHolder)

This creates a SpringAnimation 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

SpringAnimation

Added in 1.1.0-alpha04
public SpringAnimation(FloatValueHolder floatValueHolder, float finalPosition)

This creates a SpringAnimation 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. A Spring will be created with the given final position and default stiffness and damping ratio. This spring can be accessed and reconfigured through setSpring.

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

float finalPosition

the final position of the spring to be created.

SpringAnimation

Added in 1.1.0-alpha04
public <K> SpringAnimation(K object, FloatPropertyCompat<K> property)

This creates a SpringAnimation that animates the property of the given object. Note, a spring will need to setup through setSpring before the animation starts.

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

SpringAnimation

Added in 1.1.0-alpha04
public <K> SpringAnimation(
    K object,
    FloatPropertyCompat<K> property,
    float finalPosition
)

This creates a SpringAnimation that animates the property of the given object. A Spring will be created with the given final position and default stiffness and damping ratio. This spring can be accessed and reconfigured through setSpring.

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

float finalPosition

the final position of the spring to be created.

Public methods

animateToFinalPosition

Added in 1.0.0
public void animateToFinalPosition(float finalPosition)

Updates the final position of the spring.

When the animation is running, calling this method would assume the position change of the spring as a continuous movement since last frame, which yields more accurate results than changing the spring position directly through setFinalPosition. If the animation hasn't started, calling this method will change the spring position, and immediately start the animation.
Parameters
float finalPosition

rest position of the spring

canSkipToEnd

Added in 1.0.0
public boolean canSkipToEnd()

Queries whether the spring can eventually come to the rest position.

Returns
boolean

true if the spring is damped, otherwise false

cancel

@MainThread
public void cancel()

Cancels the on-going animation. If the animation hasn't started, no op. Note that this method should only be called on main thread.

Throws
android.util.AndroidRuntimeException

if this method is not called on the main thread

getSpring

Added in 1.0.0
public SpringForce getSpring()

Returns the spring that the animation uses for animations.

Returns
SpringForce

the spring that the animation uses for animations

setSpring

Added in 1.0.0
public SpringAnimation setSpring(SpringForce force)

Uses the given spring as the force that drives this animation. If this spring force has its parameters re-configured during the animation, the new configuration will be reflected in the animation immediately.

Parameters
SpringForce force

a pre-defined spring force that drives the animation

Returns
SpringAnimation

the animation that the spring force is set on

skipToEnd

Added in 1.0.0
public void skipToEnd()

Skips to the end of the animation. If the spring is undamped, an IllegalStateException will be thrown, as the animation would never reach to an end. It is recommended to check canSkipToEnd before calling this method. This method should only be called on main thread. If animation is not running, no-op.

Throws
java.lang.IllegalStateException

if the spring is undamped (i.e. damping ratio = 0)

android.util.AndroidRuntimeException

if this method is not called on the main thread

start

@MainThread
public void start()

Starts an animation. If the animation has already been started, no op. Note that calling start will not immediately set the property value to start value of the animation. The property values will be changed at each animation pulse, which happens before the draw pass. As a result, the changes will be reflected in the next frame, the same as if the values were set immediately. This method should only be called on main thread.

Throws
android.util.AndroidRuntimeException

if this method is not called on the main thread

Extension functions

DynamicAnimationKt.withSpringForceProperties

public final @NonNull SpringAnimation DynamicAnimationKt.withSpringForceProperties(
    @NonNull SpringAnimation receiver,
    @ExtensionFunctionType @NonNull Function1<@NonNull SpringForceUnit> func
)

Updates or applies spring force properties like SpringForce.mDampingRatio, SpringForce.mFinalPosition and stiffness on SpringAnimation.

If SpringAnimation.mSpring is null in case SpringAnimation is created without final position it will be created and attached to SpringAnimation

Parameters
@ExtensionFunctionType @NonNull Function1<@NonNull SpringForceUnit> func

lambda with receiver on SpringForce