Total Pageviews

Tuesday, 16 July 2013

Android Frames By Frames 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.
<?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

<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 :-


Here we are done with the xml part, see the image below for reference :-
Frame animation in android

Step 3- Outside the onCreate method :


Declare the Image View and Animation Drawable
// 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
// 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.
// 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();
      }
}
So the final result will look like this :-
Frame animation in android Frame animation in android Frame animation in android




No comments:

Post a Comment