SpringAnimation

public final class SpringAnimation
extends DynamicAnimation<SpringAnimation>

java.lang.Object
   ↳ androidx.dynamicanimation.animation.DynamicAnimation<androidx.dynamicanimation.animation.SpringAnimation>
     ↳ androidx.dynamicanimation.animation.SpringAnimation


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.

Developer Guides

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

Inherited constants

Inherited fields

Public constructors

SpringAnimation(FloatValueHolder floatValueHolder)

This creates a SpringAnimation that animates a FloatValueHolder instance.

SpringAnimation(FloatValueHolder floatValueHolder, float finalPosition)

This creates a SpringAnimation that animates a FloatValueHolder instance.

SpringAnimation(K object, FloatPropertyCompat<K> property)

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

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 canSkipToEnd()

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

void cancel()

Cancels the on-going animation.

SpringForce getSpring()

Returns the spring that the animation uses for animations.

SpringAnimation setSpring(SpringForce force)

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

void skipToEnd()

Skips to the end of the animation.

void