Adding Animation to the Activity

Now that you have the activity and layout in place, we'll show you how to add animation to this sample. In Android, you accomplish frame-by-frame animation through a class in the graphics package called AnimationDrawable. This class can take a list of Drawable resources (like images) and render them at specified intervals. This class is really a thin wrapper around the animation support provided by the basic Drawable class.

The Drawable class enables animation by asking its container or view to invoke a Runnable class that essentially redraws the Drawable using a different set of parameters. Note that you don't need to know these internal implementation details to use the AnimationDrawable class. But if your needs are more complex, you can look at the AnimationDrawable source code for guidance in writing your own animation protocols.

To make use of the AnimationDrawable class, start with a set of Drawable resources placed in the /res/drawable subdirectory. You will then construct an XML file that defines the list of frames (see Listing 6-3).

Listing 6-3. XML File Defining the List of Frames to be Animated

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">

<item

android

drawable=

"@drawable/colored-ball1"

android:

duration=

"50"

/>

<item

android

drawable=

"@drawable/colored-ball2"

android:

duration=

"50"

/>

<item

android

drawable=

"@drawable/colored-ball3"

android:

duration=

"50"

/>

<item

android

drawable=

"@drawable/colored-ball4"

android:

duration=

"50"

/>

<item

android

drawable=

"@drawable/colored-ball5"

android:

duration=

"50"

/>

<item

android

drawable=

"@drawable/colored-ball6"

android:

duration=

"50"

/>

<item

android

drawable=

"@drawable/colored-ball7"

android:

duration=

"50"

/>

<item

android

drawable=

"@drawable/colored-ball8"

android:

duration=

"50"

/>

</animation-list>

</animation-list>

Each frame points to one of the colored-ball images you have assembled through their resource IDs. The animation-list tag essentially gets converted into an AnimationDrawable object representing the collection of images. You will then need to set this Drawable as a background resource for our ImageView in the sample. Assuming that the file name for this XML file is frame_animation.xml and that it resides in the /res/drawable subdirectory, you can use the following code to set the AnimationDrawable as the background of the ImageView:

view.setBackGroundResource(Resource.drawable.frame_animation);

With this code, Android realizes that the resource ID Resource.drawable.frame_animation is an XML resource and accordingly constructs a suitable AnimationDrawable Java object for it before setting it as the background. Once this is set, you can access this AnimationDrawable object by doing a get on the view object like this:

Object backgroundObject = view.getBackground(); AnimationDrawable ad = (AnimationDrawable)backgroundObject;

Once you have the AnimationDrawable, you can use the start() and stop() methods of this object to start and stop the animation. Here are two other important methods on this object:

setOneShot();

addFrame(drawable, duration);

The setOneShot() method runs the animation once and then stops. The addFrame() method adds a new frame using a Drawable object and sets its display duration. The functionality of the addFrame() method resembles that of the XML tag android:drawable.

Put this all together to get the complete code for our frame-by-frame animation test harness (see Listing 6-4).

chapter 6 ■ unveiling 2D animation

Listing 6-4. Complete Code for the Frame-by-Frame Animation Test Harness

0 0

Post a comment