显示带有库的 Gif 图像 [英] Showing Gif Image with Library

查看:22
本文介绍了显示带有库的 Gif 图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using this library to show gif image: https://github.com/felipecsl/GifImageView

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

But I don't know how to implement my gif image in these code. I have some GifImages in assets folder.

这是我的代码:

PlayActivity.java:

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:

And 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.

The library link you provided had a sample within it. Your way of loading the GifImageView was different from the way written in the sample. The sample was downloading a gif from the internet.

以下是示例 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 图像:

Solution:

I am not sure if you can load GifImageView from your assets with this library, but I prefer using ion library. It's really nice and simple. With that library, you can also stretch gif images as you like:

https://github.com/koush/ion

简单示例:

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

这篇关于显示带有库的 Gif 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆