page.title=Animation Resources parent.title=Resource Types parent.link=available-resources.html @jd:body
An animation resource can define one of two types of animations:
An animation defined in XML that performs transitions such as rotating, fading, moving, and stretching on a graphic.
res/anim/filename.xmlR.anim.filename@[package:]anim/filename
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@[package:]anim/interpolator_resource"
android:shareInterpolator=["true" | "false"] >
<alpha
android:fromAlpha="float"
android:toAlpha="float" />
<scale
android:fromXScale="float"
android:toXScale="float"
android:fromYScale="float"
android:toYScale="float"
android:pivotX="float"
android:pivotY="float" />
<translate
android:fromX="float"
android:toX="float"
android:fromY="float"
android:toY="float" />
<rotate
android:fromDegrees="float"
android:toDegrees="float"
android:pivotX="float"
android:pivotY="float" />
<set>
...
</set>
</set>
The file must have a single root element: either an
<alpha>, <scale>, <translate>,
<rotate>, or <set> element that holds
a group (or groups) of other animation elements (even nested <set> elements).
res/anim/hyperspace_jump.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<scale
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXScale="1.0"
android:toXScale="1.4"
android:fromYScale="1.0"
android:toYScale="0.6"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="false"
android:duration="700" />
<set
android:interpolator="@android:anim/accelerate_interpolator"
android:startOffset="700">
<scale
android:fromXScale="1.4"
android:toXScale="0.0"
android:fromYScale="0.6"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="400" />
<rotate
android:fromDegrees="0"
android:toDegrees="-45"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="400" />
</set>
</set>
This application code will apply the animation to an {@link android.widget.ImageView} and start the animation:
ImageView image = (ImageView) findViewById(R.id.image);
Animation hyperspaceJump = AnimationUtils.{@link android.view.animation.AnimationUtils#loadAnimation(Context,int) loadAnimation}(this, R.anim.hyperspace_jump);
image.{@link android.view.View#startAnimation(Animation) startAnimation}(hyperspaceJump);
An interpolator is an animation modifier defined in XML that affects the rate of change in an animation. This allows your existing animation effects to be accelerated, decelerated, repeated, bounced, etc.
An interpolator is applied to an animation element with the {@code android:interpolator} attribute, the value of which is a reference to an interpolator resource.
All interpolators available in Android are subclasses of the {@link android.view.animation.Interpolator} class. For each interpolator class, Android includes a public resource you can reference in order to apply the interpolator to an animation using the the {@code android:interpolator} attribute. The following table specifies the resource to use for each interpolator:
| Interpolator class | Resource ID |
|---|---|
| {@link android.view.animation.AccelerateDecelerateInterpolator} | {@code @android:anim/accelerate_decelerate_interpolator} |
| {@link android.view.animation.AccelerateInterpolator} | {@code @android:anim/accelerate_interpolator} |
| {@link android.view.animation.AnticipateInterpolator} | {@code @android:anim/anticipate_interpolator} |
| {@link android.view.animation.AnticipateOvershootInterpolator} | {@code @android:anim/anticipate_overshoot_interpolator} |
| {@link android.view.animation.BounceInterpolator} | {@code @android:anim/bounce_interpolator} |
| {@link android.view.animation.CycleInterpolator} | {@code @android:anim/cycle_interpolator} |
| {@link android.view.animation.DecelerateInterpolator} | {@code @android:anim/decelerate_interpolator} |
| {@link android.view.animation.LinearInterpolator} | {@code @android:anim/linear_interpolator} |
| {@link android.view.animation.OvershootInterpolator} | {@code @android:anim/overshoot_interpolator} |
Here's how you can apply one of these with the {@code android:interpolator} attribute:
<set android:interpolator="@android:anim/accelerate_interpolator">
...
</set>
If you're not satisfied with the interpolators provided by the platform (listed in the table above), you can create a custom interpolator resource with modified attributes. For example, you can adjust the rate of acceleration for the {@link android.view.animation.AnticipateInterpolator}, or adjust the number of cycles for the {@link android.view.animation.CycleInterpolator}. In order to do so, you need to create your own interpolator resource in an XML file.
res/anim/filename.xml@[package:]anim/filename
<?xml version="1.0" encoding="utf-8"?>
<InterpolatorName xmlns:android="http://schemas.android.com/apk/res/android"
android:attribute_name="value"
/>
If you don't apply any attributes, then your interpolator will function exactly the same as those provided by the platform (listed in the table above).
XML file saved at res/anim/my_overshoot_interpolator.xml:
<?xml version="1.0" encoding="utf-8"?>
<overshootInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
android:tension="7.0"
/>
This animation XML will apply the interpolator:
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@anim/my_overshoot_interpolator"
android:fromXScale="1.0"
android:toXScale="3.0"
android:fromYScale="1.0"
android:toYScale="3.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="700" />
An animation defined in XML that shows a sequence of images in order (like a film).
res/drawable/filename.xmlR.drawable.filename@[package:]drawable.filename
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot=["true" | "false"] >
<item
android:drawable="@[package:]drawable/drawable_resource_name"
android:duration="integer" />
</animation-list>
res/anim/rocket.xml:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
<item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
<item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
</animation-list>
ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
rocketImage.{@link android.view.View#setBackgroundResource(int) setBackgroundResource}(R.drawable.rocket_thrust);
rocketAnimation = (AnimationDrawable) rocketImage.{@link android.view.View#getBackground()};
rocketAnimation.{@link android.graphics.drawable.AnimationDrawable#start()};