Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Android Animation


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
  1. <RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
  2.     xmlns:tools=“http://schemas.android.com/tools”
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent”
  5.     android:paddingBottom=“@dimen/activity_vertical_margin”
  6.     android:paddingLeft=“@dimen/activity_horizontal_margin”
  7.     android:paddingRight=“@dimen/activity_horizontal_margin”
  8.     android:paddingTop=“@dimen/activity_vertical_margin”
  9.     tools:context=“.MainActivity” >
  10.     <View
  11.              />
  12. </RelativeLayout>
File: logo.xml

Have a image view only.

  1. <?xml version=“1.0” encoding=“utf-8”?>
  2. <ImageView xmlns:android=“http://schemas.android.com/apk/res/android”
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent”
  5.     android:id=“@+id/anm”
  6.      >
  7. </ImageView>

MainActivity class

File: MainActivity.java
  1. package com.Kreationnext.animation;
  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.graphics.drawable.AnimationDrawable;
  5. import android.widget.ImageView;
  6. public class MainActivity extends Activity {
  7.     ImageView anm;
  8.      public void onCreate(Bundle savedInstanceState) {
  9.             super.onCreate(savedInstanceState);
  10.             setContentView(R.layout.logo);
  11.             anm = (ImageView)findViewById(R.id.anm);
  12.             anm.setBackgroundResource(R.drawable.animation);
  13.         // the frame-by-frame animation defined as a xml file within the drawable folder
  14.             /*
  15.              * NOTE: It’s not possible to start the animation during the onCreate.
  16.              */
  17.         }
  18.      public void onWindowFocusChanged (boolean hasFocus) {
  19.             super.onWindowFocusChanged(hasFocus);
  20.             AnimationDrawable frameAnimation =
  21.                 (AnimationDrawable) anm.getBackground();
  22.             if(hasFocus) {
  23.                 frameAnimation.start();
  24.             } else {
  25.                 frameAnimation.stop();
  26.             }
  27.         }
  28. }

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
  1. <?xml version=“1.0” encoding=“utf-8”?>
  2. <animation-list xmlns:android=“http://schemas.android.com/apk/res/android”
  3.     android:oneshot=“false”>
  4.     <item android:drawable=“@drawable/frame0” android:duration=“120” />
  5.     <item android:drawable=“@drawable/frame1” android:duration=“120” />
  6.     <item android:drawable=“@drawable/frame2” android:duration=“120” />
  7.     <item android:drawable=“@drawable/frame3” android:duration=“120” />
  8.     <item android:drawable=“@drawable/frame4” android:duration=“120” />
  9.     <item android:drawable=“@drawable/frame5” android:duration=“120” />
  10.     <item android:drawable=“@drawable/frame6” android:duration=“120” />
  11.     <item android:drawable=“@drawable/frame7” android:duration=“120” />
  12.     <item android:drawable=“@drawable/frame8” android:duration=“120” />
  13.     <item android:drawable=“@drawable/frame9” android:duration=“120” />
  14.     <item android:drawable=“@drawable/frame10” android:duration=“120” />
  15.     <item android:drawable=“@drawable/frame11” android:duration=“120” />
  16.     <item android:drawable=“@drawable/frame12” android:duration=“120” />
  17.     <item android:drawable=“@drawable/frame13” android:duration=“120” />
  18.     <item android:drawable=“@drawable/frame14” android:duration=“120” />
  19.     <item android:drawable=“@drawable/frame14” android:duration=“120” />
  20.     <item android:drawable=“@drawable/frame13” android:duration=“120” />
  21.     <item android:drawable=“@drawable/frame12” android:duration=“120” />
  22.     <item android:drawable=“@drawable/frame11” android:duration=“120” />
  23.     <item android:drawable=“@drawable/frame10” android:duration=“120” />
  24.     <item android:drawable=“@drawable/frame9” android:duration=“120” />
  25.     <item android:drawable=“@drawable/frame8” android:duration=“120” />
  26.     <item android:drawable=“@drawable/frame7” android:duration=“120” />
  27.     <item android:drawable=“@drawable/frame6” android:duration=“120” />
  28.     <item android:drawable=“@drawable/frame5” android:duration=“120” />
  29.     <item android:drawable=“@drawable/frame4” android:duration=“120” />
  30.     <item android:drawable=“@drawable/frame3” android:duration=“120” />
  31.     <item android:drawable=“@drawable/frame2” android:duration=“120” />
  32.     <item android:drawable=“@drawable/frame1” android:duration=“120” />
  33.     <item android:drawable=“@drawable/frame0” android:duration=“120” />
  34. </animation-list>

download this android example

Output:

android-example1

 

android-example2

 

Comments

comments