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

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

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

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

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

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


当前回答

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

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

其他回答

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

首先,Android浏览器应该支持动画gif。如果没有,那就是bug!看一下问题跟踪器。

如果你在浏览器外显示这些动画gif,情况可能就不一样了。要做到你所要求的,将需要外部库,支持动画gif解码。

第一个接口是Java2D或JAI (Java Advanced Imaging) API,如果Android Dalvik在你的应用程序中支持这些库,我会感到非常惊讶。

把它放到WebView中,它必须能够正确地显示它,因为默认浏览器支持gif文件。(Froyo+,如果我没记错的话)

用壁画。以下是如何做到这一点:

http://frescolib.org/docs/animations.html

以下是示例的回购:

https://github.com/facebook/fresco/tree/master/samples/animation

当心壁画不支持包装内容!

仅android API (android Pie)28和+使用AnimatedImageDrawable作为

// ImageView from layout
val ima : ImageView = findViewById(R.id.img_gif)
// create AnimatedDrawable 
val decodedAnimation = ImageDecoder.decodeDrawable(
        // create ImageDecoder.Source object
        ImageDecoder.createSource(resources, R.drawable.tenor))
// set the drawble as image source of ImageView
ima.setImageDrawable(decodedAnimation)
// play the animation
(decodedAnimation as? AnimatedImageDrawable)?.start()

XML代码,添加一个ImageView

<ImageView
    android:id="@+id/img_gif"
    android:background="@drawable/ic_launcher_background" <!--Default background-->
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_width="200dp"
    android:layout_height="200dp" />

AnimatedImageDrawable是Drawable的子元素,由ImageDecoder.decodeDrawable创建

ImageDecoder. decodedrawable进一步需要ImageDecoder的实例。由ImageDecoder.createSource创建的源。

ImageDecoder。createSource只能将source作为一个名称,ByteBuffer, File, resourceId, URI, ContentResolver来创建source对象,并使用它来创建AnimatedImageDrawable作为Drawable(多态调用)

static ImageDecoder.Source  createSource(AssetManager assets, String fileName)
static ImageDecoder.Source  createSource(ByteBuffer buffer)
static ImageDecoder.Source  createSource(File file)
static ImageDecoder.Source  createSource(Resources res, int resId)
static ImageDecoder.Source  createSource(ContentResolver cr, Uri uri)

注意:你也可以使用ImageDecoder#decodeBitmap创建位图。

输出:

AnimatedDrawable还支持调整大小,帧和颜色操作