Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android DrawBitMap very slow when using ARGB_8888

Tags:

android

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

like image 354
Radhe Shyam Avatar asked Dec 07 '10 07:12

Radhe Shyam


2 Answers

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.

like image 181
mibollma Avatar answered Oct 21 '22 06:10

mibollma


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?

like image 28
EboMike Avatar answered Oct 21 '22 06:10

EboMike