问题

我想在我的应用程序中显示动画GIF图像。正如我发现Android本身不支持动画GIF的困难方式。

但是它可以使用AnimationDrawable显示动画:
Develop > Guides > Images & Graphics > Drawables Overview
该示例使用在应用程序资源中保存为帧的动画,但我需要的是直接显示动画gif。

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

有谁知道如何从动画GIF中提取帧并将它们转换为Drawable


#1 热门回答(179 赞)

Android实际上可以使用android.graphics.Movie类解码和显示动画GIF。

这没有太多记录,但是在SDK Reference。此外,它用于Samples in ApiDemos in BitmapDecode示例中,带有一些动画标志。


#2 热门回答(38 赞)

更新:
使用滑行:

dependencies {
  compile 'com.github.bumptech.glide:glide:4.0.0'
}

用法:

Glide.with(context).load(GIF_URI).into(new GlideDrawableImageViewTarget(IMAGE_VIEW));

##查看文档

老答案:

这是一个很棒的图书馆:
https://github.com/koral--/android-gif-drawable
XML的用法:

<pl.droidsonroids.gif.GifImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/src_anim"
    android:background="@drawable/bg_anim"
    />

JAVA的用法:

GifImageView givImageView = (GifImageView)findViewById...;

GifDrawable可以直接从各种来源构建:

//asset file
    GifDrawable gifFromAssets = new GifDrawable( getAssets(), "anim.gif" );

    //resource (drawable or raw)
    GifDrawable gifFromResource = new GifDrawable( getResources(), R.drawable.anim );

    //byte array
    byte[] rawGifBytes = ...
    GifDrawable gifFromBytes = new GifDrawable( rawGifBytes );

    //FileDescriptor
    FileDescriptor fd = new RandomAccessFile( "/path/anim.gif", "r" ).getFD();
    GifDrawable gifFromFd = new GifDrawable( fd );

    //file path
    GifDrawable gifFromPath = new GifDrawable( "/path/anim.gif" );

    //file
    File gifFile = new File(getFilesDir(),"anim.gif");
    GifDrawable gifFromFile = new GifDrawable(gifFile);

    //AssetFileDescriptor
    AssetFileDescriptor afd = getAssets().openFd( "anim.gif" );
    GifDrawable gifFromAfd = new GifDrawable( afd );

    //InputStream (it must support marking)
    InputStream sourceIs = ...
    BufferedInputStream bis = new BufferedInputStream( sourceIs, GIF_LENGTH );
    GifDrawable gifFromStream = new GifDrawable( bis );

    //direct ByteBuffer
    ByteBuffer rawGifBytes = ...
    GifDrawable gifFromBytes = new GifDrawable( rawGifBytes );

和setImageDrawable:

givImageView.setImageDrawable(GifDrawable);

#3 热门回答(27 赞)

还把(main / assets / htmls / 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" />

在你的Activity中将下一个代码放在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与静态图像更多信息checkhttps://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html

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


原文链接