Leo*_*nti 254 java android animated-gif
我想在我的应用程序中显示动画GIF图像.正如我发现的那样,Android本身并不支持动画GIF.
但是它可以使用AnimationDrawable显示动画:
该示例使用在应用程序资源中保存为帧的动画,但我需要的是直接显示动画gif.
我的计划是将动画GIF分解为帧并将每个帧添加为可绘制到AnimationDrawable.
有谁知道如何从动画GIF中提取帧并将它们转换为Drawable?
Poi*_*ull 191
Android实际上可以使用android.graphics.Movie类解码和显示动画GIF.
这没有太多文档记录,但在SDK参考中.此外,它在BitmapDecode示例中的ApiDemos中的示例中使用,带有一些动画标志.
itz*_*har 57
更新:
使用滑行:
dependencies {
implementation 'com.github.bumptech.glide:glide:4.0.0'
}
Run Code Online (Sandbox Code Playgroud)
用法:
Glide.with(context).load(GIF_URI).into(new GlideDrawableImageViewTarget(IMAGE_VIEW));
Run Code Online (Sandbox Code Playgroud)
Ale*_*aos 28
也把(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>
Run Code Online (Sandbox Code Playgroud)
在你的Xml中声明例如这样(main/res/layout/name.xml):[你定义大小,例如]
<WebView
android:layout_width="70dp"
android:layout_height="70dp"
android:id="@+id/webView"
android:layout_gravity="center_horizontal" />
Run Code Online (Sandbox Code Playgroud)
在您的Activity中将下一个代码放在onCreate中
web = (WebView) findViewById(R.id.webView);
web.setBackgroundColor(Color.TRANSPARENT); //for gif without background
web.loadUrl("file:///android_asset/htmls/name.html");
Run Code Online (Sandbox Code Playgroud)
如果要动态加载,则必须使用数据加载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);
Run Code Online (Sandbox Code Playgroud)
建议:更好的加载带有静态图像的gif以获取更多信息请查看https://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html
就是这样,我希望你能提供帮助.
Leo*_*nti 22
我通过将gif动画分割成帧然后将其保存到手机来解决了这个问题,所以我不必在Android中处理它.
然后我将每一帧下载到手机上,从中创建Drawable,然后创建AnimationDrawable - 非常类似于我的问题中的示例
App*_*ide 16
我找到了一个非常简单的方法,这里有一个简单的工作示例
在开始工作之前,在代码中有一些要做的事情
在下面的
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceStated);
setContentView(new MYGIFView());
}
}
Run Code Online (Sandbox Code Playgroud)
只是替换
setContentView(new MYGIFView());
Run Code Online (Sandbox Code Playgroud)
在
setContentView(new MYGIFView(this));
Run Code Online (Sandbox Code Playgroud)
和IN
public GIFView(Context context) {
super(context);
Run Code Online (Sandbox Code Playgroud)
提供您自己的gif动画文件
is = context.getResources().openRawResource(R.drawable.earth);
movie = Movie.decodeStream(is);
}
Run Code Online (Sandbox Code Playgroud)
取代第一线
public MYGIFView(Context context) {
Run Code Online (Sandbox Code Playgroud)
根据班级名称......
完成这些小改动之后它应该对我有用......
希望这个帮助
Gow*_*K C 15
滑翔 4.6
1.加载gif
GlideApp.with(context)
.load(R.raw.gif) // or url
.into(imageview);
Run Code Online (Sandbox Code Playgroud)
2.获取文件对象
GlideApp.with(context)
.asGif()
.load(R.raw.gif) //or url
.into(new SimpleTarget<GifDrawable>() {
@Override
public void onResourceReady(@NonNull GifDrawable resource, @Nullable Transition<? super GifDrawable> transition) {
resource.start();
//resource.setLoopCount(1);
imageView.setImageDrawable(resource);
}
});
Run Code Online (Sandbox Code Playgroud)
Nic*_*lov 10
在Android上显示动画GIF的方法:
https://github.com/koral--/android-gif-drawable - 解码器用C实现,所以它非常有效.
https://code.google.com/p/giffiledecoder - 解码器是用Java实现的,因此更容易使用.即使对于大文件,仍然相当有效.
您还可以找到许多基于GifDecoder类的库.这也是一个基于Java的解码器,但它的工作原理是将整个文件加载到内存中,因此它只适用于小文件.
我很难让Android动画gif工作.我只跟着两个工作:
WebView工作正常,非常简单,但问题是它使视图加载速度变慢,应用程序在一秒左右没有响应.我不喜欢那样.所以我尝试了不同的方法(DID NOT WORK):
我有一些来回的Ion; 最后,我有它工作,它真的很快:-)
Ion.with(imgView)
.error(R.drawable.default_image)
.animateGif(AnimateGifMode.ANIMATE)
.load("file:///android_asset/animated.gif");
Run Code Online (Sandbox Code Playgroud)
Glide
适用于Android的Image Loader Library,由Google推荐.
格莱德有什么,但毕加索没有
将GIF动画加载到简单的ImageView的功能可能是Glide最有趣的功能.是的,你不能用毕加索做到这一点.
一些重要的链接 -
使用ImageViewEx,一个使用gif的库就像使用一样简单ImageView.
试试这个,下面是代码显示进度条中的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>
Run Code Online (Sandbox Code Playgroud)
custom_loading.xml(在drawable文件夹中)
在这里我把black_gif.gif(在drawable文件夹中),你可以把你自己的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%" />
Run Code Online (Sandbox Code Playgroud)
LoadingActivity.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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
369306 次 |
| 最近记录: |