使用库显示 Gif 图像

Showing Gif Image with Library

我正在使用这个库来显示 gif 图像:https://github.com/felipecsl/GifImageView

但我不知道如何在这些代码中实现我的 gif 图像。我在资产文件夹中有一些 GifImages。

这是我的代码:

PlayActivity.java:

import android.app.Activity;
import android.os.Bundle;

import com.felipecsl.gifimageview.library.GifImageView;

import java.io.IOException;
import java.io.InputStream;

public class PlayActivity extends Activity{

    GifImageView gifView;
    //byte[] bytes;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_play_activity);

        try {
            InputStream is = getAssets().open("rain_4.gif");
            byte[] bytes = new byte[is.available()];
            is.read(bytes);
            is.close();

            gifView = (GifImageView) findViewById(R.id.gifImageView);
            gifView = new GifImageView(this);
            gifView.setBytes(bytes);
            gifView.startAnimation();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        if(gifView != null) gifView.startAnimation();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(gifView != null) gifView.startAnimation();
    }
}

和layout_play_activity.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".PlayActivity">

    <com.felipecsl.gifimageview.library.GifImageView
        android:id="@+id/gifImageView"
        android:layout_gravity="center"
        android:scaleType="fitCenter"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

首先,您需要将gif 放在assets 文件夹中。在 Android Studio 项目中默认不创建资产文件夹,因此您应该在 src/main/assets 中创建一个目录并将您的 .gif 文件放入其中。 应为可绘制资源保留可绘制资源文件夹。

您可以使用输入流将字节加载到字节数组中,这是在您的 onCreate 方法中完成的:

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_play_activity);
    try {
        //animation.gif is just an example, use the name of your file
        //that is inside the assets folder.

        InputStream is = getAssets().open("animation.gif");
        bytes = new byte[is.available()];
        is.read(bytes);
        is.close();

        gifView = (GifImageView) findViewById(R.id.gifImageView);
        gifView = new GifImageView(this);
        gifView.setBytes(bytes);
        gifView.startAnimation();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

我建议查看 Glide library,因为它无需任何修改即可生成动画 GIF,并且可以适合任何项目。

如果您在任何类型的回收视图中使用这些图像,它还有一个额外的好处,即加载更顺畅和适当调整大小。


正如@fixmycode 所说,如果您想使用当前的实现,您永远不会定义字节[]。

1.I 建议您查看 Facebook 的 图片管理库 Fresco 与其他图片加载库相比,它非常棒且成熟.

2.Fresco 具有 SimpleDraweeView 作为自定义图像视图,支持 Rounded Corners and Circles link 支持动画(.gif、.webp)以及普通图片(.jpg, .png).

3.Fresco 使用 3 层架构(BITMAP_MEMORY_CACHEENCODED_MEMORY_CACHEDISK_CACHE)处理所有图像缓存。它还减少了 OOM(内存不足)问题。当视图中的图像超出屏幕时,它会自动回收位图,从而释放内存。

您提供的库 link 中有一个示例。您加载 GifImageView 的方式与示例中的方式不同。该示例正在从互联网上下载一个 gif。

下面是示例的 MainActivity 的片段:

GifImageView gifImageView = (GifImageView) findViewById(R.id.gifImageView);

new GifDataDownloader() {
    @Override
    protected void onPostExecute(final byte[] bytes) {
        gifImageView.setBytes(bytes);
        gifImageView.startAnimation();
        Log.d(TAG, "GIF width is " + gifImageView.getGifWidth());
        Log.d(TAG, "GIF height is " + gifImageView.getGifHeight());
    }
}.execute("http://gifs.joelglovier.com/aha/aha.gif");

解决方案:

我不确定您是否可以使用此库从您的资源中加载 GifImageView,但我更喜欢使用 ion 库。这真的很好也很简单。使用该库,您还可以随意拉伸 gif 图片:

https://github.com/koush/ion

简单示例:

ImageView imageView = (ImageView) findViewById(R.id.imageView);
Ion.with(imageView).load("http://gifs.joelglovier.com/aha/aha.gif");