Animation sample program in Android

By: Ashley Emailed: 1599 times Printed: 2051 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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>


Android Home | All Android Tutorials | Latest Android Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(12)


1. View Comment

I just copied this example to my app. It's not working. It raises lot of error. I need the exact code for this project. If you don't mind means, then send it to me. Thanks and Regards - S. Praveen Kumar

View Tutorial          By: Praveen Kumar at 2011-11-17 12:59:40
2. View Comment

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
3. View Comment

I just copied this example to my app. It's not working. It raises lot of error. I need the exact code for this project. If you don't mind means, then send it to me. Thanks

View Tutorial          By: jhansi at 2012-02-22 10:26:17
4. View Comment

This is all the code that you need for creating drawable animations. Of course, you need to know something about Android development to be able to use it. This example is very similar to the one on Android's developer site.

View Tutorial          By: subduedjoy at 2012-03-20 20:52:18
5. View Comment

Animation code(very Similar to this) is not working on Android 2.3.x version but working on Android 4.x

Any suggestion please.


View Tutorial          By: Rakesh Kumar at 2012-04-23 09:42:55
6. View Comment

This is a simple app and its working good.......

View Tutorial          By: sathish at 2012-04-23 12:16:07
7. View Comment

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

BR,
CL Sree


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

I did all steps, but it only shows the first picture of the 3 pictures which are in the ani xml file.
Whats the problem?


View Tutorial          By: behzad at 2012-07-15 16:32:41
9. View Comment

imgView.setBackgroundResource(R.drawable.ani); i got error in this line.....it doesn;t shows this xml file....But i include it into res/drawable/ani.xml....

View Tutorial          By: mamu at 2012-10-18 14:41:31
10. View Comment

In case your having trouble

1 the ani.xml file must be located in the res/layout directory not the drawable directory.

2 as mentioned earlier Change the line from
imgView.setBackgroundResource(R.drawable.ani);

to

imgView.setBackgroundResource(R.layout.ani);

3. in the ani.xml file you must have the drawable references in your drawable folder or the correct resolution display folder.

eg:

<item
android:drawable="@drawable/ic_launcher1"
android:duration="50" />
<item
android:drawable="@drawable/ic_launcher2"
android:duration="50" />
<item
android:drawable="@drawable/ic_launcher3"
android:duration="50" />

in this example i simply used the ic_launcher icon that by default is included in drawable-hdpi, drawable-mdpi, drawable-ldpi, folders and renamed them as ic_launcher_1,2,3 respectivley. And then copied them into the drawable folder...

you can use any png you want just make sure they are in the drawable folder and referenced in ani.xml

hope this makes sence...


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

Its nice and really apriciate rakesh sir i try this code it work great
if u got any problem then mail on my id i will send u it full running source code in new way somewhere it litilee bit confuse for new user but it good for learning and it for still image preferable how i used it as for moving hand plz help i wannna be a game developer it will help me plzzzzzzzzz my mail id Princeraizadha.98@gmail.com


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

its amazing for me i learn a lot from it thanks..

View Tutorial          By: sufyan at 2014-10-26 14:39:34

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Ashley
Is autodiscover CNAME a must in Office 365
Directory Synchronization tool in Office 365
Configure Outlook Manually for Office 365
Review: Ruggear - The world's most Rugged Phone in Singapore
Solution to error: unable to open connection to server due to security error
Making your Alias email as 'Send As' in Office 365
Steps to Create Shared Mailbox in office 365
xxxx cannot be loaded because the execution of scripts is disabled on this system. Please see “get-help about_signing” for more details.
The user's Outlook Live mailbox is full.
Outlook Live couldn't connect to the IMAP server.
The user's mailbox has already been migrated.
An Outlook Live mailbox wasn't found for the user.
Outlook Live couldn't sign in to the user account on the IMAP messaging system.
Android Send SMS and Make a phone call in Android Application
Android Preferences - Using Preferences in Android Tutorial

More Tutorials in Android
Getting Started with Android
Solution to error: unable to open connection to server due to security error
Android Preferences - Using Preferences in Android Tutorial
Android Send SMS and Make a phone call in Android Application
Android Emulator
Intent in Android to call one activity from another activity.
Animation sample program in Android
Another Animation sample program in Android
ArrayAdapter sample program in Android
Spinner sample program in Android
GridView sample program in Android
Gallery sample program in Android
Date and Time sample program in Android
Progress bar and downloading a file sample program in Android
Reading a file sample program in Android

More Latest News
Most Viewed Articles (in Android )
Progress bar and downloading a file sample program in Android
ArrayAdapter sample program in Android
Android Send SMS and Make a phone call in Android Application
Reading a file sample program in Android
Animation sample program in Android
GridView sample program in Android
Date and Time sample program in Android
Intent in Android to call one activity from another activity.
AlertDialog sample program in Android
Spinner sample program in Android
Reading and Writing a file to SD card sample program in Android
Solution to error: unable to open connection to server due to security error
Getting Started with Android
Error generating R.java from manifest
Another Animation sample program in Android
Most Emailed Articles (in Android)
ArrayAdapter sample program in Android
GridView sample program in Android
Android Emulator
Android Preferences - Using Preferences in Android Tutorial
Android Send SMS and Make a phone call in Android Application
Animation sample program in Android
Error generating R.java from manifest
Another Animation sample program in Android
Spinner sample program in Android
Date and Time sample program in Android
Progress bar and downloading a file sample program in Android
Reading a file sample program in Android
Intent in Android to call one activity from another activity.
Solution to error: unable to open connection to server due to security error
Getting Started with Android