Posted on

Android Animation Types Explained


Android Animation Types Explained
Designed by Freepik

Hi, as promised in the previous posts, we’re going to get into animation types. It might be overwhelming if you just use any animation type like ViewAnimation, ViewPropertyAnimator for alpha, scale etc. It works, because api’s are really simple to use, but you might still doubt about why there’s a duplicate (?) of achieving the same thing. In this post we’ll get deep into Animation Types and will understand the purpose of them.

 Animation Types

There’s three main types of animations available on Android: ViewAnimation, Property Animation, DrawableAnimation and much more. And of course since they basically achieve the same purpose, you might ask why so many? Let’s see in more details for each one and after that it hopefully will be clear for us.

ViewAnimation

This is the oldest type of animations. It was available since API 1. It has those 4 types of animations

  • Alpha
  • Scale
  • Rotate
  • Translate

And AnimationSet to wrap multiple animations. And you can call create them ether from Java or XML. First of all, let’s see how we use it with Java, because everything is translated to Java anyways.

Java

Default animation duration is zero, that’s why you have to set it always.

XML

First you create fade_out.xml file

and create animation from it in Java and run it

Now one more thing, it doesn’t matter where in res directory you put this fade_out.xml. It will be referenced from R.anim anyway. But it’s better to put in anim directory because compiler will help you with attributes suggestions.

Values

All animations have absolute values, besides Translation, it’s from the current position. Pivots have two types: absolute or percentage. By the way, after animating your view will reset to its pre animation values, if you want it to stay with the animated values – set

or analog method in Java

Property Animation

Now let’s get to more advanced stuff. This class was introduces in Android 3.0. That’s a much bigger framework which allows to animate anything, not just views. First of all, I want to talk about some differences: it has default duration of 300 ms, if you animate views they stay at the animated position and default interpolator is AccelerateDecelerate. The root class  is Animator.

Value Animator

The main child in here is ValueAnimator it simply animates primitives like ints, floats, but for most cases you would want to apply those animated values to a view or any other objects. That’s where you set AnimatorUpdateListener and set values to you objects on each callback

Java

XML

Create value_anim.xml in res/animator directory and inflate in from it

Object Animator

Rather then using ValueAnimator and setting update listener you can use ObjectAnimator, which handles it for you. It looks much cleaner with it.

Java

XML

Create this object_fade_out.xml and inflate it in Java

ViewPropertyAnimator

Finally we got to the one that most of you probably use most of all. It’s just like ObjectAnimator, but it performs better if you change multiple properties simultaneously by calling invalidate() after all properties are animated. You can animate by absolute value or starting from current one (+/-) using -By suffix. There’s only Java way

Drawable Animation

In addition to those above we have Drawable Animation. I know your hear exploding already. What, twelve’s way to do fade out views, you might say? No, don’t worry, this one actually differs from previous.  AnimationDrawable is more like 2D game development. Because it’s frame drawable animation. For example, we have PNG’s of running hero, for each frame we set its duration, put into list and set in to any view.

Here I have sonic.xml

Set this drawable as src of an ImageView and start animation in Java

That’s what I got

Android Animation Types Explained

Animated Vector Drawable

Guess what? How about one more way to fade out views? Sure we can do that!

vector_square.xml

fill_fade_out.xml

avd_square.xml

Set avd_square.xml as src of an ImageView and

This is an example of different approaches to accomplish the same trivial thing for the purpose of understanding the whole picture. If you want some cooler icon animations stuff, check this blog post.

More?

Finally, there’s even more. Transition Framework, Canvas Animations… Definitely, Android guys provided us plenty of ways to make cool animations. Each class makes our job so much easier. I won’t cover Activity Transitions in here, it makes some complex staff extremely simple to implement and it looks amazing. You can check this awesome blog post for more cool animations.

Conclusion

If there’s something that I want to bring from this post is than old View Animations API is really old and ugly compare to ViewPropertyAnimator  which you call by animate() on View. That ObjectAnimator is an improvement of ValueAnimator for objects and it’s very similar to ViewPropertyAnimator.

Alright, thanks for reading, check this repo for source code which looks more structured and will help you a lot. Don’t forget to subscribe and share with friends if you think they will benefit from it!