Android: How to show animation

Android has nice  animation functionality for views. The package for animation is android.view.animation, and new package is android.animation. You can create move, scale, rotate, and fade in/out any type of animation to views. Also these animations can be combined altogether to make more interactive animation effect using animation set.

Here you will find an example to create an animation set to move a text view with alpha effect from right to left. How it works is given below, assuming that you can develop simple android application:

  • Create an animation layout controller xml file in …/res/anim/layout_anim_controller.xml

<!--?<span class="hiddenSpellError" pre=""-->xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="50%"
android:animation="@anim/layout_right_to_left_slide" />
  • Create an animation set xml file in …/res/anim/layout_right_to_left_slide.xml

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

<translate
android:fromXDelta="100%p"
android:toXDelta="0"
android:duration="10000" />

xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="10000" />

</set>

  • Create an activity such as AnimationActivity
  • Set animation effect on views in activity using AnimationUtils

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;

public class <strong>AnimationActivity </strong>extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ViewGroup layout = (ViewGroup) findViewById(R.id.mainLayout);
setLayoutAnimation(layout, this);
}
public static void setLayoutAnimation(ViewGroup panel, Context ctx)
{
LayoutAnimationController controller = AnimationUtils.loadLayoutAnimation(ctx, R.anim.layout_anim_controller);
panel.setLayoutAnimation(controller);
}
}

  • Now start the application you will see animated text view
  • Output is as following, text view is moving from right to left:

    android_animation

    android animation

You can create more complex animation effect using the animation set that are provided in android platform. Have a  creative animated journey to android animation world. 🙂

References:

Advertisements

About M Moniruzzaman
A passionate software engineer, have been developing applications on various platforms such as Android, iPhone, .Net (C#) technologies and web based ASP.NET, PHP, JavaScript, jQuery technologies for more than 10 years. Especially I have expertise on developing applications for Android and iPhone, as well as service oriented, client-server based applications where clients will be reside on Android/iPhone that communicate with WCF(.NET) service hosted on server. I have completed certification in Microsoft Certified Professional Developer (MCPD) on .Net 4 . I have completed my graduation in -- B.Sc. (Engineering) in Computer Science and Engineering, ShahJalal University of Science and Technology, Bangladesh. Thanks, M. Moniruzzaman (Zaman)

2 Responses to Android: How to show animation

  1. Pingback: Android: How to show animation | PDA8

  2. subburaj says:

    thats works fine to me. i used that as slide up animations for the view in the activity.
    i had two activities A and B. i done the animation to the view in the activity B . now i press back means return to activity A with out an animation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: