Understanding Spring Animation in Android

Ashish Kumar
2 min readApr 28, 2020

--

This is Part III of Learning Android Series (a.k.a project “The Circle”)

  1. Part I
  2. Part II
  3. Part III (You are here)
  4. Coming Soon…

Did you ever thought that your animation are not natural looking, they lack the real physics motion, they don’t change Dynamically or transition between two animation is not smooth. If above problems bothers you, then you are at the right place today we will learn about Spring Animation which is part of Android Dynamic Animation Library.

Problem with traditional approach

If you have used ObjectAnimator or ValueAnimator you know that there is no continuity between two successive animation. So if state changes, animation has to be cancelled in the middle and new animation has to be played, so the transition is very rough. It is because of the fact that these ValueAnimator is nothing but timing engine.It just calculates the animated values and set them on the target objects. But for smoother animation we need something which can track certain factors like velocity, momentum etc. between two animations. Which means we need a Physics Engine.

Using ObjectAnimator

Dynamic Animation to the rescue

Android Dynamic Animation library is based a physics based, which means it can easily transition between two animations without abrupt changes in velocity or momentum.

Dynamic Animation library includes two types of animation

  1. Spring Animation
  2. Fling Animation

We will focus on Spring Animation this time, To start using Dynamic animation we first have to use the Dynamic Animation library. Just put the following line in app level build.gradle

implementation "androidx.dynamicanimation:dynamicanimation:1.0.0"

Solving the problem

As now we have imported dynamic animation library, we can use the spring animation to have a smooth animation.

  1. Create two SpringAnimation one for x-axis and one for y-axis
  2. Set a new SpringForce() for each animation
  3. Cancel the already playing animation
  4. Set the final position using spring.finalPostion
  5. Play the new Animation

Spring animation takes SpringForce which can help in adjusting the spring properties by setting DamingRatio , Stiffness etc.

All done now our animation looks like this

Using SpringAnimation

Congratulations now you’ve learned how to add life to animation with the help of Dynamic Animation.

Stay Cool 😎

--

--

Ashish Kumar

Computer Science student | Android Developer | Full Stack Developer