我想在我的应用程序中显示动画GIF图像。 我发现Android本身并不支持GIF动画。

但是它可以使用AnimationDrawable显示动画:

开发>指南>图像和图形>绘图概述

这个例子使用动画保存为框架的应用程序资源,但我需要的是直接显示动画gif。

我的计划是将动画GIF分解为帧,并将每帧添加为可绘制的AnimationDrawable。

有人知道如何从动画GIF中提取帧并将它们转换为可绘制的吗?


当前回答

有两个选项可以将动画gif加载到我们的Android应用程序中

1)使用Glide将gif加载到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)使用html加载gif到WebView

创建带有。gif文件地址的html:

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

将这个文件存储到assets目录中:

将这个html加载到应用程序的WebView中:

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

下面是这两个选项的完整示例。

其他回答

我解决了这个问题,在将gif动画保存到手机之前将其分割成帧,这样我就不必在Android中处理它了。

然后我把每一帧下载到手机上,从中创建Drawable,然后创建AnimationDrawable -非常类似于我的问题中的例子

为了节省资源,有滑翔库。 不知道为什么要使用其他任何东西,特别是webview只显示图像。 Glide是一个完美而简单的库,它可以从gif中准备动画,并将其直接放到imageview中。 gifdrawable句柄动画本身的逻辑。 Gif有lzw压缩原始rgb数据的动画里面。 没有理由复杂的使用webview和管理更多的文件,只显示一个gif文件在应用程序。

@PointerNull给出了很好的解决方案,但并不完美。它不能在一些大文件的设备上工作,并显示有bug的Gif动画与前ICS版本的delta帧。 我找到了没有这个bug的解决方案。它是带有原生解码到可绘制的库:koral的android-gif-drawable。

也放(main/assets/ htmlls /name.gif)[用这个HTML调整大小]

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

在你的Xml中像这样声明(main/res/layout/name.xml):[你定义大小,例如]

<WebView
android:layout_width="70dp"
android:layout_height="70dp"
android:id="@+id/webView"
android:layout_gravity="center_horizontal" />

在你的活动中,把下一个代码放在onCreate里面

web = (WebView) findViewById(R.id.webView); 
web.setBackgroundColor(Color.TRANSPARENT); //for gif without background
web.loadUrl("file:///android_asset/htmls/name.html");

如果你想动态加载,你必须用数据加载webview:

// or "[path]/name.gif" (e.g: file:///android_asset/name.gif for resources in asset folder), and in loadDataWithBaseURL(), you don't need to set base URL, on the other hand, it's similar to loadData() method.
String gifName = "name.gif";
String yourData = "<html style=\"margin: 0;\">\n" +
        "    <body style=\"margin: 0;\">\n" +
        "    <img src=" + gifName + " style=\"width: 100%; height: 100%\" />\n" +
        "    </body>\n" +
        "    </html>";
// Important to add this attribute to webView to get resource from outside.
webView.getSettings().setAllowFileAccess(true);

// Notice: should use loadDataWithBaseURL. BaseUrl could be the base url such as the path to asset folder, or SDCard or any other path, where your images or the other media resides related to your html
webView.loadDataWithBaseURL("file:///android_asset/", yourData, "text/html", "utf-8", null);
// Or if you want to load image from SD card or where else, here is the idea.
String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
webView.loadDataWithBaseURL(base + '/', yourData, "text/html", "utf-8", null);

建议:最好是加载gif与静态图像的更多信息,请检查https://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html

就是这样,我希望你能帮助我。

目前我们可以使用Glide https://github.com/bumptech/glide

在Android上显示动画GIF的方法:

电影类。正如上面提到的,它有很多bug。 WebView。使用起来很简单,而且通常很有效。但有时它开始表现不佳,而且总是在一些你没有的晦涩设备上。另外,您不能在任何类型的列表视图中使用多个实例,因为它会对内存产生影响。不过,您可以将其视为主要方法。 自定义代码解码gif为位图,并显示为Drawable或ImageView。我将提到两个库:

https://github.com/koral--/android-gif-drawable -解码器是用C语言实现的,因此非常高效。

https://code.google.com/p/giffiledecoder -解码器是在Java中实现的,所以它更容易使用。即使是大文件,仍然相当高效。

你还会发现许多基于GifDecoder类的库。这也是一个基于java的解码器,但它是通过将整个文件加载到内存中来工作的,因此它只适用于小文件。