Basic animations

You can't learn how to run if you don't start walking first.

The Animation class

android.view.animation

The Animation class has 5 known subclasses

  • AlphaAnimation (will fade in or out your view)

  • RotateAnimation (will rotate your view)

  • ScaleAnimation (will scale up or down your view)

  • TranslateAnimation (will move your view)

  • AnimationSet (for a combination of animations that should be played together on the same view)

When to use

When you want to animate one property of a view

When NOT to use

  1. When you want to animate more than one properties or

  2. you want to animate a clickable view

Property animation API

android.animation

It makes it easy to animate any kind of property on any object by allowing you to define a start and end value and apply a time-based change on this attribute.

The Animator class is the superclass for classes which provide basic support for animations which can be started, ended, and have AnimatorListeners attached to them. These classes are:

  • Animator set

  • ValueAnimator

  • ObjectAnimator

  • TimeAnimator

ValueAnimator: provides a simple timing engine for running animations which calculate animated values. It runs in a custom handler to ensure that property changes happen on the UI thread.

ObjectAnimator: sets the animated values to the target objects.

TimeAnimator: provides a simple callback mechanism to listeners that is synchronized with all other animators in the system

AnimatorSet: This class plays a set of Animator objects in the specified order more or less as the AnimationSet class we saw earlier. That said, the animator set is more flexible and animations can be set up to play together, in sequence, or after a specified delay. In addition you can set it to play different animations on different objects and not just on the same one.

When animating properties there are 2 steps: calculating the animated values and setting those values on the object and property in question. ValueAnimatortakes care of the first part; calculating the values, while the ObjectAnimatorclass, is responsible for setting those values on target objects.

When NOT to use

  1. I found only one case of a simple animation that this did not work, when I wanted to animate only one layer of a LayerDrawable*

*If you want to find more information check this article.

Creating an animation

All animations can be either from XML or programmatically.

Example ObjectAnimator:

...
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(btn, View.ROTATION, 360);
objectAnimator.setDuration(5000);
objectAnimator.setRepeatCount(1);
objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
objectAnimator.start();
...

ViewPropertyAnimator.class

Property animation API provides also the ViewPropertyAnimator class that is a simple way to animate several properties in parallel, using a single Animator.

ViewPropertyAnimator propertyAnimator = 
btn.animate().rotation(360).setDuration(5000);
propertyAnimator.alpha(0f).setDuration(5000);

The good: it provides better performance for several simultaneous animations, because it will optimize invalidate calls to take place only once for several properties instead of each animated property independently causing its own invalidation. It also has a much simpler syntax!

The bad: it’s only for animating properties in parallel!

Last updated