显示动画GIF

Leo*_*nti 254 java android animated-gif

我想在我的应用程序中显示动画GIF图像.正如我发现的那样,Android本身并不支持动画GIF.

但是它可以使用AnimationDrawable显示动画:

开发>指南>图像和图形> Drawables概述

该示例使用在应用程序资源中保存为帧的动画,但我需要的是直接显示动画gif.

我的计划是将动画GIF分解为帧并将每个帧添加为可绘制到AnimationDrawable.

有谁知道如何从动画GIF中提取帧并将它们转换为Drawable

Poi*_*ull 191

Android实际上可以使用android.graphics.Movie类解码和显示动画GIF.

这没有太多文档记录,但在SDK参考中.此外,它在BitmapDecode示例中的ApiDemos中的示例中使用,带有一些动画标志.

  • @Bicou在后HC设备上你应该在你的View上设置`setLayerType(LAYER_TYPE_SOFTWARE)`.但它仍然只适用于某些GIF和某些设备. (12认同)
  • 这有多稳定?我已经在我的应用程序中实现了它,在我的冰淇淋三明治设备上它根本不工作,在2.3模拟器上它正在工作但是一些GIF帧是错误的(错误的颜色).在电脑上它当然工作正常.是什么赋予了? (9认同)
  • @MichałK谢谢!`Paint p = new Paint(); p.setAntiAlias(真); setLayerType(LAYER_TYPE_SOFTWARE,p);`工作了! (9认同)
  • 电影课是故意无证的 - 它很弱而且不受支持.如果你需要适当的Gif动画,你最好自己实现它.我使用NDK为我的应用做了这个. (2认同)
  • 此类在 API 级别 28 中已弃用。首选 [AnimatedImageDrawable](https://developer.android.com/reference/android/graphics/drawable/AnimatedImageDrawable.html) (2认同)

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

就是这样,我希望你能提供帮助.

  • 如果要从sdcard加载GIF? (2认同)

Leo*_*nti 22

我通过将gif动画分割成帧然后将其保存到手机来解决了这个问题,所以我不必在Android中处理它.

然后我将每一帧下载到手机上,从中创建Drawable,然后创建AnimationDrawable - 非常类似于我的问题中的示例

  • 这是处理动画的文档化方法. (2认同)

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)

根据班级名称......

完成这些小改动之后它应该对我有用......

希望这个帮助

  • 请随便清理这个答案. (15认同)
  • 您试过哪个Android版本?我尝试了Android 2.2和2.3但它返回Movie对象null.什么可能是错的? (2认同)
  • 答案有什么问题?我刚刚提供了一个链接和一些必要的修复工具 (2认同)

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的方法:

  • 电影课.如上所述,它是相当错误的.
  • 的WebView.它使用起来非常简单并且通常有效.但有时它会开始行为不端,而且它总是存在于你没有的一些模糊设备上.此外,您不能在任何类型的列表视图中使用多个实例,因为它会对您的内存起作用.不过,您可能会将其视为主要方法.
  • 自定义代码将GIF解码为位图并将其显示为Drawable或ImageView.我会提到两个库:

https://github.com/koral--/android-gif-drawable - 解码器用C实现,所以它非常有效.

https://code.google.com/p/giffiledecoder - 解码器是用Java实现的,因此更容易使用.即使对于大文件,仍然相当有效.

您还可以找到许多基于GifDecoder类的库.这也是一个基于Java的解码器,但它的工作原理是将整个文件加载到内存中,因此它只适用于小文件.


Roo*_*ahi 9

我很难让Android动画gif工作.我只跟着两个工作:

  1. 的WebView
  2. 离子

WebView工作正常,非常简单,但问题是它使视图加载速度变慢,应用程序在一秒左右没有响应.我不喜欢那样.所以我尝试了不同的方法(DID NOT WORK):

  1. ImageViewEx已弃用!
  2. 毕加索没有加载GIF动画
  3. android-gif-drawable看起来很棒,但它在我的项目中引起了一些有线NDK问题.它导致我的本地NDK库停止工作,我无法修复它

我有一些来回的Ion; 最后,我有它工作,它真的很快:-)

Ion.with(imgView)
  .error(R.drawable.default_image)
  .animateGif(AnimateGifMode.ANIMATE)
  .load("file:///android_asset/animated.gif");
Run Code Online (Sandbox Code Playgroud)


Sho*_*que 8

Glide

适用于Android的Image Loader Library,由Google推荐.

  • Glide与毕加索非常相似,但这比毕加索要快得多.
  • Glide比Picasso消耗更少的内存.

格莱德有什么,但毕加索没有

将GIF动画加载到简单的ImageView的功能可能是Glide最有趣的功能.是的,你不能用毕加索做到这一点. 在此输入图像描述 一些重要的链接 -

  1. https://github.com/bumptech/glide
  2. http://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en


Nig*_*ode 7

使用ImageViewEx,一个使用gif的库就像使用一样简单ImageView.

  • 是的,我认为它比较好,只是指出作者已决定弃用它。(因此不维护它),指出我们应该使用像 Picasso 这样的东西,这很奇怪,因为 Picasso 是一个图像下载器,并且在显示动画 gif 时比许多下载/缓存中的任何一个都无济于事工具在那里。 (2认同)

nir*_*ola 6

试试这个,下面是代码显示进度条中的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)


bap*_*pho 6

没人提到IonGlide库.他们工作得很好.

与WebView相比,它更容易处理.


Dr1*_*1Ku 5

我已经成功地使用了本文中提出的解决方案,这是一个名为的类GifMovieView,它可以呈现一个View可以显示或添加到特定的类ViewGroup.查看指定文章第2部分和第3部分中介绍的其他方法.

这种方法的唯一缺点是对电影的抗锯齿效果不是那么好(必须是使用"阴暗"Android Movie类的副作用).然后,最好在动画GIF中将背景设置为纯色.