Tutorial details

Animation Resources and AnimationListener | App Code for Sale | Preview

Animation Resources and AnimationListener | Android Tutorial

How we can use animation in Android and how we can implements an AnimationListener

Overview PAGE TOP

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:

01  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
02      xmlns:tools="http://schemas.android.com/tools"
03      android:layout_width="match_parent"
04      android:layout_height="match_parent" >
06      <TextView
07          android:id="@+id/logo"
08          android:layout_width="wrap_content"
09          android:layout_height="wrap_content"
10          android:text="S/A"
11          tools:context=".MainActivity" />
13  </RelativeLayout>

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 movetoright_anim.xml like this one:

1   <?xml version="1.0" encoding="utf-8"?>
2   <translate xmlns:android="http://schemas.android.com/apk/res/android"
3              android:fromXDelta="0%"
4              android:toXDelta="100%p"
5              android:duration="800"
6              android:interpolator="@android:anim/accelerate_interpolator">
7   </translate>

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 fallbounceanim.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:

1   <?xml version="1.0" encoding="utf-8"?>
2   <translate xmlns:android="http://schemas.android.com/apk/res/android"
3              android:fromXDelta="50%p"
4              android:toXDelta="50%p"
5              android:fromYDelta="0%p"
6              android:toYDelta="50%p"
7              android:duration="1000"
8              android:interpolator="@android:anim/bounce_interpolator">
9   </translate>

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:

1   <?xml version="1.0" encoding="utf-8"?>
2   <translate xmlns:android="http://schemas.android.com/apk/res/android"
3              android:fromXDelta="0%p"
4              android:toXDelta="0%p"
5              android:fromYDelta="0%p"
6              android:toYDelta="-50%p"
7              android:duration="1000"
8              android:interpolator="@android:anim/bounce_interpolator">
9   </translate>

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:

1   <?xml version="1.0" encoding="utf-8"?>
2   <rotate xmlns:android="http://schemas.android.com/apk/res/android"
3           android:interpolator="@android:anim/cycle_interpolator"
4           android:fromDegrees="0"
5           android:toDegrees="360"
6           android:pivotX="50%"
7           android:pivotY="50%"
8           android:duration="1000"/>

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

1   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).

01  @Override
02   public void onCreate(Bundle savedInstanceState) {
03       super.onCreate(savedInstanceState);
04       setContentView(R.layout.activity_main);
06       final TextView tv = (TextView) findViewById(R.id.logo);
07       //Animation anim = AnimationUtils.loadAnimation(this, R.anim.move_to_right_anim);
08       //Animation anim = AnimationUtils.loadAnimation(this, R.anim.fall_bounce_anim);
09       final Animation anim = AnimationUtils.loadAnimation(this, R.anim.rotate_anim);
10       tv.startAnimation(anim);
12    ......
13  }

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:

1   tv.setOnClickListener(new View.OnClickListener() {
3       @Override
4       public void onClick(View v) {
5           // We implement our logic
7       }
8   });

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:

01  tv.setOnClickListener(new View.OnClickListener() {
03   @Override
04   public void onClick(View v) {
05       tv.startAnimation(anim);
06       anim.setAnimationListener(new Animation.AnimationListener() {
08           @Override
09           public void onAnimationStart(Animation animation) {
10               // TODO Auto-generated method stub
12           }
14           @Override
15           public void onAnimationRepeat(Animation animation) {
16               // TODO Auto-generated method stub
18           }
20           @Override
21           public void onAnimationEnd(Animation animation) {
22               // We start the Activity
24           }
25       });
27   }
28   });

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

Reference PAGE TOP


0 Comments Leave a comment

Please login in order to leave a comment.

Newest first

to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.