CHAPTER 10: Android Animation: Making Your UI Designs Move 371
Procedural Loops: RepeatCount and RepeatMode
Like frame animation, procedural animation can play once, and then stop, or can play continuously
in a loop. There are two parameters that control looping, one which controls whether an animation
will loop or not and another which controls the way or “direction” in which the animation will loop
back and forth. A procedural animation parameter that controls the number of times an animation, or
component part of an animation set, will loop is called the repeatCount parameter. This parameter
will require a whole number (integer) data value.
If you leave this repeatCount parameter out of (that is, unspecified in) the procedural animation
definition, then an animation will play once and then stop. This means that the default setting is
android:repeatCount=“1” for this parameter. The exception to using an integer value for this
parameter is the infinite constant. If you want to have an animation loop forever, you would want to
use an android:repeatCount=“infinite” parameter setting.
In case you’re wondering, the value that the constant “infinite” defines is -1, so an
android:repeatCount=“-1” parameter definition should work just as well. The parameter which
defines what style of looping is used is the repeatMode parameter, which you can set to one of
two predefined constants. The most common of these two is restart, which will cause an animation
to loop seamlessly (unless you’ve defined the startOffset parameter). In case you’re wondering, the
value that a “restart” constant defines is 1 , so android:repeatMode=“1” works too!
The other type or mode of animation looping is the reverse mode, which is also called
“pong animation,” as it causes the animation to reverse at the end of its range, and run backward,
until it reaches the beginning again, at which time it runs forward, like the original video game called
Pong. Back and forth, ad infinitum! In case you’re wondering, the value that a “reverse” constant
defines is 2 , so android:repeatMode=“2” will also work.
All of these parameters may seem simple on their own, but when combined into complicated
structures using an animation set, which we are going to take a look at later, these parameters can
quickly become very complicated in combination with each other to produce some very detailed
and impressive animation results. Don’t underestimate the power of these parameters when they are
put together by a savvy developer, in the right XML structure. By the end of this book, you will be
that savvy developer! Next, let’s take a look at the Android Animation class and its subclasses that
implement tween animation, and then implement procedural animation using XML.
The Animation Class: Android Tween Animation
The Android Animation class is used to create tween animations of View objects in Android. This is
done by the interpolation of data values within groups of predefined transform types. This creates
the “frames” of procedural animation, although Android OS will decide how many frames are needed
to create a smooth animation result.
Android’s Animation class is part of the android.view package, and is kept with all the other Animation
classes, subclasses, and methods, using the android.view.animation package. This is quite different
from the bitmap or frame animation, which uses AnimationDrawable objects and is kept in the android.
graphics.drawable package. The Animation class hierarchy starts with the java.lang.Object master
object, which is then subclassed to create the Animation class. The Java class hierarchy for the
Animation class would thus look like the following:
java.lang.Object
android.view.animation.Animation