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

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

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

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

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

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


当前回答

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

其他回答

下滑4.6

1. 加载gif

GlideApp.with(context)
            .load(R.raw.gif) // or url
            .into(imageview);

2. 来获取文件对象

GlideApp.with(context)
                .asGif()
                .load(R.raw.gif) //or url
                .into(new SimpleTarget<GifDrawable>() {
                    @Override
                    public void onResourceReady(@NonNull GifDrawable resource, @Nullable Transition<? super GifDrawable> transition) {

                        resource.start();
                      //resource.setLoopCount(1);
                        imageView.setImageDrawable(resource);
                    }
                });

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

没有人提到Ion或Glide库。它们工作得很好。

与WebView相比,它更容易处理。

我很难让gif动画在Android上运行。我只有以下两个工作:

WebView 阴离子

WebView工作正常,非常简单,但问题是它会使视图加载变慢,应用程序会在一秒钟左右失去响应。我不喜欢那样。所以我尝试了不同的方法(不管用):

ImageViewEx已弃用! 毕加索没有加载动画GIF android-gif-drawable看起来很棒,但它在我的项目中引起了一些有线NDK问题。它导致我本地的NDK库停止工作,我无法修复它

我和Ion有过一些交锋;最后,我让它工作,它真的很快:-)

Ion.with(imgView)
  .error(R.drawable.default_image)
  .animateGif(AnimateGifMode.ANIMATE)
  .load("file:///android_asset/animated.gif");

类似于@Leonti说的,但更有深度:

为了解决同样的问题,我所做的是打开GIMP,隐藏除一个层之外的所有层,将其导出为其自己的图像,然后隐藏该层并取消隐藏下一个层,等等,直到每个层都有单独的资源文件。然后我可以在AnimationDrawable XML文件中使用它们作为框架。