CHAPTER 10: Android Animation: Making Your UI Designs Move 357
It is important to make sure to “trim” any unutilized pixels within your animation, so that the
animated elements come as close (one pixel away) to touching the edges of your image container
as possible. You will see that I’ve done this in all the animation frame assets we will be using in this
chapter, so you’ll be able to see what I mean.
Similar to what you learned about digital imagery in Chapter 9, Android will automatically handle the
decisions regarding which of the 2D frame animation pixel densities to implement for each device
screen which the OS is running on. Our largest 480 by 480 pixel resolution frame animation asset is
for the XHDPI resolution density, and I will create a 240 by 240 pixel version for HDPI, as well as a
120 by 120 pixel version to use for MDPI.
The reason that I’m not creating an XXHDPI resolution version on the highend (4K TV) is because the
XHDPI animation frames can be scaled up if needed for those devices, which represent 2% of the
device market, and on the lowend (240 pixel flip-phone or smartwatch), the MDPI animation frames
can be scaled down if needed for those devices, which also represent about a 2% market share
amongst all the current Android hardware devices.
The Android AnimationDrawable Class
The Android AnimationDrawable class is used to create a frame animation type of Drawable object
in Android, which holds a list of drawable assets which define the frames of the animation, as well as
playback parameters.
Android’s AnimationDrawable class is part of the android.graphics package, as you might imagine,
and is kept with all of the other types of Drawable objects in Android, using the android.graphics.
drawable sub-package.
The class hierarchy starts with the java.lang.Object master class, which is subclassed to create the
Drawable class, which is subclassed to create a DrawableContainer class, which is subclassed
to create an AnimationDrawable class. The Java class hierarchy for the AnimationDrawable class
would thus look like the following:
java.lang.Object
android.graphics.drawable.Drawable
android.graphics.drawable.DrawableContainer
android.graphics.drawable.AnimationDrawable
The reason that this DrawableContainer class is between the Drawable and AnimationDrawable
is because it was logical to create the DrawableContainer class for what you might consider
“multi-drawables,” or drawables with more than one state, level, frame, or other such drawable
asset element. Examples of these ContainerDrawable subclasses include the StateListDrawable,
used to create the multi-state ImageButton, the LevelListDrawable used for level indicators, such
as the signal level meter on your smartphone, and the AnimationDrawable.
The simplest way to create one of these frame animation drawable assets is to define the animation
frames using an XML file, which will be stored in the HelloUniverse project’s /res/drawable folder.
We will be creating three spaceship animations during this chapter, using the AnimationDrawable and
Animation classes, after we discuss how the AnimationDrawable and Animation classes function.
After we create the AnimationDrawable object(s), we’ll set them up with background images, using an
ImageView object. Later on in the chapter, when we write your Java code for the SlidingPaneLayout,
we’ll call the .start( ) method to start each AnimationDrawable object’s playback cycle.