Animation in android is possible from many ways. In this chapter we will discuss one easy and widely used way of making animation called tweened animation.

Tween Animation

Tween Animation takes some parameters such as start value , end value, size , time duration , rotation angle e.t.c and perform the required animation on that object. It can be applied to any type of object. So in order to use this , android has povided us a class called Animation.
In order to perform animation in android , we are going to call a static function loadAnimation() of the class AnimationUtils. We are going to recieve the result in an instance of Animation Object. Its syntax is as follows:
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.myanimation);
Note the second parameter. It is the name of the our animation xml file. You have to create a new folder called anim under res directory and make an xml file under anim folder.
This animation class has many usefull functions which are listed below:
Sr.NoMethod & Description
This method starts the animation.
2setDuration(long duration)
This method sets the duration of an animation.
This method gets the duration which is set by above method 
This method ends the animation.
This method cancels the animation.
In order to apply this animation to an object , we will just call the startAnimation() method of the object. Its syntax is:
ImageView image1 = (ImageView)findViewById(;

Zoom in animation

In order to perform a zoom in animation , create an XML file under anim folder under res directory and put this code in the file.



The parameter fromXScale and fromYScale defines the start point and the parameters toXScale andtoYScale defines the end point. The duration defines the time of animation and the pivotX,pivotYdefines the center from where the animation would start.


The following example demonstrates the use of Animation in android. You would be able to choose different type of animation from the menu and the selected animation will be applied on an imageView on the screen.
To experiment with this example , you need to run this on an emulator or an actual device.
1You will use Eclipse IDE to create an Android application and name it as Animation under a package com.example.animation. While creating this project, make sure you Target SDK and Compile With at the latest version of Android SDK to use higher levels of APIs.
2Modify src/ file to add animation code 
3Modify layout XML file res/layout/activity_main.xml add any GUI component if required.
4Create a new folder under res directory and call it anim. Confim it by visiting res/anim
5Right click on anim and click on new and select Android XML file You have to create three different files that are listed below.
6Create files myanimation.xml,clockwise.xml,fade.xml and add the XML code.
7Modify res/values/string.xml file and add necessary string components.
8Modify res/menu/main.xml file and add necessary menu components
9Run the application and choose a running android device and install the application on it and verify the results.
Here is the modified code of src/com.example.animation/
package com.example.animation;

import com.example.animation.R;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class MainActivity extends Activity {

   protected void onCreate(Bundle savedInstanceState) {

   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(, menu);
      return true;

   public boolean onOptionsItemSelected(MenuItem item) 
         ImageView image = (ImageView)findViewById(;
         Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.myanimation);
            return true;
        ImageView image1 = (ImageView)findViewById(;
        Animation animation1 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.clockwise);
            return true;
        ImageView image2 = (ImageView)findViewById(;
        Animation animation2 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade);
           return true;

      return false;


Here is the modified code of res/layout/activity_main.xml.


Here is the code of res/anim/myanimation.xml.





Here is the code of res/anim/clockwise.xml.





Here is the code of res/anim/fade.xml.





Here is the modified code of res/menu/main.xml.



Here is the modified code of res/values/string.xml.

   Hello world!
   Zoom In/Out
   Clockwise/Anti Clockwise
   Fade In/Out

Here is the default code of AndroidManifest.xml.




Let's try to run your Animation application. I assume you have connected your actual Android Mobile device with your computer. To run the app from Eclipse, open one of your project's activity files and click Run Eclipse Run Icon icon from the toolbar. Before starting your application, Eclipse will display following window to select an option where you want to run your Android application.
Select your mobile device as an option and then check your mobile device which will display following screen:
Now just select the menu from your mobile, and a menu would appear which would be something like this:
Now just select the Zoom in , Zoom out option from menu and an animation would appear which would be something like this:
Now just select the clockwise option from menu and an animation would appear which would be something like this:
Now just select the fade in/out option from menu and an animation would appear which would be something like this:
Note: If you run it in emulator , you may not experience smooth animation effect. You have to run it in your android mobile in order to experience the smooth animation.


Post a Comment

Mithilesh Joshi