How to boost android canvas performance with a buffered Image

When it comes to drawing to canvas on android one very big performance boost can be gained by using a buffered bitmap image. The idea is to draw lines to a bitmap. And then draw the bitmap to the screen.
This approach avoids unnecessarily redrawing elements that has been drawn already multiple times.

1) Create a bitmap in your view.

2) Remember all touch events.

3) Process all touch events. (Draw lines or something)

4) Draw to buffered bitmap image.

lineManager.draw():

5) And finally draw the buffered bitmap image.

Bonus: invalidate canvas only around finger area and only every 90 milliseconds.

See also

Performance in 2D Canvas App / Game on Android and Traceview War Story

Facebook comments:

comments

2 Comments

  1. Hello , the drawn path is not smoothly, why? My paint is :

    this.mPaint = new Paint();
    this.mPaint.setAntiAlias(true);
    this.mPaint.setFilterBitmap(true);
    this.mPaint.setDither(true);
    this.mPaint.setColor(Color.RED);
    this.mPaint.setStyle(Paint.Style.STROKE);
    this.mPaint.setStrokeJoin(Paint.Join.ROUND);
    this.mPaint.setStrokeCap(Paint.Cap.ROUND);
    this.mPaint.setPathEffect(new CornerPathEffect(10) );
    this.mPaint.setStrokeWidth(12);

  2. Invalidate with a rect behaves the same as Invalidate without a rect, causing the ondraw method to be very slow as it redraws the entire view.

Leave a Reply to LiYa Cancel reply