Saturday, 28 December 2013

Frame Animation in Android

Animation….. What is that?
It’s been said that Animation is the state of being alive. Something that gives you an illusion. In mobile application development too this has got its own significance. In this blog we will go through, how to use Frame Animation in android and the art of illusion.

In Android Frame Animation, you will be swapping frames repeatedly, so that it appears continuous to the human eye and we feel that it is animated. Frame is referred to an image. So to implement frame by frame animation in android, one needs to have set of images, which describes a motion.
Now let’s move on and see how to implement this using frame animation

Step 1- Create a drawable folder.


Within it create an animation_list.xml file.
It includes :
A list of items that has the addresses of the frame images.
1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
<item android:drawable="@drawable/blank" android:duration="210" />
<item android:drawable="@drawable/logo" android:duration="210" />
<item android:drawable="@drawable/logo1" android:duration="210" />
<item android:drawable="@drawable/logo2" android:duration="210" />
<item android:drawable="@drawable/logo3" android:duration="210" />
<item android:drawable="@drawable/logo4" android:duration="210" />
<item android:drawable="@drawable/logo5" android:duration="210" />
<item android:drawable="@drawable/logo6" android:duration="210" />
<item android:drawable="@drawable/logofinal" android:duration="210" />
</animation-list>

Step 2- Create an activity_main.xml file


It Includes : An Image View
1
2
3
4
5
6
7
8
9
10
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <ImageView
        android:id="@+id/imageAnimation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true" />
</RelativeLayout>
Here we are done with the xml part, see the image below for reference :-


Step 3- Outside the onCreate method :


Declare the Image View and Animation Drawable
1
2
3
// Declaring an Image View and an Animation Drawable
ImageView view;
AnimationDrawable frameAnimation;

Step4- Inside the OnCreate method:

  • Typecast the Image view
  • Typecast the Animation Drawable
  • Set the drawable background on the image view
1
2
3
4
5
6
// Typecasting the Image View
view = (ImageView) findViewById(R.id.imageAnimation);
// Setting animation_list.xml as the background of the image view
view.setBackgroundResource(R.drawable.animation_list);
// Typecasting the Animation Drawable
frameAnimation = (AnimationDrawable) view.getBackground();

Step5- After the onCreate method :


The animation should only run when it is in focus that is when it is visible to the user. Hence define this method after the onCreate method.
1
2
3
4
5
6
7
8
9
10
11
12
// Called when Activity becomes visible or invisible to the user
@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
      if (hasFocus) {
    // Starting the animation when in Focus
    frameAnimation.start();
    } else {
        // Stoping the animation when not in Focus
    frameAnimation.stop();
      }
}

No comments:

Post a Comment