Animations

Android supports three types of animation:
- Frame-by-frame animation
A series of frames is drawn one after the other at regular intervals by loading a series of Drawable resources one after the other. - Layout animation
Animate views inside a container view such as lists and tables. - View animation
Animate any general-purpose view.
The types #2 and #3 are a type of tweeting animation which involves the drawing in between the key drawings. The idea is that knowing the beginning state and ending state of a drawing allows us to vary certain aspect of the drawing in time. This varying aspect could be color, position, size, rotation, and so on. In other words, we tell Android to perform a series of simple transformations to the content of a View.
Both animation types can be used in any View object to provide simple rotating timers, activity icons, and other useful UI elements. Tweened animation is handled by android.view.animation. Frame-by-frame animation is handled by the AnimationDrawable class.
- Frame –by- frme animation :
In Android, we can get frame-by-frame animation through a class in the graphics package called AnimationDrawable. We can tell from its name that it is like any other drawable that can work as background for any view. For example the background bitmaps are represented as Drawables.
The class AnimationDrawable, in addition to being a Drawable, can take a list of other Drawable resources like images and render them at specified intervals. This class is really a thin wrapper around the animation support provided by the basic Drawable class.
To make use of the AnimationDrawable class, let’s start with a set of Drawable resources in the /res/drawable subdirectory.
Example :
main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout> frame_animations_layout.xml <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/textViewId1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/frame"/> <Button android:id="@+id/startFAButtonId" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/start"/> <ImageView android:contentDescription="@string/desc" android:id="@+id/animationImage" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> |
frame_animation.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/d1" android:duration="50" /> <item android:drawable="@drawable/d2" android:duration="50" /> <item android:drawable="@drawable/d3" android:duration="50" /> <item android:drawable="@drawable/d4" android:duration="50" /> <item android:drawable="@drawable/d5" android:duration="50" /> <item android:drawable="@drawable/d6" android:duration="50" /> <item android:drawable="@drawable/d7" android:duration="50" /> <item android:drawable="@drawable/d8" android:duration="50" /> <item android:drawable="@drawable/d9" android:duration="50" /> <item android:drawable="@drawable/d10" android:duration="50" /> </animation-list> |
FrameAnimation.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
public class FrameAnimation extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.frame_animations_layout); this.setupButton(); } private void setupButton() { Button b = (Button)this.findViewById(R.id.startFAButtonId); b.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { parentButtonClicked(v); } }); } private void parentButtonClicked(View v) { animate(); } private void animate() { ImageView imgView = (ImageView)findViewById(R.id.animationImage); //imgView.setVisibility(ImageView.VISIBLE); imgView.setBackgroundResource(R.drawable.frame_animation); AnimationDrawable frameAnimation =(AnimationDrawable) imgView.getBackground(); frameAnimation.start(); /*if (frameAnimation.isRunning()) { frameAnimation.stop(); } else { frameAnimation.stop(); frameAnimation.start(); }*/ } } |
- Layout Animation
- We’ll use layout animation with the ListView and GridView, which are the two most commonly-used controls in Android. Specifically, we’ll use layout animation to add visual effects to the way each item in a ListView or GridView is displayed. Actually, we can use this type of animation on all controls derived from a ViewGroup.
- Layout animation works by applying tweening to each view that is part of the layout being animated. Here are four types of tweening animation:
- Scale animation (growing or shrinking) : We use this type of animation to make a view smaller or larger either on x axis or on the y axis. We can also specify the pivot point around which we want the animation to take place.
- Rotate animation (rotations): We use this to rotate a view around a pivot point by a certain number of degrees.
- Translate animation (position changes): We use this to move a view along the x or y axis.
- Alpha animation (transparency changes): We use this to change the transparency of a view.
Once we have a ListView, we can attach an animation to it so that each list item will go through that animation.
We can define both the individual animation and the mediator in XML files in the /res/anim subdirectory. Once we have the mediator XML file, we can use that file as an input to the ListView in its own XML layout definition.
main.xml
1 2 3 4 5 6 7 8 9 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout> |
list_layout.xml
1 2 3 4 5 6 7 8 9 10 11 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ListView android:id="@+id/list_view_id" android:persistentDrawingCache="animation|scrolling" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layoutAnimation="@anim/list_layout_controller" /> </LinearLayout> |
list_layout_controller.xml
1 2 3 4 |
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android" android:delay="30%" android:animationOrder="reverse" android:animation="@anim/translate_alpha" /> |
alpha.xml
1 2 3 4 5 |
<alpha 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="5000" /> |
rotation.xml
1 2 3 4 5 6 7 |
<rotate xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:fromDegrees="0.0" android:toDegrees="360" android:pivotX="70%" android:pivotY="70%" android:duration="5000" /> |
scale.xml
1 2 3 4 5 6 7 8 9 10 11 12 |
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"> <scale android:fromXScale="1" android:toXScale="1" android:fromYScale="0.1" android:toYScale="1.0" android:duration="1000" android:pivotX="50%" android:pivotY="50%" android:startOffset="100" /> </set> |
translate_alpha.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"> <translate android:fromYDelta="-100%" android:toYDelta="0" android:duration="500" /> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="1000" /> </set> LayoutAnimation.java public class LayoutAnimation extends Activity { @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.list_layout); setupListView(); } private void setupListView(){ String[] listItems = new String[] { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6", }; ArrayAdapter<String> listItemAdapter = new ArrayAdapter<String>(this ,android.R.layout.simple_list_item_1 ,listItems); ListView lv = (ListView)this.findViewById(R.id.list_view_id); lv.setAdapter(listItemAdapter); } } |
Now that we have the layout needed for the activity, we can write the code for the activity to load the layout file so we can generate our UI.
Activity_Main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/txtMessage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is fadeout animation" android:layout_centerInParent="true" android:textSize="25dp"/> <Button android:id="@+id/btnStart" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start Animation" android:layout_marginTop="30dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="20dp"/> </RelativeLayout> Create one xml file in drawable folder withbelow code Anim.xml <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true" > <alpha android:duration="1000" android:fromAlpha="1.0" android:interpolator="@android:anim/accelerate_interpolator" android:toAlpha="0.0" /> </set> MainActivity.java package com.example.animation; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.animation.Animation; import android.view.animation.Animation.AnimationListener; import android.view.animation.AnimationUtils; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity implements AnimationListener { TextView txtMessage; Button btnStart; // Animation Animation animFadeOut; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtMessage = (TextView) findViewById(R.id.txtMessage); btnStart = (Button) findViewById(R.id.btnStart); // load the animation animFadeOut = AnimationUtils.loadAnimation(getApplicationContext(), R.drawable.anim); // set animation listener animFadeOut.setAnimationListener(this); // button click event btnStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // start the animation txtMessage.startAnimation(animFadeOut); } }); } @Override public void onAnimationEnd(Animation animation) { // Take any action after completing the animation // check for fade out animation if (animation == animFadeOut) { Toast.makeText(getApplicationContext(), "Animation Stopped", Toast.LENGTH_SHORT).show(); } } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationStart(Animation animation) { // TODO Auto-generated method stub } } |