Android frame Animation

By | May 27, 2012

A series of frames is drawn one after the other at regular intervals.
For this create a xml which contains ImageView for showing the animation

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=
	"http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:background="#FFFFFF"
   android:gravity="center_vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">


   <ImageView
      android:id="@+id/Image"
       android:layout_gravity="center_horizontal"
      android:layout_width="wrap_content"
      android:background="@drawable/d1"
      android:layout_height="wrap_content"/>

      <Button
      android:id="@+id/startFAButtonId"
      android:layout_width="wrap_content"
      android:layout_gravity="center_horizontal"
      android:layout_height="wrap_content"
      android:text="Start Animation"
      />
</LinearLayout>

The main java file is

package com.coderzheaven.animation;

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

public class Frame extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button b = (Button)this.findViewById(R.id.startFAButtonId);
        b.setOnClickListener(new View.OnClickListener()
        {
			public void onClick(View v)
			{
				animate();
			}
		});

    }
 	   private void animate() {
 	      ImageView imgView = (ImageView)findViewById(R.id.Image);
 	      imgView.setVisibility(ImageView.VISIBLE);
 	      imgView.setBackgroundResource(R.drawable.animation_frame);

 	      AnimationDrawable frameAnimation =  (AnimationDrawable) imgView.getBackground();

 	      if (frameAnimation.isRunning()) {
 	         frameAnimation.stop();
 	      }
 	      else {
 	         frameAnimation.stop();
 	         frameAnimation.start();
 	      }
 	   }
 }

Next the main part, an xml which holds each image and duration in which each image shows.
The xml should be placed inside drawable folder

<?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/d1" android:duration="50" />
   <item android:drawable="@drawable/d2" android:duration="50" />
   <item android:drawable="@drawable/d3" android:duration="50" />
   <item android:drawable="@drawable/d4" android:duration="50" />
   <item android:drawable="@drawable/d5" android:duration="50" />
</animation-list>

When i click the button the animation will start

One thought on “Android frame Animation

  1. Pingback: Wikipedia

Leave a Reply

Your email address will not be published. Required fields are marked *