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

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

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

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

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

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


当前回答

如果你想使用Glide加载gif:

Glide.with(this)
        .asGif()
        .load(R.raw.onboarding_layers) //Your gif resource
        .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.NONE))
        .listener(new RequestListener<GifDrawable>() {
            @Override
            public boolean onLoadFailed(@Nullable @org.jetbrains.annotations.Nullable GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) {
                return false;
            }

            @Override
            public boolean onResourceReady(GifDrawable resource, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) {
                resource.setLoopCount(1);
                return false;
            }
        })
        .into((ImageView) view.findViewById(R.id.layer_icons));

其他回答

Android实际上可以解码和显示动画gif,使用Android。graphics。movie类。

这没有太多的文档,但是在SDK Reference中有。此外,它被用于ApiDemos在BitmapDecode的例子中的样本与一些动画标志。

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

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

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

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还支持调整大小,帧和颜色操作

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