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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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
1 2 3 4 | final Handler handler = new Handler(); final Runnable r = new Runnable(){ public void run(){}; } |
a Handler, a Runnable and a run.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | 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(); } } |