0

I have a custom View that is drawn to on a background thread, simliar to the BallThread defined here.

In my particular application, I am drawing a line of pixels, each with a unique color. As the next set of data comes in, the curretn drawn pixels are shifted up, a new row is added, and everything is redrawn. The panel also supports panning and zooming.

My problem is that as time goes on and I have more pixels to track and be redrawn, the animation for panning and zooming becomes more bogged down. I feel that the amount of things I need to draw seems to exceed the limitations of what a Canvas can support in a timely fashion.

class DrawingThread extends Thread {
    private SurfaceHolder _surfaceHolder;
    private DrawingView _panel;
    private boolean _run = false;

    public DrawingThread(SurfaceHolder surfaceHolder, DrawingView panel) {
        _surfaceHolder = surfaceHolder;
        _panel = panel;
    }

    public void setRunning(boolean run) {
        _run = run;
    }

    public SurfaceHolder getSurfaceHolder() {
        return _surfaceHolder;
    }

    @Override
    public void run() {
        Canvas canvas;
        while (_run) {
            canvas = null;
            try {
                canvas = _surfaceHolder.lockCanvas(null);
                synchronized (_surfaceHolder) {
                    _panel.doDraw(canvas);
                }

                Thread.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                // do this in a finally so that if an exception is thrown
                // during the above, we don't leave the Surface in an
                // inconsistent state
                if (canvas != null) {
                    _surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }
        }
    }
}

Is there something else I can be doing that can process pixel draws (e.g. canvas.drawPixel()) faster than a Canvas?

Community
  • 1
  • 1
Stealth Rabbi
  • 10,156
  • 22
  • 100
  • 176
  • doDraw() is what's doing a the thousands of canvas.drawPixel() calls – Stealth Rabbi Aug 13 '14 at 21:17
  • The abilities of Canvas for such graphics is fairly limited. You will probably get a lot of improvement if you move to OpenGL ES. – Code-Apprentice Aug 13 '14 at 21:26
  • If, in `doDraw()`, you're re-drawing every single data point, you'd probably get better results if you'd cache the previously drawn items to an off-screen Bitmap, drawing that first each time through, and just drawing the new data points. – Mike M. Aug 13 '14 at 21:41

0 Answers0