Animation sample program in Android

By: Ashley  

This sample android program shows you how to do simple animation in Android. In this program the xml file ani.xml is used to rotate the images in sequence. The images must be copied to the /res/drawable folder together with the ani.xml file.

The FrameAnimation1.java file is as follows:

package com.javasamples.ani;

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

public class FrameAnimation1 extends Activity {
	Button b;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		this.setupButton();
	}

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

	private void parentButtonClicked(View v) {
		animate();
	}

	private void animate() {
		ImageView imgView = (ImageView) findViewById(R.id.animationImage);
		// imgView.setVisibility(ImageView.VISIBLE);
		imgView.setBackgroundResource(R.drawable.ani);

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

		if (frameAnimation.isRunning()) {
			frameAnimation.stop();
			b.setText("Start");
		} else {
			frameAnimation.start();
			b.setText("Stop");
		}
	}
}// eof-class

The output of this program will be as shown in the android emulator below.

The main.xml file in your res/layout folder is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TextView
		android:id="@+id/textViewId1"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="Animated Demo" />
	<Button
		android:id="@+id/startFAButtonId"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="Start Animation" />
	<ImageView
		android:id="@+id/animationImage"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content" />
</LinearLayout>

In addition to the main.xml, you need to create another xml file ani.xml with the following code in your res/drawable folder. In that same folder, you also need to copy your image files that you specifiy in the ani.xml

<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item
android:drawable="@drawable/icon"
android:duration="50" />
<item
android:drawable="@drawable/icon1"
android:duration="50" />
<item
android:drawable="@drawable/icon2"
android:duration="50" />
</animation-list>

                                        


Archived Comments

1. its amazing for me i learn a lot from it thanks..
View Tutorial          By: sufyan at 2014-10-26 14:39:34

2. Its nice and really apriciate rakesh sir i try this code it work great
if u got any proble

View Tutorial          By: prince at 2013-04-18 09:46:34

3. In case your having trouble

1 the ani.xml file must be located in the res/layout dire

View Tutorial          By: Sin at 2013-01-17 05:40:37

4. imgView.setBackgroundResource(R.drawable.ani); i got error in this line.....it doesn;t shows this xm
View Tutorial          By: mamu at 2012-10-18 14:41:31

5. this is a good and simple app and working great but how to slow the speed of animated pictures???
View Tutorial          By: Dhiraj Bothra at 2012-09-09 06:24:24

6. imgView.setBackgroundResource(R.drawable.ani);

getting error with this line.wht to do

View Tutorial          By: Syamantak Basu at 2012-08-21 09:32:19

7. I did all steps, but it only shows the first picture of the 3 pictures which are in the ani xml file
View Tutorial          By: behzad at 2012-07-15 16:32:41

8. Thanks alot man! This is just what i needed!!!
View Tutorial          By: Smaïl at 2012-06-03 23:24:41

9. Change the line from
imgView.setBackgroundResource(R.drawable.ani);
to
imgView.

View Tutorial          By: laki at 2012-05-02 08:59:25

10. This is a simple app and its working good.......
View Tutorial          By: sathish at 2012-04-23 12:16:07

11. This is a simple app and its working good.......
View Tutorial          By: sathish at 2012-04-23 12:15:08

12. This is a simple app and its working good.......
View Tutorial          By: sathish at 2012-04-23 12:13:58

13. This is a simple app and its working good.......
View Tutorial          By: sathish at 2012-04-23 12:12:22

14. Animation code(very Similar to this) is not working on Android 2.3.x version but working on Android
View Tutorial          By: Rakesh Kumar at 2012-04-23 09:42:55

15. This is all the code that you need for creating drawable animations. Of course, you need to know som
View Tutorial          By: subduedjoy at 2012-03-20 20:52:18

16. I just copied this example to my app. It's not working. It raises lot of error. I need the exact cod
View Tutorial          By: jhansi at 2012-02-22 10:26:17

17. It tried this app... It's working fine... really a good pieces of code...
View Tutorial          By: Prakash at 2012-01-04 06:33:12

18. I just copied this example to my app. It's not working. It raises lot of error. I need the exact cod
View Tutorial          By: Praveen Kumar at 2011-11-17 12:59:40


Most Viewed Articles (in Android )

Latest Articles (in Android)

Comment on this tutorial