I want to display animated GIF images in my aplication. As I found out the hard way Android doesn't support animated GIF natively.
However it can display animations using AnimationDrawable:
Develop > Guides > Images & Graphics > Drawables Overview
The example uses animation saved as frames in application resources but what I need is to display animated gif directly.
My plan is to break animated GIF to frames and add each frame as drawable to AnimationDrawable.
Does anyone know how to extract frames from animated GIF and convert each of them into Drawable?
Ways to show animated GIF on Android:
https://github.com/koral--/android-gif-drawable - decoder is implemented in C, so it's very efficient.
https://code.google.com/p/giffiledecoder - decoder is implemented in Java, so it's easier to work with. Still reasonably efficient, even with large files.
You'll also find many libraries based on GifDecoder class. That's also a Java-based decoder, but it works by loading the entire file into memory, so it's only applicable to small files.
There are two options to load animated gifs into our Android apps
1)Using Glide to load the gif into an
ImageView
.2) Using an html to load the gif into a
WebView
Create the html with the address to the .gif file:
store this file into the assets directory:
The load this html into the WebView of your application:
Heres is a complete example of this two options.
Just wanted to add that the Movie class is now deprecated.
It is recommended to use this
AnimatedImageDrawable
I have had success with the solution proposed within this article, a class called
GifMovieView
, which renders aView
which can then be displayed or added to a specificViewGroup
. Check out the other methods presented in parts 2 and 3 of the specified article.The only drawback to this method is that the antialiasing on the movie is not that good (must be a side-effect of using the "shady" Android
Movie
Class). You are then better off setting the background to a solid color within your animated GIF.Something I did for showing gifs in apps. I extended ImageView so people can use its attributes freely. It can show gifs from url or from the assets directory. The library also makes it easy for extending classes to inherit from it and extend it to support different methods to initialize the gif.
https://github.com/Gavras/GIFView
There's a little guide on the github page.
It was also published on Android Arsenal:
https://android-arsenal.com/details/1/4947
Use example:
From XML:
In the activity:
Setting the gif programmatically:
You may use GifAnimationDrawable library found in this link - https://github.com/Hipmob/gifanimateddrawable, and which convert any gif to AnimationDrawable. Enjoy :)