Implement a spring animation
Last updated
Last updated
We are going to apply the spring animation to our avatar view in the Detail activity.
First we need to set the stiffness and the damping ratio of our animation. To do so we are going to create a SpringForce
object.
In the SpringForce
object we are going to set the stiffness level to STIFFNESS_LOW and the damping ratio to DAMPING_RATIO_HIGH_BOUNCY. The final position should be 0f since we don't want our view to translate at the end of the animation, but instead to return to the position it was before the animation.
Once you're done, we will create 2 SpringAnimation
objects: one with DynamicAnimation.TRANSLATION_X
and the other one with DynamicAnimation.TRANSLATION_Y
. To both of them we need to apply the SpringForce
object we just created as their spring.
Finally, we need a onTouchListener for getting coordinates of our view and for applying our animations.