5.9 Animation
5.9 Animation
In android, Animations are used to change the appearance and behaviour of the objects over a particular interval of
time. The animations will provide a better look and feel high-quality user interface for our applications.
We have a different type of animations available in android, here we will discuss the most commonly used android
animations such as zoom in / zoom out, fade in / fade out, slide up / slide down and rotate clockwise or anti-clockwise and
move, etc.
Create XML File to Define Animation-
We need to create an XML file that defines the type of animation to perform in a new folder anim under res
directory (res -> anim -> animation.xml) with the required properties. In case, anim folder not exists in res directory, create a
new one.
Following is the example of creating XML files under anim folder to define slide up / down animation properties.
The XML files will contain the code like as shown below based on the type of
animation.
Attributes Description
android:duration It is used to define the duration of the animation to complete.
android:startOffset It is used to define the waiting time before the animation starts.
android:repeatCount It is used to define the number of times the animation repeats. In case if we
set infinite, the animation will repeat infinite times.
android:fillAfter It is used to define whether to apply animation transformation after the
animation completes or not
Android Load and Start the Animation
In android, we can perform animations by using AnimationUtils component methods such as loadAnimation().
Following is the code snippet of loading and starting an animation using loadAnimation() and startAnimation() methods.
Here we used another method startAnimation() to apply the defined animation to imageview object.
Different Types of Android Animations
In android, we have different types of animations such as Fade In / Fade Out, Zoom In / Zoom Out, Slide Up / Slide
Down , Rotate in Clockwise or Anti-Clockwise, etc.
Android Fade In / Out Animation-
In android, Fade In and Fade Out animations are used to change the appearance and behavior of the objects over a
particular interval of time. The Fade In and Fade Out animations will provide a better look and feel for our applications
To use Fade In or Fade Out animations in our android applications, we need to define a new XML file
with <alpha> tag like as shown below.
For Fade In animation, we need to increase the alpha value from 0 to 1 like as shown below.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/
android" android:interpolator="@android:anim/linear_interpolator">
<alpha
android:duration="2000"
android:fromAlpha="0.1"
android:toAlpha="1.0">
</alpha> </set>
For Fade Out animation, we need to decrease the alpha value from 1 to 0 like as shown below.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<alpha
android:duration="2000"
android:fromAlpha="1.0"
android:toAlpha="0.1" >
</alpha>
</set>
Android Slide Up / Down Animation
To use Slide Up or Slide Down animations in our android applications, we need to define a new XML file
with <scale> tag like as shown below.
For Slide Up animation, we need to set android:fromYScale="1.0" and android:toYScale="0.0" like as shown below.