user1758835
user1758835

Reputation: 215

To show animated gifs in gridview

I have written the code to show animation for single image.Now I want to display the gif images in gridview to show that animated images.Is it possible and how to do that ?

Upvotes: 3

Views: 2198

Answers (2)

Ajay
Ajay

Reputation: 1189

i think it might help you.. this is a GifWebView demo.. please check it...

mylayout.xml

<RelativeLayout 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" >

    <LinearLayout
        android:id="@+id/toplayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

    </LinearLayout>

</RelativeLayout>

MyLayout.java file

package com.test;

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

public class MainActivity extends Activity
{
    LinearLayout toplayout;
    GifWebView view;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.mylayout);

       toplayout = (LinearLayout)findViewById(R.id.toplayout);

       InputStream stream = null;
       try
       {
          stream = getAssets().open("ppp.gif");          
       } catch (IOException e) {
         e.printStackTrace();
       }

       view = new GifWebView(this, stream);
       toplayout.addView(view);       
    }
}

GifWebView.java file

package com.test;

import android.content.Context;
import java.io.InputStream;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Movie;
import android.os.SystemClock;
import android.view.View;

public class GifWebView extends View {
    private Movie mMovie;
    InputStream mStream;
    long mMoviestart;

    public GifWebView(Context context, InputStream stream) {
        super(context);
        mStream = stream;
        mMovie = Movie.decodeStream(mStream);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.TRANSPARENT);
        super.onDraw(canvas);
        final long now = SystemClock.uptimeMillis();

        if (mMoviestart == 0) {
            mMoviestart = now;
        }
        final int relTime = (int) ((now - mMoviestart) % mMovie.duration());
        mMovie.setTime(relTime);
        mMovie.draw(canvas, 10, 10);
        this.invalidate();
    }
}

Put any gif file in Assets directory and give appropriate name. in this demo name is ppp.gif.

Upvotes: 1

Kgrover
Kgrover

Reputation: 2116

I would try to use this view and simply add it to a GridView custom cell layout.

Essentially, the GifView in the layout should work.

Upvotes: 0

Related Questions