Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing Gif Image with Library

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>
like image 977
Khuong Avatar asked Sep 26 '22 13:09

Khuong


2 Answers

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");
like image 199
Hussein El Feky Avatar answered Oct 03 '22 02:10

Hussein El Feky


1.I would suggest you to checkout Image Management Library by Facebook that is Fresco that is pretty awesome and mature as compared to other Image Loading Library.

2.Fresco has SimpleDraweeView as custom image view which supports Rounded Corners and Circles link and supports Animated(.gif, .webp) as well as Normal Images(.jpg, .png).

3.Fresco handles all the things caching of images with 3 Tier architecture ( BITMAP_MEMORY_CACHE, ENCODED_MEMORY_CACHE and DISK_CACHE). It also reduces OOM(Out Of Memory) issues. When image in a view goes out of screen it automatically recycles the bitmap, hence releasing the memory.

like image 27
Vipul Asri Avatar answered Oct 03 '22 02:10

Vipul Asri