-1

Hi I am looking to create gif animation in xml.I separated gif images and used the below code

myxm.xml

<?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/a" android:duration="100" />
 <item android:drawable="@drawable/c" android:duration="100" />
 <item android:drawable="@drawable/e" android:duration="100" />

</animation-list> 

and in my layout i used the code

 <ImageView
    android:id="@+id/imageButton1"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="16dp"
    android:src="@anim/myxm" />

But it doesn't works animation.Is it the right way to create gif animation in xml ? please help me thanks in advance :)

Maxwell
  • 552
  • 2
  • 5
  • 20

1 Answers1

0

You can try the following,

AnimationDrawable frameAnimation;
ImageView view;

    view = (ImageView) findViewById(R.id.imageButton1);
   // Setting myxm.xml as the background of the image view
    view.setBackgroundResource(R.anim.myxm);

        // Typecasting the Animation Drawable

        frameAnimation = (AnimationDrawable) view.getBackground();

        // 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();
                  }
            }
Spring Breaker
  • 8,233
  • 3
  • 36
  • 60