在android中播放下载的Gif图像 [英] play downloaded Gif image in android

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

问题描述

我正在我的应用程序中从服务器下载 GIF 图像,然后我用 ImageView 显示它,但它没有动画.有没有其他方法可以播放下载的动画 GIF 图像.提前致谢.

I'm downloading GIF image in my app from server and then i'm showing it with ImageView but it was not animated . is there any other way to play downloaded animated GIF image . Thanks in advance .

推荐答案

我使用下面的自定义视图而不是图像视图.

I am using the below custom View instead of Image View.

public class SampleView extends View {

    private Movie mMovie;
    private long mMovieStart;

    public SampleView(Context context) {
        super(context);
        setFocusable(true);

        java.io.InputStream is;
        is = context.getResources().openRawResource(R.drawable.girl_dances);
        mMovie = Movie.decodeStream(is); 
    }

    public SampleView(Context context, AttributeSet attrSet) {
        super(context, attrSet);
        setFocusable(true);

        java.io.InputStream is;
        is = context.getResources().openRawResource(R.drawable.girl_dances);
        mMovie = Movie.decodeStream(is);
    }

    public SampleView(Context context, AttributeSet attrSet, int defStyle) {
        super(context, attrSet, defStyle);
        setFocusable(true);

        java.io.InputStream is;
        is = context.getResources().openRawResource(R.drawable.girl_dances);
        mMovie = Movie.decodeStream(is);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(0x00000000);

        Paint p = new Paint();
        p.setAntiAlias(true);

        long now = android.os.SystemClock.uptimeMillis();
        if (mMovieStart == 0) { // first time
            mMovieStart = now;
        }
        if (mMovie != null) {
            int dur = mMovie.duration();
            if (dur == 0) {
                dur = 1000;
            }
            int relTime = (int) ((now - mMovieStart) % dur);
            mMovie.setTime(relTime);
            mMovie.draw(canvas, getWidth() / 2 - mMovie.width() / 2,
                    getHeight() / 2 - mMovie.height() / 2);
            invalidate();
        }
    }
    }

XML 布局:

<com.test.sample.SampleView
android:id="@+id/gif_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

这篇关于在android中播放下载的Gif图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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