使用ARGB_8888时,Android DrawBitMap非常慢

时间:2010-12-07 07:04:29

标签: android

我发现DrawBitMap需要50-60毫秒才能绘制三个位图,一个是占据全屏的矩形,一个是圆形,另一个是路径。我的位图是通过在空白位图上使用Canvas.drawPath,drawRect和drawCircle创建的,其中Bitmap.Config为ARGB_8888。 我正在使用ARGB_8888使背景可见以获得分层效果。 我很震惊地发现时间大约为50ms,因为我认为drawBitmap是一个非常简单的操作。有人可以指导我是否有任何根本性的错误。以下是我的代码

创建空白位图

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);

在适当的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);

在OnDraw中绘制BitMap

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);

现在,当我记录这三个drawBitMap的时间时,我发现它需要大约50ms 代码中是否存在重大错误。将Bitmap.Config更改为RGB_565会将时间缩短到8毫秒左右,但背景不可见,我在路径周围会出现一个黑框

3 个答案:

答案 0 :(得分:6)

看起来很正常。 画布的透明度非常慢。

您可以尝试切换到OpenGL ES或尽可能少透明地设计内容,以便尽可能频繁地使用RGB_565。

答案 1 :(得分:2)

您应该始终匹配屏幕的格式。最近有一个非常相似的问题,Romain提到如果格式匹配,blits基本上会变成memcpys。当然,请确保您没有使用深奥的blit模式。

另外,如果你没有缩放/旋转任何东西,为什么还要使用消除锯齿?

至于565没有工作 - 我只是略过你的代码。你在使用alpha通道吗?你的位图到底是什么样的?

答案 2 :(得分:0)

其中一位Android开发者解释了此here。要快速绘制ARGB_8888,您需要绘制到32位窗口。请参阅文章底部的基准。