Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to optimize Canvas drawing - drawBitmap on Android?

Tags:

android

canvas

I've done my profiling and it seems that most of my time is spent during drawBitmap, which is called (understandingly) on every frame.

I use SurfaceView/updating thread/canvas locking approach as demonstrated in LunarLander sample. But I've changed it (according to this question) as to

  • on the very first frame construct a bitmap buffer and paint onto it
  • paint each subsequent frame reusing that Bitmap (not everything on my screen changes)
  • at the end of each frame paint the buffer once into the target Canvas (to the screen)

Traceview showed me that this drawBitmap takes 5ms for each frame on my 800x480 device. Can I get any better than that or is it just something that is 'carved into the stone' and I just have to optimize other parts of the code to achieve good frames per second?

like image 542
Axarydax Avatar asked Dec 01 '10 22:12

Axarydax


1 Answers

It depends on many things, but usually drawBitmap() will be as fast as it can get. In your particular case, if you don't need blending, make sure your are using an opaque bitmap. In addition, try to use a Bitmap in a format compatible with your Surface. For instance, if you are using a 16 bits Surface, drawing a 16-bits (RGB565) bitmap will be very fast (it's just a memcpy call.) If your Surface is 32 bits, use an ARGB8888 opaque Bitmap.

like image 83
Romain Guy Avatar answered Sep 18 '22 03:09

Romain Guy