【问题标题】:Playing a animated gif as a Movie only display canvas background将动画 gif 作为电影播放仅显示画布背景
【发布时间】:2013-05-21 02:03:41
【问题描述】:

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

我注意到 WebView 可以正常使用此代码:

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

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

我看过很多这样的电影教程:

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

当我得到一个大红色窗口时,我可以看到画布被绘制,并且通过我添加的日志,我可以看到我的电影不为空并且有长度!

不幸的是,画布中除了红色的大矩形之外什么都没有出现,我已经用不同的 gif 文件进行了测试。

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

【问题讨论】:

    标签: android animated-gif


    【解决方案1】:

    你可以看看我的库源代码https://github.com/sbakhtiarov/gif-movie-view我们只是使用它。

    【讨论】:

    • 我试过你的代码,确实可以。问题是动画不关心 GIF 帧时间。如您所知,每一帧都有一个动画师必须小心等待的毫秒时间。你的类的实现简单的播放一帧又一帧以恒定的速度。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-23
    • 2012-04-14
    • 2016-10-01
    • 1970-01-01
    • 2013-03-28
    • 2021-05-17
    • 1970-01-01
    相关资源
    最近更新 更多