Animate drawable graphics

Stay organized with collections Save and categorize content based on your preferences.

In some situations, images need to be animated on screen. This is useful if you want to display a custom loading animation comprised of several images, or if you want one icon to morph into another after a user's action. Android provides a couple options for animating drawables.

The first option is to use an Animation Drawable. This allows you to specify several static drawable files that will be displayed one at a time to create an animation. The second option is to use an Animated Vector Drawable, which lets you animate the properties of a vector drawable.

Use AnimationDrawable

One way to animate Drawables is to load a series of Drawable resources one after another to create an animation. This is a traditional animation in the sense that it is created with a sequence of different images, played in order, like a roll of film. The AnimationDrawable class is the basis for Drawable animations.

While you can define the frames of an animation in your code, using the AnimationDrawable class API, it's more simply accomplished with a single XML file that lists the frames that compose the animation. The XML file for this kind of animation belongs in the res/drawable/ directory of your Android project. In this case, the instructions are the order and duration for each frame of the animation.

The XML file consists of an <animation-list> element as the root node and a series of child <item> nodes that each define a frame: a drawable resource for the frame and the frame duration. Here's an example XML file for a Drawable animation:

<animation-list xmlns:android=""
    <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" />

This animation runs for just three frames. By setting the android:oneshot attribute of the list to true, it will cycle just once then stop and hold on the last frame. If it is set false then the animation will loop. With this XML saved as rocket_thrust.xml in the res/drawable/ directory of the project, it can be added as the background image to a View and then called to play. Here's an example Activity, in which the animation is added to an ImageView and then animated when the screen is touched:


private lateinit var rocketAnimation: AnimationDrawable

override fun onCreate(savedInstanceState: Bundle?) {

    val rocketImage = findViewById<ImageView>( {
        rocketAnimation = background as AnimationDrawable

    rocketImage.setOnClickListener({ rocketAnimation.start() })


AnimationDrawable rocketAnimation;

public void onCreate(Bundle savedInstanceState) {

  ImageView rocketImage = (ImageView) findViewById(;
  rocketAnimation = (AnimationDrawable) rocketImage.getBackground();

  rocketImage.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {

It's important to note that the start() method called on the AnimationDrawable cannot be called during the onCreate() method of your Activity, because the AnimationDrawable is not yet fully attached to the window. If you want to play the animation immediately, without requiring interaction, then you might want to call it from the onStart() method in your Activity, which will get called when Android makes the view visible on screen.

For more information on the XML syntax, available tags and attributes, see Animation Resources.

Use AnimatedVectorDrawable

A vector drawable is a type of drawable that is scalable without getting pixelated or blurry. The AnimatedVectorDrawable class (and AnimatedVectorDrawableCompat for backward-compatibility) lets you animate the properties of a vector drawable, such as rotating it or changing the path data to morph it into a different image.

You normally define animated vector drawables in three XML files:

  • A vector drawable with the <vector> element in res/drawable/
  • An animated vector drawable with the <animated-vector> element in res/drawable/
  • One or more object animators with the <objectAnimator> element in res/animator/

Animated vector drawables can animate the attributes of the <group> and <path> elements. The <group> elements defines a set of paths or subgroups, and the <path> element defines paths to be drawn.

When you define a vector drawable that you want to animate, use the android:name attribute to assign a unique name to groups and paths, so you can refer to them from your animator definitions. For example:


<vector xmlns:android=""
        android:rotation="45.0" >
            android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" />

The animated vector drawable definition refers to the groups and paths in the vector drawable by their names:


<animated-vector xmlns:android=""
  android:drawable="@drawable/vectordrawable" >
        android:animation="@animator/rotation" />
        android:animation="@animator/path_morph" />

The animation definitions represent ObjectAnimator or AnimatorSet objects. The first animator in this example rotates the target group 360 degrees:


    android:valueTo="360" />

The second animator in this example morphs the vector drawable's path from one shape to another. Both paths must be compatible for morphing: they must have the same number of commands and the same number of parameters for each command.


<set xmlns:android="">
        android:valueFrom="M300,70 l 0,-70 70,70 0,0   -70,70z"
        android:valueTo="M300,70 l 0,-70 70,0  0,140 -70,0 z"
        android:valueType="pathType" />

Here is the resulting AnimatedVectorDrawable:

Animated Vector Drawable (AVD) preview

The Animated Vector Drawable tool in Android Studio offers the ability to preview animated drawable resources. This tool helps you preview <animation-list>, <animated-vector>, and <animated-selector> resources in Android Studio and makes it easier to refine your custom animations. User previewing and playing an animation inside Android Studio

For more information, see the API reference for AnimatedVectorDrawable.