Tutorial details

Android beginner tutorial: Part 96 - Frame Animation using code | App Code for Sale | Preview

Android beginner tutorial: Part 96 - Frame Animation using code | Android Tutorial

How to create a Frame Animation using Java code instead of an XML file

Overview PAGE TOP

In this tutorial we will learn how to create a Frame Animation using Java code instead of an XML file.

We'll be using the same 3 drawable resources (images) as frames in this tutorial as we did last time. But instead of creating the sequence in an XML file, we'll do that in the MainActivity.java class of our project.

The acitivty_main.xml layout file remains like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <Button android:text="Start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/btn_start"
            />
        <Button android:text="Stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/btn_stop"
            />
    </LinearLayout>

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</LinearLayout>

In MainActivity class, we first declare an AnimationDrawable and ImageView instances:

AnimationDrawable animation;
ImageView image;

We then add 2 clicks listeners to the two buttons on the screen. When the Start button is pressed, create 3 BitmapDrawable instances that refer to the 3 drawable resources we have. Then set the value of the animation object to a new AnimationDrawable() instance, set its oneShot property to false using setOneShot() method. Add each frame to it using addFrame() method, which has 2 parameters - reference to the drawable used in this frame and the duration of the frame.

Then we set the content of the image to this animation using setImageDrawable() method and start the animation after making it visible using the setVisible() method:

final Button btnStart = (Button)findViewById(R.id.btn_start);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BitmapDrawable frame1 = (BitmapDrawable)getResources().getDrawable(R.drawable.sample1);
BitmapDrawable frame2 = (BitmapDrawable)getResources().getDrawable(R.drawable.sample2);
BitmapDrawable frame3 = (BitmapDrawable)getResources().getDrawable(R.drawable.sample3);

animation = new AnimationDrawable();
animation.setOneShot(false);
animation.addFrame(frame1, 500);
animation.addFrame(frame2, 500);
animation.addFrame(frame3, 500);

image = (ImageView)findViewById(R.id.image);
image.setImageDrawable(animation);
animation.setVisible(true, true);
animation.start();
}
});

When the Stop button is pressed, stop the animation:

final Button btnStop = (Button)findViewById(R.id.btn_stop);
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animation.stop();
animation.setVisible(false, false);
}
});

Full code:

package com.example.codeforfoodtest_two;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity{

AnimationDrawable animation;
ImageView image;

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

final Button btnStart = (Button)findViewById(R.id.btn_start);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BitmapDrawable frame1 = (BitmapDrawable)getResources().getDrawable(R.drawable.sample1);
BitmapDrawable frame2 = (BitmapDrawable)getResources().getDrawable(R.drawable.sample2);
BitmapDrawable frame3 = (BitmapDrawable)getResources().getDrawable(R.drawable.sample3);

animation = new AnimationDrawable();
animation.setOneShot(false);
animation.addFrame(frame1, 500);
animation.addFrame(frame2, 500);
animation.addFrame(frame3, 500);

image = (ImageView)findViewById(R.id.image);
image.setImageDrawable(animation);
animation.setVisible(true, true);
animation.start();
}
});

final Button btnStop = (Button)findViewById(R.id.btn_stop);
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animation.stop();
animation.setVisible(false, false);
}
});

}
}

Thanks for reading!

Reference PAGE TOP

http://kirill-poletaev.blogspot.it/2013/02/android-beginner-tutorial-part-96-frame.html

0 Comments Leave a comment

Please login in order to leave a comment.

Newest first
!

Sign-in to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.