Android Animations

Android property animations

“Motion provides meaning. Objects are presented to the user without breaking the continuity of experience even as they transform and reorganize. Motion in the world of material design is used to describe spatial relationships, functionality, and intention with beauty and fluidity.”

-Material Design guidelines


It is widely known that animations and transitions are among the most important features in your Android application. Not do animations provide smooth user experience, but also they allow the app to gracefully handle data manipulation.

For instance you can set your transition animation to perform every time your data is being loaded from the internet. Among the other things, animations can “teach” the user how to use the application. Fluid transitions and animations can even save you work from making tutorials which can be cumbersome and overwhelming for a user.

Basic example


In the following tutorial we will just barely scratch the surface of the possibilities we can do with Android animations. We will begin with “Property animations”.

As its name says, it can change Java properties of your Object. For instance you can change width, height, colour, alpha and other values. What is more we can set animation duration, delay on start, Interpolators, repeat count or we can even coordinate the animations to play at the same time, or sequentially. Good news is that it is compatible with API 11 and above. Here are some examples:

                                ObjectAnimator scaleX = ObjectAnimator.ofFloat(btnFab, "scaleX", 0, 1);
                                ObjectAnimator scaleY = ObjectAnimator.ofFloat(btnFab, "scaleY", 0, 1);

                                AnimatorSet fabAnimatorSet = new AnimatorSet();
                                fabAnimatorSet.playTogether(scaleX, scaleY);
                                fabAnimatorSet.start();

Lets break the code apart:



ObjectAnimator class is used for manipulating animating objects. For example .ofFloat() function is used to change float properties of the provided object, with its starting and ending values. Note that property is indicated with String value. Even more, we can provide duration and delay of an animation. AnimatorSet class is used for choreographing ObjectAnimator objects. This way we can set the animation to play together, sequentially etc.

What is more, we can add Interpolator that defines rate of change of an animation.

At the end we use .start() function to start the animationSet.

As you can see above, code can get a little bit messy, especially if there are lot of views to animate. Animations can also be implemented via xml, but for this tutorial we used the plain old Java way.

To conclude:

PROS

  • Widely supported (API 11 and above).
  • Fairly easy to implement basic property features.

CONS

  • Hard to handle complex animations and transitions.
  • Code can get a bit bulky

In the next tutorial we will see how transitions and shared element activity transitions work on API > 21 devices.

For reference you can check sample project at https://github.com/marijansikicclover/Android-Animations

And as always, don’t take my word for it, check the official documentation at https://developer.android.com/training/animation/index.html .

Happy coding :)