Android Animations – Quick Guide

A quick guide in the world of animations in Android from Chandan, one of our JCG partners.

(NOTE: The original post has been slightly edited to improve readability)

Setting animations to a layout programmatically is described below. Here we go:

Create an XML file named for example “bottom_out.xml” and place it in a folder called “anim” inside the “res” folder. The contents of the “bottom_out.xml” file should be as follows:

<?xml version="1.0" encoding="utf-8"?>

The values indicate the percentage relative to the parent layout. An Interpolator is used in order to indicate how the animation should proceed. It essentially defines the rate of change of an animation. This allows the basic animation effects (alpha, scale, translate, rotate) to be accelerated, decelerated, repeated, etc. For example, here we use a AccelerateInterpolator, thus the animation will be accelerated during the end. Another interpolator we could use is the LinearInterpolator, where the rate of change is constant.

Let’s see how this can be used inside an Android class.

private void startAnimationPopOut() {         
    LinearLayout myLayout = (LinearLayout) findViewById(;

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

    animation.setAnimationListener(new AnimationListener() {                  
        public void onAnimationStart(Animation animation) {


        public void onAnimationRepeat(Animation animation) {


        public void onAnimationEnd(Animation animation) {




The “myLayout” variable (pointing to is a simple LinearLayout defined in xml file under “layout” folder. The AnimationUtils class can be used to load an Animation object from a resource. We also can attach AnimationListeners and be notified in the various stages of the animation. Note that it is generally a good practice to clear any previously set animations prior to set new animation, as shown in the end of the snippet.

Below are some of the samples I would like to share:

Sinking in:


Slide from left side:


Slide from right side:


Rotate from 0 to 180 degree:


Here pivotX and PivotY are the x and y coordinates of center around which you wish the view to rotate.

Scale (here it is zooming in):


Here 1.0 implies original view size; as the pivot is set 0, the view will be scaled taking its origin as the center.

Find more in the Animation Resources page. Happy coding!

Related Articles:

Do you want to know how to develop your skillset to 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!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply

1 × = six

Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
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: