18

Here is the code for xml:

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/minepic" />

Here the minepic is a gif animated image but after running the application its just showing a static image.

Is there any solution about how to animate the .gif images in android application?

Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
Danny
  • 1,050
  • 3
  • 11
  • 26

6 Answers6

37

To give a precise and complete answer here is what you need to do step wise:

  1. You would need to have different .png images which will act as frames for your animation. Save them in res/drawable folder.

  2. Create anim.xml file in res/drawable folder with following content:

    <?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/image_3" android:duration="250" />
        <item android:drawable="@drawable/image_2" android:duration="250" />
        <item android:drawable="@drawable/image_1" android:duration="250" />
        <item android:drawable="@drawable/image" android:duration="250" />
    
    </animation-list>
    
  3. In the layout xml file inside which you want to show the animation:

    //...
    <ImageView
         android:id="@+id/iv_animation"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_centerHorizontal="true"
         android:contentDescription="Animation" />
    //...
    
  4. In the Java file which loads the the layout xml file and calls setContentView:

    //...
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        final ImageView animImageView = (ImageView) findViewById(R.id.iv_animation);
        animImageView.setBackgroundResource(R.drawable.anim);
        animImageView.post(new Runnable() {
            @Override
            public void run() {
                AnimationDrawable frameAnimation =
                    (AnimationDrawable) animImageView.getBackground();
                frameAnimation.start();
            }
        });
        // ... other code ... 
    }
    // ...
    

In order to stop the animation you can call .stop() on the AnimationDrawable. For more details about the available methods, you can see AnimationDrawable documentation. Hope it helps someone.

Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • I don't know why the author didn't accept this. This solution is perfect, thank you. – Ali Elgazar Sep 14 '15 at 17:39
  • 1
    Great answer, but one suggestion: you don't have to post as a new Runnable() because you can't access the frameAnimation object thereafter, e.g. to stop the animation. See Google's sample code http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html – Kevin Lee Dec 23 '15 at 07:14
  • @kevinze You are right. It makes sense. I think if one needs it to loop the animation infinitely, then above sniplet would still work. Otherwise your suggestion is definitely what one should use if the animation needs to be stopped at some point or one needs access to the object outside the runnable. – Shobhit Puri Dec 23 '15 at 08:27
  • @ShobhitPuri and how do we stop the animation ? – Umair Jul 23 '16 at 09:39
  • 2
    @Umair You can use `.stop()` or can pre-define if it is supposed to loop only once or set the duration. See more about the methods here: https://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html – Shobhit Puri Jul 23 '16 at 21:28
14

I wouldn't recommend using Movie or WebView classes but ImageView instead with source drawable set to animation-list. Look at example(mydrawable.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/image_1" android:duration="100" />
<item android:drawable="@drawable/image_2" android:duration="100" />
<item android:drawable="@drawable/image_3" android:duration="100" />
</animation-list> 

// in your layout : <ImageView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:src="@drawable/my_drawable" />

Obviously, you have to slice your .gif into seperate images before using this solution.

Piotr
  • 1,743
  • 1
  • 26
  • 40
  • 2
    Wouldn't this require slicing the gif beforehand? – Gjordis Feb 22 '13 at 10:50
  • 1
    Yep, I assume that's possible for him. – Piotr Feb 22 '13 at 10:51
  • In the case it is definitely better approach. But when for example downloading a gif and showing that. It can be a problem. – Gjordis Feb 22 '13 at 10:57
  • Thanks @Piotr its great idea i just checked that but cant we animate the gif image directly without writing any `some.xml` and there we are defining the slices of the image of which we are trying to animate. – Danny Feb 22 '13 at 11:01
  • I've updated my answer to explain this straightforward. Thanks. Actually I wouldn't use downloaded gifs in Android applications. You can use WebView for that but it's just laggy and looks bad. – Piotr Feb 22 '13 at 11:03
  • @Danny I'm sorry but I don't think it's possible :( Android requires writing a lot of messy xml files. Consider accepting answer ;) – Piotr Feb 22 '13 at 11:04
  • yes @Piotr i will definitely accepts the answer because its also one of the way. – Danny Feb 22 '13 at 11:07
  • @Danny you have to check green tick in order to do that ;) – Piotr Feb 22 '13 at 16:22
  • @Piotr i'm not getting the right answer if at all i wont get then definitely i make a green tick for ur answer. :) – Danny Feb 25 '13 at 11:41
  • The animation must use AnimationDrawable class to start itself. – Sea turtle May 28 '13 at 08:45
  • 2
    it is not animating unless unless using AnimationDrawable. Any way to just use xml? – ken Jul 25 '13 at 07:33
  • I'm trying to use this method as well but it gives me error on the animation xml file. It says element animation list on must be declared, I don't even know what to declare now. If you guys could point me a hint that would be so helpful. – Hendra Anggrian Nov 03 '13 at 11:23
2

As far as I understand, your GIF image is not moving, so that's the native Android behaviour if you treat GIF like a static picture. For me the best solution (not to reinvent the wheel!) was the open-source library gitDrawable. Check their README, everything is very simple: add dependency to gradle and use(in XML or code). Example of usage in Java:

GifDrawable gifFromResource = new GifDrawable( getResources(), R.drawable.anim );
torina
  • 3,825
  • 2
  • 26
  • 31
0
And i think this is the one way of solution by writing the anim-space.xml 
In which the slices of the image are added to the items one by one and setting that xml to the imageview of our layout xml.

http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html

In this developers link it is defined clearly.

Danny
  • 1,050
  • 3
  • 11
  • 26
0

Well i was able to animate android images using this simple code:

canvas.drawColor(Color.WHITE);
super.onDraw(canvas);


long now=android.os.SystemClock.uptimeMillis();
System.out.println("now="+now);
if (moviestart == 0) { // first time
moviestart = now;

}

System.out.println("\tmoviestart="+moviestart);
int relTime = (int)((now - moviestart) % movie.duration()) ;
 System.out.println("time="+relTime+"\treltime="+movie.duration());
movie.setTime(relTime);
movie.draw(canvas,this.getWidth()/2-20,this.getHeight()/2-40);
this.invalidate();
 }


it was implemented easily using movieview

or i can i give you a tutorial for this stuff

dondondon
  • 881
  • 8
  • 4
0

It's not good practice to use Threads (i.e. View.post(new Runnable)) because the view might have changed during the time the drawable is going to be painted (one case is using the animated image on ListView with items containing different background images), which may cause a ClassCastException if the ImageView, by the time the thread runs, has a background that is not an animated resource.

ImageView loadingImg = (ImageView)v.findViewById(R.id.image);
loadingImg.setBackgroundResource(R.drawable.progressdialog);
loadingImg.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
  @Override
  public void onViewAttachedToWindow(View v) {
    AnimationDrawable loadingAnimation = (AnimationDrawable) v.getBackground();
    loadingAnimation.start();
  }

  @Override
  public void onViewDetachedFromWindow(View v) {
  }
});

Example shown here

Community
  • 1
  • 1
Carlos Barcellos
  • 544
  • 1
  • 4
  • 10