ZOFTINO.COM android and web dev tutorials

Drawable Animation in Android

Animation by displaying each drawable from sequence of drawables for certain duration can be achieved in Android using AnimationDrawable. AnimationDrawable takes a list of drawables and displays them one by one for the specified duration.

First you need to define AnimationDrawable. AnimationDrawable can be defined in xml or code. Then you need to call start() method on AnimationDrawable to start the animation. You can start AnimationDrawable in response to user events or without user interaction. Make sure that start() method is called after AnimationDrawable is attached to window. If you want to display animation without user interaction, call start() method in onWindowFocusChanged() method in your activity.

You can configure drawable animation to run only once by setting oneshot property to true. For each frame in the drawable animation, you can set duration in milliseconds frame displays.

You can configure drawable animation in xml by using animation-list element and animation-list contains list of drawable items as shown in below example. You need to create drawable animation xml file in res/drawable folder. You can add drawable items in the code also by calling addFrame() method of AnimationDrawable.

Drawable Animation Example

android drawable animation example

Drawable Animation Code

Activity

 package com.zoftino.animation;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

public class DrawableAnimationActivity extends AppCompatActivity {
    AnimationDrawable drawableAnimation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drawable_anim);

        ImageView imgView = (ImageView) findViewById(R.id.imgview);
        imgView.setBackgroundResource(R.drawable.directions);

        drawableAnimation = (AnimationDrawable) imgView.getBackground();
    }

    public void startAnimation(View view){
        drawableAnimation.start();
    }

    public void stopAnimation(View view){
        if(drawableAnimation.isRunning()){
            drawableAnimation.stop();
        }
    }
}

Activity layout

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.zoftino.animation.DrawableAnimationActivity">

    <TextView
        android:id="@+id/tview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Drawable Animations"></TextView>

    <ImageView
        android:id="@+id/imgview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" ></ImageView>
    <Button
        android:text="Start Animation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="startAnimation"
        android:id="@+id/button"></Button>
    <Button
        android:text="Stop Animation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="stopAnimation"
        android:id="@+id/button2"></Button>

</LinearLayout>

Animation list

 <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/direction" android:oneshot="false">
    <item android:drawable="@drawable/aup" android:duration="40"></item>
    <item android:drawable="@drawable/afwd" android:duration="40"></item>
    <item android:drawable="@drawable/adwn" android:duration="40"></item>
    <item android:drawable="@drawable/abwd" android:duration="40"></item>
</animation-list>