开发者

Optimizations of custom ImageView that frequently refreshes by calling onDraw

I have created a custom ImageView and in its onDraw method I need to draw some bitmaps based on user interaction like touch. Everything is working fine however slowly as I start adding more and more bitmap the application really slows down.

This is what I do in my onDraw of the Custom ImageView

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

    Log.e(TAG, "onDraw called");

    for (int i=0; i < bitmapList.size(); i++){
        drawBitmap(canvas, bitmapList.get(i));
    }
}

As you can see, I am redrawing all the bitmaps in the List everytime onDraw is called naturally when the number of bitmap exceeds say 4-5 the operation becomes very expensive and slows the application down.

Any solution to this proble as to how can this be optimized?

  1. Can calling drawBitmap in a different thread make the operation less expensive?

  2. Is there a way to keep a copy of the previous canvas and then simply restore it in onDraw rather than drawing all the bitmaps again?

  3. The question essentia开发者_运维知识库lly is refreshing the View with lots of dynamic images on it and its optimization.


You should create a Bitmap which size must be equal to the ImageView's image size and draw all the bitmaps from the bitmapList on this bitmap only once. On every onDraw() call you should draw only this bitmap. When the bitmapList changes, this additional bitmap must be recreated.


You could use a backbuffer image to draw all your images into whenever they change, and then draw only the backbuffer image to the screen in onDraw()

// create backbuffer and draw it as seldom as possible
final Bitmap bufferBitmap = Bitmap.createBitmap(320, 240, Bitmap.Config.ARGB_8888);
final Canvas bufferCanvas = new Canvas(bufferBitmap);
for (int i=0; i < bitmapList.size(); i++){
  drawBitmap(bufferCanvas, bitmapList.get(i));
  }

// draw the buffered image as often as needed
canvas.drawBitmap(bufferBitmap ...);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜