我想在我的应用程序中显示动画GIF图像。 我发现Android本身并不支持GIF动画。
但是它可以使用AnimationDrawable显示动画:
开发>指南>图像和图形>绘图概述
这个例子使用动画保存为框架的应用程序资源,但我需要的是直接显示动画gif。
我的计划是将动画GIF分解为帧,并将每帧添加为可绘制的AnimationDrawable。
有人知道如何从动画GIF中提取帧并将它们转换为可绘制的吗?
我想在我的应用程序中显示动画GIF图像。 我发现Android本身并不支持GIF动画。
但是它可以使用AnimationDrawable显示动画:
开发>指南>图像和图形>绘图概述
这个例子使用动画保存为框架的应用程序资源,但我需要的是直接显示动画gif。
我的计划是将动画GIF分解为帧,并将每帧添加为可绘制的AnimationDrawable。
有人知道如何从动画GIF中提取帧并将它们转换为可绘制的吗?
当前回答
我已经成功地使用了本文中提出的解决方案,一个名为GifMovieView的类,它呈现一个视图,然后可以显示或添加到特定的ViewGroup中。查看指定文章的第2部分和第3部分中介绍的其他方法。
这种方法的唯一缺点是电影上的反锯齿不是很好(一定是使用“阴暗的”Android电影类的副作用)。然后你最好在你的动画GIF中设置一个纯色的背景。
其他回答
试试这个,下面的代码在进度条中显示gif文件
loading_activity.xml(在布局文件夹中)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff" >
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:indeterminate="true"
android:indeterminateDrawable="@drawable/custom_loading"
android:visibility="gone" />
</RelativeLayout>
Custom_loading.xml(在可绘制文件夹中)
这里我把black_gif.gif(在可绘制文件夹),你可以把自己的GIF在这里
<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/black_gif"
android:pivotX="50%"
android:pivotY="50%" />
java(在res文件夹中)
public class LoadingActivity extends Activity {
ProgressBar bar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loading);
bar = (ProgressBar) findViewById(R.id.progressBar);
bar.setVisibility(View.VISIBLE);
}
}
在Android上显示动画GIF的方法:
电影类。正如上面提到的,它有很多bug。 WebView。使用起来很简单,而且通常很有效。但有时它开始表现不佳,而且总是在一些你没有的晦涩设备上。另外,您不能在任何类型的列表视图中使用多个实例,因为它会对内存产生影响。不过,您可以将其视为主要方法。 自定义代码解码gif为位图,并显示为Drawable或ImageView。我将提到两个库:
https://github.com/koral--/android-gif-drawable -解码器是用C语言实现的,因此非常高效。
https://code.google.com/p/giffiledecoder -解码器是在Java中实现的,所以它更容易使用。即使是大文件,仍然相当高效。
你还会发现许多基于GifDecoder类的库。这也是一个基于java的解码器,但它是通过将整个文件加载到内存中来工作的,因此它只适用于小文件。
仅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还支持调整大小,帧和颜色操作
类似于@Leonti说的,但更有深度:
为了解决同样的问题,我所做的是打开GIMP,隐藏除一个层之外的所有层,将其导出为其自己的图像,然后隐藏该层并取消隐藏下一个层,等等,直到每个层都有单独的资源文件。然后我可以在AnimationDrawable XML文件中使用它们作为框架。
没有人提到Ion或Glide库。它们工作得很好。
与WebView相比,它更容易处理。