Previously I have described how to create animations for both native Android and React Native applications using OpenGL. This time I’d like to tell about different tools which could be useful for Android animations of any difficulty and their pros and cons. This article is not a tutorial or something similar; it is a small collection of my thoughts and recommendations about animating in Android.

Tools I don’t use

However, the first thing I’d like to start from is tools, which I don’t use, and why. Some of them I used in my opensource animations, but I don’t want to use them further. Of course, it’s just my subjective opinion, and it’s not a call for refactoring of your recent animation :)

ObjectAnimator

ObjectAnimator.ofFloat(image, "x", 0f, 300f).apply {

duration = 1000

start()

}

The code snippet above shows usage of the ObjectAnimator . In this case, the x property of my image changes through reflection, which is not a good idea while animating. The reflection mechanism is great, but it’s a bit overhead for simple animations. Of course, we can solve that by using the Property instead of hardcoding the property name.

ObjectAnimator.ofFloat(image, View.X, 0f, 300f).apply {

duration = 1000

start()

}

Now the property value changes using appropriate setter directly without reflection. So right now we can see the first small disadvantage of the ObjectAnimator — you need to create Property for every custom property you want to modify to avoid using reflection.

The other issue is that you need a new ObjectAnimator for every view you want to modify since it doesn’t support simultaneous changes of several objects.

Anyway, it worth to say that ObjectAnimator is widely used in AnimatedVectorDrawable to animate SVG due to its ability to animate a property of any type. In my opinion, in any other case, there are better solutions for animations.

Animation and its subclasses

image.startAnimation(TranslateAnimation(0f, 300f, 0f, 0f).apply {

duration = 1000

})

Animation is the abstract parent class of TranslateAnimation , RotateAnimation , AlphaAnimation , ScaleAnimation and AnimationSet .

They could be useful to animate single view property, in any other circumstances Animation subclasses require to setup several instances to play AnimationSet just like ObjectAnimator does. And another big disadvantage is that you can animate only basic properties like rotation, scale, alpha and position (e.g., not a background color) and these tools are restricted to View 's subclasses only. One more issue you face if you use Animation is that it animates a View 's pixel only, not a View itself, e.g., you apply TransitionAnimation to your object, but it stays clickable in the previous location, if not to specify different behavior.

For now, the only reasonable usage of the Animation is the transitions between activities or fragments.

ViewPropertyAnimator

ViewPropertyAnimator is created to substitute the ObjectAnimator , and it’s adapted to simultaneous modifications due to optimization of the invalidate() method calls, which doubtlessly is good news. It’s a great tool, to animate several properties of a view in parallel.

image.animate().apply {

duration = 1000

x(300f)

y(150f)

alpha(0.5f)

start()

}

It looks much better, isn’t it? But usually animation requires to animate several views at the same time and not only views but other objects, so it’s still not good enough.