android - 使用库显示 Gif 图像

标签 android gif animated-gif

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

但我不知道如何在这些代码中实现我的 gif 图像。我在 Assets 文件夹中有一些 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>

最佳答案

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

以下是示例的 MainActivity 的 fragment :

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");

解决方案:

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

https://github.com/koush/ion

简单示例:

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

关于android - 使用库显示 Gif 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32996878/

相关文章:

android - Android中Activity之间的3D "cube"动画

java - 在自定义 Drawable 类中获取 Context

Magento Gif 上传

android - 如何在 Android 中将 gif 图像添加到 TextView 或 EditText

python - 为多个 xarray 图制作带有修复颜色条的干净高质量 GIF

javascript - GIF动画在所有主流浏览器中都和javascript运行在同一个线程上是真的吗?

android - Kotlin android 扩展,无法导入包

android - Android 可以播放 GIF 格式吗?

gif图像在html中重复播放

java - 声音效果/操作?