android - 在android中播放下载的Gif图像

标签 android gif animated-gif

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

最佳答案

我正在使用下面的自定义 View 而不是 ImageView 。

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 - 在android中播放下载的Gif图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9494413/

相关文章:

android - 如何使用我的项目从库中获取应用程序 ID?

android - setAction() 对 intent (Broadcast) 做了什么

ios - Gif 未出现在手动输入的约束上

android - 将动画 .GIF 设置为背景 Android

javascript - 从动画 GIF 中提取单帧到 Canvas

java - Android NotificationListenerService : how to know if user clicked on the notification (opening the related app) or simply deleted it

android - 单击 ListView 上的监听器

ios - 将 GIF 图像从 URL 保存到相机胶卷

Python Pillow 透明 gif 不起作用

android - Android 1.6 上的 WebView 中不显示动画 GIF