6

I find that DrawBitMap is taking 50-60 ms for drawing just three bitmaps one is a rectangle occupying the full screen, one is a circle and another one is a Path. My bitmaps are created by using Canvas.drawPath, drawRect and drawCircle on a blank bitmap with the Bitmap.Config as ARGB_8888. I am using ARGB_8888 to make the background visible to get a layering effect. I was shocked to find the time taken as around 50ms as I thought drawBitmap would be a very simple operation. Can someone guide as to is there any fundamental mistake that I am making. Following is my code

Creating the Blank Bitmaps

Rectangle = Bitmap.createBitmap(320,480,Bitmap.Config.ARGB_8888);
Circle = Bitmap.createBitmap(70,70,Bitmap.Config.ARGB_8888);
Leaf1 = Bitmap.createBitmap(20,30,Bitmap.Config.ARGB_8888);

Drawing the Shapes on the appropriate BitMap

Canvas c = new  Canvas(Rectangle);
Paint p = new Paint();
p.setAntiAlias(true);
p.setColor(0xff6e8b3e);
c.drawRect(0,0,320,480,p);

Canvas c = new Canvas(Circle);
Paint p = new Paint();
CirclePath = new Path();
p.setAntiAlias(true);
p.setColor(0xffcd661d);
System.out.println("x = "+x+" y = "+y);
CirclePath.addCircle(50,50,10,Path.Direction.CW);
c.drawPath(CirclePath,p);

Canvas c = new  Canvas(Leaf1);
Paint paint = new Paint();
Path path = new Path();
paint.setAntiAlias(true);
path.moveTo((float)184.37,(float)219.15);
path.cubicTo((float)188.32,(float)219.15,(float)192.88,(float)220.44,(float)195.62,(float)223.54);
path.cubicTo((float)197.84,(float)226.05,(float)203.2,(float)229.84,(float)198.18,(float)245.98);

Drawing the BitMap in OnDraw

canvas.drawBitmap(Rectangle,0,0,p);
canvas.translate(x,y); // For animation effect
canvas.drawBitmap(Circle,0,0,p);
canvas.drawBitmap(Leaf1,0,0,p);

Now when I record the time taken for this three drawBitMap I find it is taking around 50ms Is there something big time mistake in the code. Changing the Bitmap.Config to RGB_565 brings the time down to around 8ms but then the background is not visible and I am getting a black box around the path

EboMike
  • 76,846
  • 14
  • 164
  • 167
Radhe Shyam
  • 61
  • 1
  • 1
  • 2

3 Answers3

7

Looks normal. Canvas is very slow on transparency.

You can either try to switch to OpenGL ES or design your content with as little transparency as possible so you can use RGB_565 as often as possible.

mibollma
  • 14,959
  • 6
  • 52
  • 69
  • +1. If you do lots of non-trivial stuff (rotate, zoom, alpha, anti-aliasing, etc), use OpenGL instead. If you don't, be sure to turn it off. – EboMike Dec 07 '10 at 07:24
  • Hi mibollma, The transparency is required only around the image so the background is visible. The image is created programmatically using drawPath and since createBitmap takes a rectangular size, unless I specify transparency background is not visible outside the image but within the image box – Radhe Shyam Dec 07 '10 at 11:08
2

You should always match the format of your screen. There was a very similar question recently, and Romain mentioned that blits essentially turn into memcpys if the format matches. And, of course, make sure that you're not using an esoteric blit mode.

Also, why are you using anti-aliasing if you're not scaling/rotating anything?

As for 565 not working - I'm just skimming over your code. Are you using the alpha channel? What exactly do your bitmaps look like?

EboMike
  • 76,846
  • 14
  • 164
  • 167
  • Yes alpha channel is being used as I want the background to show properly around the image. And Yes the aim is to do rotate and scale as well so I am using anti-aliasing. Basically I am designing a SVG player which does vector graphics animation so the bitmap is constructed using drawPath functions and then bitmap is drawn on the screen for each frame – Radhe Shyam Dec 07 '10 at 10:41
0

One of the Android devs explains this here. To draw ARGB_8888 quickly you need to draw to a 32-bit window. See bottom of article for benchmarks.

cuzi
  • 978
  • 10
  • 21
mdm
  • 5,528
  • 5
  • 29
  • 28