Sunday, 8 December 2013

GIF Animation in Android

Hi!

 It is to hard for doing animation in android, because we can't call directly any animations including .gif.

But we can over come it by using the fallowing way

Step-1

decompress your gif file as screens

Step-2

write an xml file for animating the gif screens like



<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:drawable="@drawable/g_gm_zero"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_one"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_two"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_three"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_four"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_five"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_six"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_seven"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_eight"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_nine"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_ten"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_eleven"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_twelve"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_thirteen"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_fourteen"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_fifteen"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_sixteen"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_seventeen"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_eighteen"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_nineteen"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_twenty"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_twenty_one"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_twenty_two"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_twenty_three"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_twenty_four"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_twenty_five"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_twenty_six"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_twenty_seven"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_twenty_eight"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_twenty_nine"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_thirty"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_thirty_one"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_thirty_two"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_thirty_three"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_thirty_four"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_thirty_five"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_thirty_six"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_thirty_seven"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_thirty_eight"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_thirty_nine"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_forty"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_forty_one"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_forty_two"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_forty_three"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_forty_four"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_forty_five"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_forty_six"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_forty_seven"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_forty_eight"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_forty_nine"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_fifty"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_fifty_one"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_fifty_two"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_fifty_three"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_fifty_four"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_fifty_five"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_fifty_six"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_fifty_seven"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_fifty_eight"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_fifty_nine"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_sixty"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_sixty_one"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_sixty_two"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_sixty_three"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_sixty_four"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_sixty_five"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_sixty_six"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_sixty_seven"
        android:duration="100"/>
    <item
        android:drawable="@drawable/g_gm_sixty_eight"
        android:duration="100"/>

</animation-list>



Step-3

    write your activity class for load and run the animation,


  • crate an object for AnimationDrawable class,
  • Apply the animation on your components (here i am using ImageButton),
  • Start & stop the animation depends on windows focusing action (because if not on windowsfocus listener the animation will affect the processor speed)
Activity class

package dcs.raj.entertainment;

import dcs.raj.restaurant.R;
import android.os.Bundle;
import android.widget.ImageButton;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;

public class Entertainment extends Activity {

private ImageButton img_btn_game;

private AnimationDrawable frm_ani_game;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_entertainment);
initSelva();
}

public void initSelva() {

img_btn_game = (ImageButton) findViewById(R.id.entern_game);
img_btn_game.setBackgroundResource(R.drawable.a_game);
frm_ani_game = (AnimationDrawable) img_btn_game.getBackground();
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
frm_ani_game.start();

} else {
frm_ani_game.stop();
}
}

@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
}
}


Thank you!


Please Leave Your Comment..,


Have A Happy Day..,

No comments:

Post a Comment