3

I'm writing an android application that draws directly to the canvas on the onDraw event of a View.

I'm drawing something that involves drawing each pixel individually, for this I use something like:

for (int x = 0; x < xMax; x++) {
  for (int y = 0; y < yMax; y++){
    MyColour = CalculateMyPoint(x, y);
    canvas.drawPoint(x, y, MyColour);
  }
}

The problem here is that this takes a long time to paint as the CalculateMyPoint routine is quite an expensive method.

Is there a more efficient way of painting to the canvas, for example should I draw to a bitmap and then paint the whole bitmap to the canvas on the onDraw event? Or maybe evaluate my colours and fill in an array that the onDraw method can use to paint the canvas?

Users of my application will be able to change parameters that affect the drawing on the canvas. This is incredibly slow at the moment.

Pentium10
  • 204,586
  • 122
  • 423
  • 502
Mattl
  • 1,588
  • 3
  • 24
  • 49
  • What is making CalculateMyPoint take so long to process? – Adam Mar 21 '10 at 14:03
  • I have a reasonably involved calculation to calculate a colour to use for any given pixel. I'm just wondering if it's better to calculate the the colour for each pixel else where instead of in the onDraw event of the View. – Mattl Mar 21 '10 at 17:30
  • Do you have to draw every pixel individually? Because thats over 400k pixels on a Droid or nexus one and I feel like there's more opportunity to optimize in the number of calls of CalculateMyPoint than in it's runtime. – jqpubliq Mar 21 '10 at 17:54
  • Can you show us what type of image the code is trying to draw? Surely there is a more efficient way or rendering the drawing than by drawing each pixel individually. – Eric Rowell Sep 19 '11 at 19:17
  • Thanks for your comments. It's the Mandelbrot set, and I was trying to implement pinch zooming and support deep zooms but have had performance issues. I may revisit the project when I get a moment. – Mattl Sep 20 '11 at 10:18

1 Answers1

5

As people have pointed out, if CalculateMyPixel() is expensive, having that called 150,000 (HVGA) or 384,00 times (WVGA) is just going to kill you.

On top of that, trying to draw your UI as individual pixels via canvas.drawPoint() each time there is an update is just about the least efficient way to do this.

If you are drawing individual pixels, you almost certainly want to have some kind of off-screen bitmap containing the pixels, which you draw with a simple Canvas.drawBitmap().

Then you can decide the best way to manage that bitmap. This simplest is to just make a Bitmap object of the desired size and use the APIs there to fill it in.

Alternatively, there is a version of drawBitmap() that takes a raw integer array, so you can fill that in directly with whatever values you want, avoiding a method call for each pixel.

Now you can move your pixel calculation out of the onDraw() method, which needs to be fast to have a responsive UI, and fill your pixels in somewhere else. Maybe you compute them once at initialization time. Maybe you compute them, and do selective updates of only the parts that have changed before calling invalidate() (for example the pixels from (0,0)-(10,l0) have changed so take the current bitmap and just modify that area). If computing pixels is really just intrinsically slow, you will probably want to create a separate thread to do that work in (updating the bitmap with the new pixels, then postInvalidate() on the UI to get them drawn).

Also now that you have your pixels in a bitmap, you can do tricks like make the size of the bitmap smaller and scale it when drawing to the screen, allowing you to take much less time updating those pixels while still filling the entire UI (albeit at a lower resolution).

hackbod
  • 90,665
  • 16
  • 140
  • 154
  • Thank you, I guess this is all common sense.
    At the moment it takes about 1 minute to initialise the application with the code in my OnDraw routine, anything over 1 second is probably unacceptable.
    This call can initialise my bitmap with the array of pixels I need:
    public static Bitmap createBitmap (int[] colors, int width, int height, Bitmap.Config config)
    It is also true that I really don't need WVGA resolution so I'll experiment with scaling.
    – Mattl Mar 22 '10 at 12:06
  • I spent the evening following these tips and got my rendering from nearly a minute down to 3 seconds! I think I can shave another second off by optimising further. Thanks again hackbod. – Mattl Mar 23 '10 at 10:34