在GridView中显示动态GIF的方法

3

我已经编写了用于显示单个图像动画的代码。现在我想在网格视图中显示gif图像以显示这些动画图像。是否有可能,并且如何实现?

2个回答

1

我认为这可能会对你有所帮助.. 这是一个GifWebView演示.. 请查看它...

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文件

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 文件

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

将任何gif文件放入Assets目录中并赋予适当的名称。在此演示中名称为ppp.gif。

1
请从http://www.mediafire.com/?tk3fr5r6tri3v7x链接下载,并且如果有帮助的话,请投票和接受。 - Ajay
@Ajay_Addon:我也需要同样的东西,你能给我发个演示吗? - Hussain Akhtar Wahid 'Ghouri'
@Ajay_Addon:是的,我后来尝试了一下,它完美地运行了,但是我还有一个关于 gif 图像的问题,你有兴趣帮忙吗? - Hussain Akhtar Wahid 'Ghouri'
@Hussain Akhtar Wahid:为什么不呢,兄弟。是的吗? - Ajay
@Hussain Akhtar Wahid:这是指如果我点击任何gif图像,它会改变,对吗? - Ajay
显示剩余6条评论

0

我会尝试使用这个视图,并将其简单地添加到GridView自定义单元格布局中。

实质上,布局中的GifView应该可以工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接