Android supports two types of animation. Tweened animations can be used to rotate, move, stretch, and fade a View; or you can create frame-by-frame animations to display a sequence of Drawable images. A comprehensive overview of creating, using, and applying animations can be found in Chapter 15.

Defining animations as external resources enables you to reuse the same sequence in multiple places and provides you with the opportunity to present different animations based on device hardware or orientation.

Tweened Animations

Each tweened animation is stored in a separate XML file in the project's res/anim folder. As with layouts and Drawable resources, the animation's file name is used as its resource identifier.

An animation can be defined for changes in alpha (fading), scale (scaling), translate (movement), or rotate (rotation).

Table 3-1 shows the valid attributes, and attribute values, supported by each animation type.

You can create a combination of animations using the set tag. An animation set contains one or more animation transformations and supports various additional tags and attributes to customize when and how each animation within the set is run.

The following list shows some of the set tags available.

> duration Duration of the animation in milliseconds.

> startOffset Millisecond delay before the animation starts.

> fillBefore true to apply the animation transformation before it begins.

> fillAfter true to apply the animation transformation after it ends.

> interpolator Sets how the speed of this effect varies over time. Chapter 15 explores the interpolators available. To specify one, reference the system animation resources at android:anim/interpolatorName

TABLE 3-1: Animation type attributes




Alpha Scale


Rotate fromAlpha/toAlpha fromXSca I e/toXSca I e from YSca I e/toYSca le pivotX/pivotY

fromX/to X fromY/toY

fromDegrees/toDegrees pivotX/pivot Y

Float from 0 to 1 Float from 0 to 1 Float from 0 to 1

String of the percentage of graphic width/height from 0% to 100%

Float from 0 to 1

Float from 0 to 1

Float from 0 to 360

String of the percentage of graphic width/height from 0% to 100%

If you do not use the startof f set tag, all the animation effects within a set will execute simultaneously.

The following example shows an animation set that spins the target 360 degrees while it shrinks and fades out.

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android=""

android:interpolator="@android:anim/accelerate_interpolator"> <rotate android:fromDegrees="0" android:toDegrees="3 60" android:pivotX="50%" android:pivotY="50%" android:startOffset="500" android:duration="1000" /> <scale android:fromXScale="1.0" android:toXScale="0.0"

android:fromYScale="1.0" android:toYScale="0.0" android:pivotX="50%" android:pivotY="50%" android:start0ffset="500" android:duration="500" /> <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:start0ffset="500" android:duration="500" /> </set>

Frame-by-Frame Animations

Frame-by-frame animations let you create a sequence of Drawables, each of which will be displayed for a specified duration, on the background of a View.

Because frame-by-frame animations represent animated Drawables they are stored in the res/drawable folder, rather than with the tweened animations, and use their file names (without the .xml extension) as their resource IDs.

The following XML snippet shows a simple animation that cycles through a series of bitmap resources, displaying each one for half a second. In order to use this snippet you will need to create new image resources rocketl through rocket3.

<animation-list xmlns:android="" android:oneshot="false">

<item android:drawable="@drawable/rocket1" android:duration="500" /> <item android:drawable="@drawable/rocket2" android:duration="500" /> <item android:drawable="@drawable/rocket3" android:duration="500" /> </animation-list>

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide

Post a comment