我想在我的应用程序中显示动画GIF图像。 我发现Android本身并不支持GIF动画。
但是它可以使用AnimationDrawable显示动画:
开发>指南>图像和图形>绘图概述
这个例子使用动画保存为框架的应用程序资源,但我需要的是直接显示动画gif。
我的计划是将动画GIF分解为帧,并将每帧添加为可绘制的AnimationDrawable。
有人知道如何从动画GIF中提取帧并将它们转换为可绘制的吗?
我想在我的应用程序中显示动画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还支持调整大小,帧和颜色操作
其他回答
这是我为在应用程序中显示动图所做的。我扩展了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");
没有人提到Ion或Glide库。它们工作得很好。
与WebView相比,它更容易处理。
把它放到WebView中,它必须能够正确地显示它,因为默认浏览器支持gif文件。(Froyo+,如果我没记错的话)
滑翔
Image Loader Library for Android,由谷歌推荐。
Glide和毕加索的很相似,但比毕加索的快得多。 Glide比毕加索消耗的内存要少。
格莱德有而毕加索没有的东西
能够将GIF动画加载到一个简单的ImageView可能是Glide最有趣的功能。是的,你不能这样对待毕加索。 一些重要的环节-
https://github.com/bumptech/glide http://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en
Android实际上可以解码和显示动画gif,使用Android。graphics。movie类。
这没有太多的文档,但是在SDK Reference中有。此外,它被用于ApiDemos在BitmapDecode的例子中的样本与一些动画标志。