我想在我的应用程序中显示动画GIF图像。 我发现Android本身并不支持GIF动画。
但是它可以使用AnimationDrawable显示动画:
开发>指南>图像和图形>绘图概述
这个例子使用动画保存为框架的应用程序资源,但我需要的是直接显示动画gif。
我的计划是将动画GIF分解为帧,并将每帧添加为可绘制的AnimationDrawable。
有人知道如何从动画GIF中提取帧并将它们转换为可绘制的吗?
我想在我的应用程序中显示动画GIF图像。 我发现Android本身并不支持GIF动画。
但是它可以使用AnimationDrawable显示动画:
开发>指南>图像和图形>绘图概述
这个例子使用动画保存为框架的应用程序资源,但我需要的是直接显示动画gif。
我的计划是将动画GIF分解为帧,并将每帧添加为可绘制的AnimationDrawable。
有人知道如何从动画GIF中提取帧并将它们转换为可绘制的吗?
当前回答
更新:
使用滑动:
dependencies {
implementation 'com.github.bumptech.glide:glide:4.9.0'
}
用法:
Glide.with(context).load(GIF_URI).into(new DrawableImageViewTarget(IMAGE_VIEW));
看文档
其他回答
我很难让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");
最简单的方法-可以考虑下面的代码
我们可以利用Imageview的setImageResource,参考下面的代码相同。
下面的代码可以用来显示像gif一样的图像,如果你有gif的多次分割图像。只需从在线工具中将gif分割成单独的png,并按照以下顺序将图像放入可绘制图中
Image_1.png, image_2.png等等。
让处理程序动态地更改图像。
int imagePosition = 1;
Handler handler = new Handler();
Runnable runnable = new Runnable() {
public void run() {
updateImage();
}
};
public void updateImage() {
appInstance.runOnUiThread(new Runnable() {
@Override
public void run() {
int resId = getResources().getIdentifier("image_" + imagePosition, "drawable", appInstance.getPackageName());
gifImageViewDummy.setImageResource(resId);
imagePosition++;
//Consider you have 30 image for the anim
if (imagePosition == 30) {
//this make animation play only once
handler.removeCallbacks(runnable);
} else {
//You can define your own time based on the animation
handler.postDelayed(runnable, 50);
}
//to make animation to continue use below code and remove above if else
// if (imagePosition == 30)
//imagePosition = 1;
// handler.postDelayed(runnable, 50);
//
}
});
}
试试这个,下面的代码在进度条中显示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);
}
}
我找到了一个非常简单的方法,这里有一个很好的简单的工作例子
显示动画小部件
在让它工作之前,在代码中有一些更改要做
在下面
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceStated);
setContentView(new MYGIFView());
}
}
只是替换
setContentView(new MYGIFView());
in
setContentView(new MYGIFView(this));
而在
public GIFView(Context context) {
super(context);
提供您自己的gif动画文件
is = context.getResources().openRawResource(R.drawable.earth);
movie = Movie.decodeStream(is);
}
替换第一行
public MYGIFView(Context context) {
根据课程名称…
在做了这个小改变后,它应该为我工作…
希望这对你有所帮助
有两个选项可以将动画gif加载到我们的Android应用程序中
1)使用Glide将gif加载到ImageView中。
String urlGif = "https://cdn.dribbble.com/users/263558/screenshots/1337078/dvsd.gif";
//add Glide implementation into the build.gradle file.
ImageView imageView = (ImageView)findViewById(R.id.imageView);
Uri uri = Uri.parse(urlGif);
Glide.with(getApplicationContext()).load(uri).into(imageView);
2)使用html加载gif到WebView
创建带有。gif文件地址的html:
<html style="margin: 0;">
<body style="margin: 0;">
<img src="https://..../myimage.gif" style="width: 100%; height: 100%" />
</body>
</html>
将这个文件存储到assets目录中:
将这个html加载到应用程序的WebView中:
WebView webView = (WebView)findViewById(R.id.webView);
webView = (WebView) findViewById(R.id.webView);
webView.loadUrl("file:///android_asset/html/webpage_gif.html");
下面是这两个选项的完整示例。