我想在我的应用程序中显示动画GIF图像。 我发现Android本身并不支持GIF动画。
但是它可以使用AnimationDrawable显示动画:
开发>指南>图像和图形>绘图概述
这个例子使用动画保存为框架的应用程序资源,但我需要的是直接显示动画gif。
我的计划是将动画GIF分解为帧,并将每帧添加为可绘制的AnimationDrawable。
有人知道如何从动画GIF中提取帧并将它们转换为可绘制的吗?
我想在我的应用程序中显示动画GIF图像。 我发现Android本身并不支持GIF动画。
但是它可以使用AnimationDrawable显示动画:
开发>指南>图像和图形>绘图概述
这个例子使用动画保存为框架的应用程序资源,但我需要的是直接显示动画gif。
我的计划是将动画GIF分解为帧,并将每帧添加为可绘制的AnimationDrawable。
有人知道如何从动画GIF中提取帧并将它们转换为可绘制的吗?
当前回答
为了节省资源,有滑翔库。 不知道为什么要使用其他任何东西,特别是webview只显示图像。 Glide是一个完美而简单的库,它可以从gif中准备动画,并将其直接放到imageview中。 gifdrawable句柄动画本身的逻辑。 Gif有lzw压缩原始rgb数据的动画里面。 没有理由复杂的使用webview和管理更多的文件,只显示一个gif文件在应用程序。
其他回答
首先,Android浏览器应该支持动画gif。如果没有,那就是bug!看一下问题跟踪器。
如果你在浏览器外显示这些动画gif,情况可能就不一样了。要做到你所要求的,将需要外部库,支持动画gif解码。
第一个接口是Java2D或JAI (Java Advanced Imaging) API,如果Android Dalvik在你的应用程序中支持这些库,我会感到非常惊讶。
我解决了这个问题,在将gif动画保存到手机之前将其分割成帧,这样我就不必在Android中处理它了。
然后我把每一帧下载到手机上,从中创建Drawable,然后创建AnimationDrawable -非常类似于我的问题中的例子
只是想补充一点,Movie类现在已弃用。
这个类在API级别P中已弃用。
建议使用此方法
AnimatedImageDrawable
用于绘制动画图像(如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还支持调整大小,帧和颜色操作
这是我为在应用程序中显示动图所做的。我扩展了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");