Java Tutorials for Beginners and Professionals

Animations with Android tutorial

This tutorial explains what animation is and how to use animations in your Android application with the help of an example.

Animations

Do you know what animations are? Animation is actually illusion of motion. It is a technique in which successive images which differs very lightly creates effect of motion. Animations are very useful in mobile applications. Animation in education is used to explain theories and concepts. Animations in applications in used to show some guidelines or procedures. Adding animations to Android application increases user additivity. It adds fun and smooths the user experience. In this tutorial you will learn how to increase user experience by adding animations in your Android app.

Animation in Android

Android provides a class called Animations. This class provides many different methods. Here are few of them:

loadAnimation( context, layout): This method is used to load animation. It has two parameters.

start(): Used to start animation.

setDuration( long duration): This method sets duration of animation in Android.

getDuration( ): Used to get duration of Android animation.

end(): Used to end animation.

cancel(): Used to cancel animation.

Animation example in Android

Lets start creating example for animation in Android. I will discuss four types of animations that is blink, fade, clockwise and slide. Open your Android Studio and create a new activity. In main activity there is one image view and a button. I used image of a football. Here is the code for activity_main.xml

When user clicks on animation button first time that is count=0 then the image will rotate clockwise. When user clicks second time that is count=1, then the image will fade. When user clicks for the third time that is count=2 then image will start blinking. And for the fourth time that is count=3 then the image will show slide animation. Now run this and here is the output