Display Animated GIF

2018-12-31 04:58发布

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?

29条回答
情到深处是孤独
2楼-- · 2018-12-31 05:08

Ways to show animated GIF on Android:

  • Movie class. As mentioned above, it's fairly buggy.
  • WebView. It's very simple to use and usually works. But sometimes it starts to misbehave, and it's always on some obscure devices you don't have. Plus, you can’t use multiple instances in any kind of list views, because it does things to your memory. Still, you might consider it as a primary approach.
  • Custom code to decode gifs into bitmaps and show them as Drawable or ImageView. I'll mention two libraries:

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.

查看更多
听够珍惜
3楼-- · 2018-12-31 05:08

There are two options to load animated gifs into our Android apps

1)Using Glide to load the gif into an ImageView.

    String urlGif = "https://cdn.dribbble.com/users/263558/screenshots/1337078/dvsd.gif";
    //add Glide implementation into the build.gradle file.
    ImageView imageView = (ImageView)findViewById(R.id.imageView);
    Uri uri = Uri.parse(urlGif);
    Glide.with(getApplicationContext()).load(uri).into(imageView);

2) Using an html to load the gif into a WebView

Create the html with the address to the .gif file:

<html style="margin: 0;">
<body style="margin: 0;">
<img src="https://..../myimage.gif" style="width: 100%; height: 100%" />
</body>
</html>

store this file into the assets directory:

enter image description here

The load this html into the WebView of your application:

    WebView webView =  (WebView)findViewById(R.id.webView);
    webView = (WebView) findViewById(R.id.webView);
    webView.loadUrl("file:///android_asset/html/webpage_gif.html");

Heres is a complete example of this two options.

enter image description here

查看更多
梦寄多情
4楼-- · 2018-12-31 05:08

Just wanted to add that the Movie class is now deprecated.

This class was deprecated in API level P.

It is recommended to use this

AnimatedImageDrawable

Drawable for drawing animated images (like GIF).

查看更多
公子世无双
5楼-- · 2018-12-31 05:10

I have had success with the solution proposed within this article, a class called GifMovieView, which renders a View which can then be displayed or added to a specific ViewGroup. 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.

查看更多
还给你的自由
6楼-- · 2018-12-31 05:10

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:

<com.whygraphics.gifview.gif.GIFView xmlns:gif_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/main_activity_gif_vie"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:scaleType="center"
        gif_view:gif_src="url:http://pop.h-cdn.co/assets/16/33/480x264/gallery-1471381857-gif-season-2.gif" />

In the activity:

    GIFView mGifView = (GIFView) findViewById(R.id.main_activity_gif_vie);

    mGifView.setOnSettingGifListener(new GIFView.OnSettingGifListener() {
                @Override
                public void onSuccess(GIFView view, Exception e) {
                    Toast.makeText(MainActivity.this, "onSuccess()", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onFailure(GIFView view, Exception e) {

        }
});

Setting the gif programmatically:

mGifView.setGifResource("asset:gif1");
查看更多
ら面具成の殇う
7楼-- · 2018-12-31 05:10

You may use GifAnimationDrawable library found in this link - https://github.com/Hipmob/gifanimateddrawable, and which convert any gif to AnimationDrawable. Enjoy :)

查看更多
登录 后发表回答