Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Drawable Animations in Android

Introduction

Drawable animations are a simple yet powerful way to create frame-by-frame animations in Android. They involve displaying a sequence of Drawable resources one after another to create the illusion of motion. This tutorial will guide you through the process of creating and implementing drawable animations in an Android application.

Setting Up Your Project

Before we start creating drawable animations, ensure you have an Android project set up in Android Studio. If not, follow these steps:

1. Open Android Studio.
2. Click on "Start a new Android Studio project".
3. Choose "Empty Activity" and click "Next".
4. Name your project and set up the necessary configurations.
5. Click "Finish" to create the project.

Creating Frame-by-Frame Animation

To create a frame-by-frame animation, you need a sequence of drawable resources (images) that represent each frame of the animation. Place these images in the res/drawable directory of your project.

For example, let's consider we have three images: frame1.png, frame2.png, and frame3.png.

Place the following XML file in the res/drawable directory and name it animation.xml:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
  <item android:drawable="@drawable/frame1" android:duration="200" />
  <item android:drawable="@drawable/frame2" android:duration="200" />
  <item android:drawable="@drawable/frame3" android:duration="200" />
</animation-list>

This XML file defines an animation list where each <item> represents a frame of the animation. The android:duration attribute specifies how long each frame will be displayed (in milliseconds). The android:oneshot attribute determines if the animation should play only once or loop continuously.

Displaying the Animation

To display the animation, you need to set the animation list as the source of an ImageView and start the animation programmatically.

First, add an ImageView to your activity_main.xml layout file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"/>
</RelativeLayout>

Next, in your MainActivity.java, load the animation and start it:

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView imageView = findViewById(R.id.imageView);
        imageView.setBackgroundResource(R.drawable.animation);

        AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
        animationDrawable.start();
    }
}

Conclusion

Drawable animations in Android provide a straightforward way to create frame-by-frame animations using a sequence of images. By following this tutorial, you should now be able to create your own drawable animations and incorporate them into your Android applications. Experiment with different images and durations to create unique animations for your app.