Search code examples
javaandroidandroid-canvas

lockCanvas() returns different canvases


Trying to draw something every ms on single canvas. I mean only adding details to canvas, not redrawing it all every frame. So this code gives me three different canvases. Third, then first again. Why?

public void run() {
    this.run = true;
    Canvas canvas = null;
    while (run) {
        try {
            canvas = this.surfaceHolder.lockCanvas();
            synchronized (this.surfaceHolder) {
                Thread.sleep(delay);
                draw(new Img(canvas, size));
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            if (canvas != null) {
            this.surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }
        synchronized (this) {
            if (wait) {
                try {
                    wait();
                } catch (Exception e) {}
            }
        }
    }
}

If it is android triple buffering - how to turn it off, or do something with it? Android 4.2.1


Solution

    1. I would recommend against naming a boolean field, "run" in a Runnable implementation (in which the method returns void). Even if problems don't surface from the conflict, it's confusing. Maybe "running", or something (anything), would make more sense - easier to debug.

    2. Don't use Object.wait when you are multi-threading. It won't always (generally, will not) act as you might expect.

    3. You are most likely getting multiple instances of your Canvas member because somewhere (maybe in the Android framework, or maybe in your code... hard to tell), "new Canvas(args)" is being called while what you believe to be your only Canvas instance is out on another thread. While you have only one reference, more than one instance can be created.

    4. I wouldn't recommend using synchronized(whatever) unless you are sure you need to do so.

    5. Hang in there. This problem is very confusing - I worked through it last Spring and it wasn't easy or fun.

    Hope any of the above helps in some way.

    -Brandon