14.01.2013 Views

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 11: Advanced Android <strong>Development</strong><br />

Creating and Using Frame-by-Frame Animations<br />

372<br />

Frame-by-frame animations are akin to traditional cell-based cartoons where an image is chosen for each<br />

frame. Where tweened animations use the target View to supply the content of the animation, frame-byframe<br />

animations let you specify a series of Drawable objects that are used as the background to a View.<br />

The AnimationDrawable class is used to create a new frame-by-frame animation presented as a<br />

Drawable resource.<br />

You can defi ne your Animation Drawable resource as an external resource in your project’s drawable<br />

folder using XML. Use the animation-list tag to group a collection of item tags, each of which uses a<br />

drawable attribute to defi ne an image to display, and a duration attribute to specify the time (in milliseconds)<br />

to display it.<br />

The following XML snippet shows how to create a simple animation that displays a rocket taking off<br />

(rocket images not included). The fi le is stored as res/drawable/animated_rocket.xml:<br />

<br />

<br />

<br />

<br />

<br />

To display your animation, set it as the background to a View using the setBackgroundResource<br />

method, as shown in the following code snippet:<br />

ImageView image = (ImageView)findViewById(R.id.my_animation_frame);<br />

image.setBackgroundResource(R.drawable.animated_rocket);<br />

Alternatively, use the setBackgroundDrawable to use a Drawable instance instead of a resource reference.<br />

Run the animation calling its start method, as shown in the code snippet below:<br />

AnimationDrawable animation = (AnimationDrawable)image.getBackground();<br />

animation.start();<br />

Using Themes to Skin Your <strong>Application</strong>s<br />

The multifunction nature of a mobile device means users will be running and switching between many<br />

applications created by a range of different developers. Themes are a way of ensuring that your applications<br />

present a consistent look and feel.<br />

To apply a theme, set the android:theme attribute on either the application or an individual<br />

activity tag in the manifest, as shown in the code snippet below:<br />

<br />

<br />

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!