Android   发布时间:2022-04-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 播放动画gif作为电影只显示画布背景大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我想在我的Android应用程序中播放动画gif.

我注意到WebView可以正常使用这段代码

webview.loadUrl("file:///android_asset/gif.gif");

不幸的是,webView太占用内存,我的应用程序经常崩溃.

我看过很多像这样使用Movie的教程:

public class GIFView extends View {
    private Movie movie;
    private long moviestart;

    public GIFView(Context context) throws IOException {
        super(context);
        movie = Movie.decodeStream(getresources().getAssets().open("gif.gif"));
    }

    public GIFView(Context context, AttributeSet attrs) throws IOException {
        super(context, attrs);
        movie = Movie.decodeStream(getresources().getAssets().open("gif.gif"));
    }

    public GIFView(Context context, AttributeSet attrs, int defStylE)
            throws IOException {
        super(context, attrs, defStylE);
        movie = Movie.decodeStream(getresources().getAssets().open("gif.gif"));
    }

    private long _start_time;

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.RED);
        super.onDraw(canvas);

        long _current_time = android.os.SystemClock.uptimeMillis();
        if (0 == _start_timE) {
            _start_time = _current_time;
        }
        if (null != moviE) {
            final int _relatif_time = (int) ((_current_time - _start_timE) % movie
                    .duration());
            movie.setTime(_relatif_timE);
            Log.i("",""+_relatif_timE);
            double scalex = (doublE) this.getWidth() / (doublE) movie.width();
            double scaley = (doublE) this.getHeight() / (doublE) movie.height();
            canvas.scale((float) scalex, (float) scaley);
             movie.draw(canvas, (float) scalex, (float) scaley)
        }

        this.invalidate();
    }
}

当我得到一个大的红色窗口时,我可以看到Canvas被绘制,并且我添加了Log,我可以看到我的电影不是null并且有一个长度!

不幸的是,除了大红色矩形之外,画布上没有任何内容,我已经使用不同的gif文件进行了测试.

知道为什么这不起作用吗?

解决方法:

您可以查看我的库源代码https://github.com/sbakhtiarov/gif-movie-view,我们只需使用它.

大佬总结

以上是大佬教程为你收集整理的android – 播放动画gif作为电影只显示画布背景全部内容,希望文章能够帮你解决android – 播放动画gif作为电影只显示画布背景所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: