Animation image in Android

Here I will show you how to make an animation image in Android.

It is very easy to make a animation image in background.
The first thing you need is to create a image view in your layout.

<RelativeLayout 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: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=".TheDeveloperWorldIsYours" >
    <ImageView android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/iv"/>

</RelativeLayout>

The most import line code is

final Handler handler = new Handler();
final Runnable r = new Runnable(){
      public void run(){};
}

a Handler, a Runnable and a run.

package com.thedeveloperworldisyours.animationimagen;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;

public class TheDeveloperWorldIsYours extends Activity {

	private static ImageView iv;

	final Handler handler = new Handler();
	Runnable r;

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

		iv = (ImageView) findViewById(R.id.iv);

		r = new Runnable()
		{
			int x = 1;
		    public void run()
		    {

		        switch(x) {
				 case 1:
					 iv.setImageResource(R.drawable.base1);
				     break;
				 case 2:
					 iv.setImageResource(R.drawable.base2);
				     break;
				 case 3:
					 iv.setImageResource(R.drawable.base3);
				     break;
				 case 4:
					 iv.setImageResource(R.drawable.base4);
				     break;
				 case 5:
					 iv.setImageResource(R.drawable.base5);
				     break;
				 case 6:
					 iv.setImageResource(R.drawable.base6);
				     break;
				 case 7:
					 iv.setImageResource(R.drawable.base7);
				     break;
				 case 8:
					 iv.setImageResource(R.drawable.base8);
				     break;
				 case 9:
					 iv.setImageResource(R.drawable.base9);
				     break;
				 default:
					 iv.setImageResource(R.drawable.base);
				     break;
				 }
		        if(x==9)
		        	x=1;
		        else
		        	x++;
		        handler.postDelayed(this, 1000);

		    }
		};

		handler.postDelayed(r, 1000);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		handler.removeCallbacks(r);
		super.onPause();
	}

}

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>