About Francesco Azzola

I'm a senior software engineer with more than 15 yrs old experience in JEE architecture. I'm SCEA certified (Sun Certified Enterprise Architect), SCWCD, SCJP. I'm an android enthusiast and i've worked for long time in the mobile development field.

Animation Resources and AnimationListener

In this post i want to analyze how we can use animation in Android and how we can implements an AnimationListener. In particular i want to describe the tween animation that are the most useful ones. Tween animation can be diveded in four groups:

  • translate – Move a View along X and Y axis
  • rotate – Rotate a View
  • scale – Scale a View
  • fade – Fade effect on a view

Even if it is quite trivial to create an animation, i found a bit confusing how set animation parameter. One thing we have to know before digging into the details is in Android animations can be described using XML. To do it, we have to create a folder under res directory called anim. I won’t dig into the parameter details (you can read them in API Doc) rather i prefer to show a bit more how to animate a View considering some aspects about X and Y axis.

In general we can say that there three different way to measure and indicate how a View can move:

  • absolute value (ranging from –100 to 100)
  • relative to the view (ranging from –100% to 100%)
  • relative to its parent (ranging from –100%p to 100%p)

Let’s suppose, for simplicity, we want to animate a TextView so we can create a layout like this one:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent" >

        tools:context=".MainActivity" />


This TextView is placed upper left side of then screen. Let’s suppose we want to move it to the right, to do it we create a file called move_to_right_anim.xml like this one:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"

In this file we simply tell to the Android OS that X as to move from 0% to 100%p (so it means to the parent right side) and that this animation has to last for 800ms. Now what’s the interpolator? Interpolator defines how to movement takes place. In this case we say that the View has to move faster while it reaches the right side.

Let’s suppose we want our TextView Falls from the top to the middle of the screen with a bouncing effect. Well this is very simply we need just to modify the xml animation file. Let’s call it fall_bounce_anim.xml. In this case we want that the animation takes place along Y axis moving from 0%p to 50%p while along X axis we want to place the TextView in the middle of the screen so 50%p.

So the XML file is:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"

In this case the duration is 1000ms so 1 sec.

Let’s suppose now that our TextView is placed in the middle of the screen and we want to move it up. In this case the “origin” of our animations is the middle of the screen

then the XML file looks like:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"

In other words we have to move up of –50%p.

Another interesting animation type is rotation. In this case we have to specify the rotation angle expressed in degrees. In this case two parameter pivotX and pivotY specify the coordinates of the rotation point. If we want, for example, that the rotation takes place around the middle point of our view we have simply to specify the pivotX and pivotY equals to 50%. XML is like this:

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"

You can try other kind of animation, like fading and so son and other type of interpolators.

How to start the Animation

Once we have defined our animation using the XML we have to “apply” this animation to the view and then starts it. The first thing we need to do is loading the animation. This can be done using an helper class called AnimationUtil. For example, to load the rotate animation we need to write

Animation anim = AnimationUtils.loadAnimation(this, R.anim.rotate_anim);

>where this is the context. The we simply start the activity using the method startActivity passing as parameter the animation loaded (anim).

 public void onCreate(Bundle savedInstanceState) {

     final TextView tv = (TextView) findViewById(R.id.logo);
     //Animation anim = AnimationUtils.loadAnimation(this, R.anim.move_to_right_anim);
     //Animation anim = AnimationUtils.loadAnimation(this, R.anim.fall_bounce_anim);
     final Animation anim = AnimationUtils.loadAnimation(this, R.anim.rotate_anim);


Animation listener

One interesting aspect of animation is the capability to add a listener, AnimationListener. Implementing this listener we can get notification when animation starts ( onAnimationStart(Animation animation) ) , repeat ( onAnimationRepeat(Animation animation) ) and ends ( onAnimationEnd(Animation animation) ). In this method we can implements our logic.

For example let’s suppose we want animate a TextView when an user clicks on it and then starts another Activity. The first thing we need is to catch the click event, this is trivial:

tv.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        // We implement our logic


Remember that tv is the TextView reference retrieved by findViewById() method. Now in the method we simply starts the animation and then we implement the AnimationListener like that:

tv.setOnClickListener(new View.OnClickListener() {

 public void onClick(View v) {
     anim.setAnimationListener(new Animation.AnimationListener() {

         public void onAnimationStart(Animation animation) {
             // TODO Auto-generated method stub


         public void onAnimationRepeat(Animation animation) {
             // TODO Auto-generated method stub


         public void onAnimationEnd(Animation animation) {
             // We start the Activity



In the onAnimationEnd method that is called at the end of our animation we start our new Activity.

Reference: Animation Resources and AnimationListener from our JCG partner Francesco Azzola at the Surviving w/ Android blog.
Related Whitepaper:

Rapid Android Development: Build Rich, Sensor-Based Applications with Processing

Create mobile apps for Android phones and tablets faster and more easily than you ever imagined

Use 'Processing', the free, award-winning, graphics-savvy language and development environment, to work with the touchscreens, hardware sensors, cameras, network transceivers, and other devices and software in the latest Android phones and tablets.

Get it Now!  

Leave a Reply

6 − = one

Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below: