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

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

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

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

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

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


当前回答

使用ImageViewEx,一个库,使使用gif像使用ImageView一样简单。

其他回答

我很难让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");

也放(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

这是我为在应用程序中显示动图所做的。我扩展了ImageView,这样人们就可以自由地使用它的属性。它可以显示gif从url或资产目录。 这个库还使扩展类从它继承和扩展它来支持不同的方法来初始化gif变得很容易。

https://github.com/Gavras/GIFView

在github页面上有一个小指南。

它也发布在Android Arsenal上:

https://android-arsenal.com/details/1/4947

使用的例子:

从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" />

活动中:

    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) {

        }
});

以编程方式设置gif:

mGifView.setGifResource("asset:gif1");

下滑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中,它必须能够正确地显示它,因为默认浏览器支持gif文件。(Froyo+,如果我没记错的话)