Exploring new Android Animation Updates

Caren Chang
3 min readJun 10, 2017

Some exciting new animation capabilities were announced as part of Android O during Google IO /17. The best part is that developers only need to update their support library versions in order to take advantage of these new APIs. Below I’ve outlined the major updates along with a few examples of what they look like.

AnimatorSet

  • Three new functions are now available in the AnimatorSet API : reverse() , setCurrentPlayTime(), getCurrentPlayTime() ,
  • easily play animations in reverse without having the write the reverse animation code manually!
  • set the position of the animation set to a specific point in time

Physics Based Animation

Spring Animation

  • Currently, Android developers are only allowed to drive animations with fixed durations and changes in animation values. This makes it hard to modify an animation while it’s running without making it look janky. However, with the newly introduced Spring Animation APIs, it’s now easier to create more flexible animations that also appear smooth to users.
  • When you create a spring animation, you can define the damping, stiffness and final position properties.
  • The damping value defines how bouncy the animation will be. The lower the damping value, the more bouncy it will be. This is because the damping ratio defines how fast the oscillations decay from one bounce to the next
  • The stiffness value defines the strength of the spring. A stiff spring applies more force to the object, which means there will be less bounce effect
  • A simple example of a spring animation is shown below:
SpringAnimation anim =
new SpringAnimation(view, DynamicAnimation.TRANSLATION_Y, FINAL_Y_POSITION);
anim.getSpring().setDampingRatio(DAMPING_RATIO_HIGH_BOUNCY);
anim.getSpring().setStiffness(STIFFNESS_VERY_LOW);
anim.start();
  • You can even chain animations together by utilizing the onAnimationUpdate() or onAnimationEnd() functions:
// in this example, secondAnimation is triggered whenever 
// firstAnimation does something
firstAnimation.addUpdateListener(new DynamicAnimation.OnAnimationUpdateListener() {
@Override
public void onAnimationUpdate(DynamicAnimation dynamicAnimation, float value, float velocity) {
secondAnimation.animateToFinalPosition(value);
}
});

Fling Animation

  • The Fling Animation API allows fling gestures to feel more natural by allowing developers to control the start velocity and friction of the fling animation.
  • Starting velocity is the speed at which you want the animation to start with, so I usually think of it as how much of a ‘push’ you want to give the animation in the beginning. The default starting velocity is set to zero, so it has to be set to a value greater than 0 for anything to happen.
  • The friction value defines how fast velocity decreases. The greater the friction, the shorter the animation will be.
FlingAnimation flingAnimation = new FlingAnimation(viewToAnimate, DynamicAnimation.TRANSLATION_X); // moves the view on the x-axis// derive the pixelPerSecond with start velocity value
float pixelPerSecond = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
startVelocityValue,
getResources().getDisplayMetrics());
flingAnimation.setStartVelocity(pixelPerSecond)
.setFriction(frictionValue)
.start();

Sample code for the animations seen in GIFs above can all be found here:
https://github.com/calren/animations . And of course the most helpful resource is always the official Android documentation : https://developer.android.com/topic/libraries/support-library/preview/physics-based-animation.html#spring-animation

--

--