Wikipedia defines animation as the process of making the illusion of motion and change by means of the rapid display of a sequence of static images that minimally differ from each other.
Well that’s the general idea but based on this, a plethora of animations can be produced in Android through certain predefined frameworks. This post will delve into the geeky bits of android animations. I will be describing these frameworks in theory and some implementation as well, with sample code (code can be found here).
Android provides built-in API’s to animate the graphic elements or custom 2-D and 3-D graphic elements. The basic graphic elements include the built-in views in an android activity. 2-D & 3-D elements can be drawn through widgets like canvas and drawables or a framework such as OpenGL. I will be taking you through the animations with basic graphic elements and not drawing graphics with OpenGL as that is a whole another field.
Android provides three animation systems-
View animations are basic animations that alter a view’s properties to produce an animation effect. These are also called as tween animations. Four types of transformations are included which are position, size, rotation and transparency. One can define these animations in XML or through the view objects methods corresponding to the animation. For instance tweening alpha attribute of a view can be achieved through its XML implementation or through AlphaAnimation object as shown in the sample code.
View animations onClickListener (in ViewAnimations.java) :-
Based on the above method, this sample video will demonstrate the tween animation effects:-
Property animations system provides the framework to create more customizable animations as compared to view animations. Property animations work on the idea of changing a view’s property between certain specified values for a specified duration of time. It is also possible to define how the transition occurs from the start value to the end value i.e. linearly or non-linearly.
In addition to above points, property animations are much more robust as they animate the object unlike view animations that animate only the view. Take button view for instance, if view animation is applied to change the button’s position it will translate the view but onClick event will be triggered from its previous location only. Whereas property animations will handle the onClick event automatically as it applies the animation to the object. It is also possible to animate properties other than view attributes like the background color of view. Property animations can also be used to animate non-view objects. Property animations can also be defined through XML.
There are two different classes in property animations:-
ValueAnimator class extends the animator class and is the base class for property animations. It provides methods to create the most customizable animations. The setup for ValueAnimator is simple. You just have to specify the start and end values along with the duration of the animation. Then in the ValueAnimator.AnimatorUpdateListener you can fetch the current animation value by calling the method getAnimatedValue(). Within the listener you set whichever attribute you want to animate to the value returned by getAnimatedValue(). Finally call your views requestLayout() method to redraw the view with updated values and you can observe the animation.
Here’s some sample code for it(ValueAnimatorFragment.java):-
You can also set onAnimationEnd listener to make changes after the animation completes. Now you can observe a new method named setInterpolator(). It is used to set the transition behaviour for the animation. Here’s the code for it-
As names suggest the LinearInterpolator is responsible for linear transitions and is the default for any animation. AccelerateDecelerateInterpolator first accelerates then decelerates. CustomInterpolator extends the Interpolator interface to produce custom transitions.
This sample video will demonstrate ValueAnimator animations:-
ObjectAnimator extends the ValueAnimator class to allow a certain level of abstraction. Here you don’t have to update the values in the AnimationUpdateListener rather you just specify the name of the property you want to animate. Property names can be like “alpha”, “scaleX”, “left” (left margin that is) etc.
Here’s some sample code(ObjectAnimatorFragment.java)-
This video will demonstrate similar animations but with ObjectAnimator code:-
Set of animations :-
Multiple property animations can be executed simultaneously with AnimatorSet class. AnimatorSet provides built-in methods to play animations either sequentially or together. Also helper methods like before(), after() and with(). AnimatorSet class works on Animator object so they cannot be applied to view animations. AnimatorSet can be defined as an object or through XML.
For view animations we can define a set of animations through XML or through AnimationSet class. View animations in sets are played simultaneously by default. It is possible to specify starOffset for each animation to introduce sequential order.
Here’s some sample code for it:-
This video will demonstrate the effects of this XML:-
Above example shows a view animations set through XML. Here’s another example through AnimationSet object:-
Here’s some example code for property animations set(AnimatorSetFragment.java):-
The above shows a set of property animations through objects of both ObjectAnimator and ValueAnimator. It is also possible to create a set through XML.
Here is a video to demonstrate a set of animations:-
As can be observed, the video contains three animation effects. Translation of view is through ValueAnimator and alpha & scaling through ObjectAnimator.
View and property animations are the most fundamental frameworks in android. I will be discussing about some other android animations in the next post.