显示GIF图像与库 [英] Showing Gif Image with Library

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

问题描述

我使用这个库显示GIF图片: https://github.com/felipecsl/GifImageView

但我不知道如何实现我的GIF图像在这些code。我的资产文件夹一些GifImages。

下面是我的code:

PlayActivity.java:

 进口android.app.Activity;
进口android.os.Bundle;

进口com.felipecsl.gifimageview.library.GifImageView;

进口java.io.IOException异常;
进口的java.io.InputStream;

公共类PlayActivity延伸活动{

    GifImageView gifView;
    // byte []的字节数;

    @覆盖
    保护无效的onCreate(最终捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.layout_play_activity);

        尝试 {
            InputStream的是= getAssets()开(rain_4.gif)。
            字节[]字节=新字节[is.available()];
            is.read(字节);
            is.close();

            gifView =(GifImageView)findViewById(R.id.gifImageView);
            gifView =新GifImageView(本);
            gifView.setBytes(字节);
            gifView.startAnimation();
        }赶上(IOException异常E){
            e.printStackTrace();
        }
    }

    @覆盖
    保护无效的OnStart(){
        super.onStart();
        如果(!gifView = NULL)gifView.startAnimation();
    }

    @覆盖
    保护无效的onStop(){
        super.onStop();
        如果(!gifView = NULL)gifView.startAnimation();
    }
}
 

和layout_play_activity.xml:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:以下属性来=@扪/ activity_horizo​​ntal_margin
    机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
    机器人:paddingTop =@扪/ activity_vertical_margin
    机器人:paddingBottom会=@扪/ activity_vertical_margin
    机器人:方向=垂直
    工具:上下文=PlayActivity。>

    < com.felipecsl.gifimageview.library.GifImageView
        机器人:ID =@ + ID / gifImageView
        机器人:layout_gravity =中心
        机器人:scaleType =fitCenter
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent/>

< / LinearLayout中>
 

解决方案

您提供了在它的样本库的链接。您的加载GifImageView的方式是从写在样品中的方式不同。将样品从互联网上下载的GIF

下面是从样品MainActivity一个片段:

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

新GifDataDownloader(){
    @覆盖
    保护无效onPostExecute(最后一个字节[]字节){
        gifImageView.setBytes(字节);
        gifImageView.startAnimation();
        Log.d(TAG,GIF宽度为+ gifImageView.getGifWidth());
        Log.d(TAG,GIF高度为+ gifImageView.getGifHeight());
    }
} .execute(http://gifs.joelglovier.com/aha/aha.gif);
 

解决方法:

我不知道你是否能与此库加载GifImageView从你的资产,但我用离子库preFER。这是非常好的和简单。与该库,您还可以拉伸的GIF图像,只要你喜欢:

https://github.com/koush/ion

简单的例子:

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

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

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

Here is my code:

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();
    }
}

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>

解决方案

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.

Here is a snippet from MainActivity of the sample:

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

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

Simple Example:

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

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

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