A (175)

(Tuis.) #1

358 CHAPTER 10: Android Animation: Making Your UI Designs Move


The XML animation definition construct which you are going to be specifying later in the chapter
gets inflated by your Java code, and becomes an AnimationDrawable object. This object contains
all information regarding your frame animation asset, including each actual animation frame image
asset reference, as well as each frame’s playback (duration) setting.


After the AnimationDrawable object has been instantiated and inflated, you can trigger it by using
a .start( ) method from within your application Java code. This is usually done from the inside of
your event handler, such as the one that we will be adding to the ImageButton UI elements later
in the chapter. You would use an event handler if you wanted the frame animation to be triggered
(started) interactively. You can also call the .start( ) method from the inside of your Activity subclass’s
on Create( ) method, if the animation is intended to simply run on your Activity startup screen
somewhere once the Activity is created (started).


If you want to research more detailed information regarding the Android AnimationDrawable class,
you can find more technical details on the Android Developer website at the following URL:


http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html


Now that we have gone over the AnimationDrawable class basics, let’s take a look at how to use
XML markup to create your frame animation definition file(s), which will live in the Android application
project’s /res/drawable folder.


Creating Frame Animation Using XML Markup


The way that frame animation is defined in Android is by using an XML definition file containing
markup that defines an animation list filled with frame items. This XML file should be stored in
the /res/drawable folder, which you created in the previous chapter, and which holds Android
Drawable asset XML definitions, such as multi-state (StateListDrawable) ImageButton elements,
NinePatchDrawable asymmetrically scalable digital image assets, and AnimationDrawable frame
animation assets.


In case you are wondering why this XML file is kept in your /res/drawable folder, and not in a
/res/anim folder, this is because there are two types of animation in Android. Frame animation uses
the /drawable resource folder hierarchy to hold the AnimationDrawable XML definitions (i.e. the /res/
drawable folder) and their referenced digital imagery assets (in the /res/drawable-dpi sub-folders).


Procedural Animation object(s), which we will be covering later on in this chapter, use the /res/anim
project folder, along with an Animation class constructor method to create Animation objects.


The frame animation XML file will specify the individual frames in your AnimationDrawable object
definition (essentially, this XML file is a 2D AnimationDrawable object constructor) by using
the parent XML tag. This frame animation XML construct essentially creates an
AnimationDrawable objectcontaining references to numbered frames (your image files). The image
file references represent the individual frames in your raster animation. A procedural (or vector)
animation XML file, on the other hand, won’t specify any frames, but will instead specify algorithmic
or procedural transformations, that, when interpolated, will create an illusion of motion. We’ll cover
this type of animation a bit later on in this chapter.

Free download pdf