TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Android Animation Example

Android Animation Example with examples of Activity and Intent, Fragments, Menu, Service, alarm manager, storage, sqlite, xml, json, multimedia, speech, web service, telephony, animation and graphics

<< Back to ANDROID

Android Animation Example

Android provides a large number of classes and interface for the animation development. Most of the classes and interfaces are given in android.animation package.

Android Animation enables you to change the object property and behavior at run time. There are various ways to do animation in android.

The AnimationDrawable class provides methods to start and end the animation. Even, you can use time based animation.

Let's have a look at the simple example of android animation.

activity_main.xml

You need to have a view only.

File: activity_main.xml
<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=".MainActivity" >

    <View
             />

</RelativeLayout>
File: logo.xml

Have a image view only.

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/anm"
     >

</ImageView>

MainActivity class

File: MainActivity.java
package com.TheDeveloperBlog.animation;

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

public class MainActivity extends Activity {

	ImageView anm;
	 public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.logo);
	        anm = (ImageView)findViewById(R.id.anm);
	        
	        anm.setBackgroundResource(R.drawable.animation);
		// the frame-by-frame animation defined as a xml file within the drawable folder
	        
	        /*
	         * NOTE: It's not possible to start the animation during the onCreate.
	         */
	    }
	 public void onWindowFocusChanged (boolean hasFocus) {
			super.onWindowFocusChanged(hasFocus);
			AnimationDrawable frameAnimation = 
				(AnimationDrawable) anm.getBackground();
			if(hasFocus) {
				frameAnimation.start();
			} else {
				frameAnimation.stop();
			}
		}

}

You need to create animation.xml file inside res/drawable-hdpi directory.

You need to have many images. Here, we are using 14 images and all the 14 images are located inside res/drawable-mdpi directory.

File: animation.xml
<?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/frame0" android:duration="120" />
	<item android:drawable="@drawable/frame1" android:duration="120" />
	<item android:drawable="@drawable/frame2" android:duration="120" />
	<item android:drawable="@drawable/frame3" android:duration="120" />
	<item android:drawable="@drawable/frame4" android:duration="120" />
	<item android:drawable="@drawable/frame5" android:duration="120" />
	<item android:drawable="@drawable/frame6" android:duration="120" />
	<item android:drawable="@drawable/frame7" android:duration="120" />
	<item android:drawable="@drawable/frame8" android:duration="120" />
	<item android:drawable="@drawable/frame9" android:duration="120" />
	<item android:drawable="@drawable/frame10" android:duration="120" />
	<item android:drawable="@drawable/frame11" android:duration="120" />
	<item android:drawable="@drawable/frame12" android:duration="120" />
	<item android:drawable="@drawable/frame13" android:duration="120" />
	<item android:drawable="@drawable/frame14" android:duration="120" />
	<item android:drawable="@drawable/frame14" android:duration="120" />
	<item android:drawable="@drawable/frame13" android:duration="120" />
	<item android:drawable="@drawable/frame12" android:duration="120" />
	<item android:drawable="@drawable/frame11" android:duration="120" />
	<item android:drawable="@drawable/frame10" android:duration="120" />
	<item android:drawable="@drawable/frame9" android:duration="120" />
	<item android:drawable="@drawable/frame8" android:duration="120" />
	<item android:drawable="@drawable/frame7" android:duration="120" />
	<item android:drawable="@drawable/frame6" android:duration="120" />
	<item android:drawable="@drawable/frame5" android:duration="120" />
	<item android:drawable="@drawable/frame4" android:duration="120" />
	<item android:drawable="@drawable/frame3" android:duration="120" />
	<item android:drawable="@drawable/frame2" android:duration="120" />
	<item android:drawable="@drawable/frame1" android:duration="120" />
	<item android:drawable="@drawable/frame0" android:duration="120" />
	
</animation-list>


Output:

android animation example output 1 android animation example output 2






Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf