Introduction to the
A SpringAnimation is similar to a FlingAnimation. The main difference is that a FlingAnimation is controlled by the speed and friction provided by a fling. However, SpringAnimation realizes the SpringAnimation effect according to three factors: spring stiffness, damping and target end point. Therefore, spring animation can be realized by setting the three parameters of Stiffness, DampingRatio and target end FinalPosition, as well as Velocity, an unnecessary factor. Since some of the basic uses of SpringAnimation are similar to those of FlingAnimation, you should refer to the article “Using FlingAnimation” for basic use, which is only explained where it is important. Here’s what it looks like.
Corresponding project address: gitee.com/guaishoun/s…
Add the library
def dynamicanimation_version = "1.0.0"
implementation "androidx.dynamicanimation:dynamicanimation:$dynamicanimation_version"
Copy the code
Use SpringForce to set the spring effect
In animations based on spring characteristics, the SpringForce class allows you to customize the stiffness, damping ratio, and final position of the spring, as well as add a speed. Once the animation starts, the spring will update the animation value and speed for each frame. The animation will continue until the spring reaches equilibrium.
More details can see the AndroidDeveloper official website spring animation.
Damping ratio is used to describe the state of gradual attenuation of spring vibration.
- Overdamping occurs when the damping ratio is greater than 1. It quickly returns the object to its static position.
- Critical damping occurs when the damping ratio is equal to 1. This returns the object to its resting position in the shortest possible time.
- Underdamping occurs when the damping ratio is less than 1. This causes the object to pass and pass over the static position several times, and then gradually reach the static position.
- When the damping ratio is zero, there is no damping. This causes the object to vibrate forever.
Common damping is
-
DAMPING_RATIO_HIGH_BOUNCY
-
DAMPING_RATIO_MEDIUM_BOUNCY
-
DAMPING_RATIO_LOW_BOUNCY
-
DAMPING_RATIO_NO_BOUNCY
Stiffness defines the spring constant used to measure the strength of a spring. The common stiffness is
STIFFNESS_HIGH
STIFFNESS_MEDIUM
STIFFNESS_LOW
STIFFNESS_VERY_LOW
When setting a spring property for an animation, remember that the spring property SpringForce is a property of the SpringAnimation, so you can use either new or the default get
final View img = findViewById(R.id.imageView);
final SpringAnimation anim = new SpringAnimation(img, DynamicAnimation.TRANSLATION_Y);
//new and set
SpringForce force = new SpringForce();
force.setDampingRatio(SpringForce.DAMPING_RATIO_LOW_BOUNCY);
force.setStiffness(SpringForce.STIFFNESS_LOW);
anim.setSpring(force);
// getDefault and set
…
//Setting the damping ratio to create a low bouncing effect.
anim.getSpring().setDampingRatio(SpringForce.DAMPING_RATIO_LOW_BOUNCY);
//Setting the spring with a low stiffness.
anim.getSpring().setStiffness(SpringForce.STIFFNESS_LOW);
Copy the code
The final location is also a property of SpringForce, but can be set using SpringAnimation
new SpringForce(finalPosition)
Copy the code
// The constructor
public <K> SpringAnimation(K object, FloatPropertyCompat<K> property,
float finalPosition)
Copy the code
springAnimation.animateToFinalPosition(float finalPosition)
Copy the code
Speed Setting speed is to add initial speed to the spring animation after swinging, so that the animation is more in line with the actual inertial motion effect.
springAnimation.setStartVelocity(velocityX);
Copy the code
conclusion
The use of SpringAnimation should be closely related to the damping ratio, stiffness and final position, while the speed is optional. Not much different from FlingAnimation. But SpringAnimation can make the interface more textured.