Android - 动态缩放比预缩放更快,wtf?
我正在尝试编写我的第一个 Android 游戏,其中包含带有位图的移动圆形对象。
我解码 png:
moon=BitmapFactory.decodeResource(resources, R.drawable.moon);
然后,对于每个 Ball 对象,我在开始时执行此操作(它们可以具有不同的大小):
scaledMoon=Bitmap.createScaledBitmap(game.moon, (int)(r*2), (int)(r*2), true);
当在画布上绘图时,我执行此操作:
c.drawBitmap(scaledMoon,x,y, null);
使用 100 个对象执行此操作,我得到的渲染时间几乎恒定为 22ms 。
但是当我这样做时(之前没有缩放!)
c.drawBitmap(game.moon, new Rect(0,0,game.moon.getWidth(),game.moon.getHeight()), new Rect((int)(x-r),(int)(y-r),(int)(x+r),(int)(y+r)), null);
我得到了 17 毫秒的渲染时间...
当只绘制一个圆圈时,
c.drawCircle(x, y, r, color);
我得到了 24 毫秒
什么......?
I am trying to program my first Android game, which contains moving circular objects with a bitmap over them.
I decode the png:
moon=BitmapFactory.decodeResource(resources, R.drawable.moon);
Then, for each Ball object I do this in the beginning (they can be different sized):
scaledMoon=Bitmap.createScaledBitmap(game.moon, (int)(r*2), (int)(r*2), true);
And when drawing on the canvas I do this:
c.drawBitmap(scaledMoon,x,y, null);
Doing this with 100 objects, I get a nearly constant render time of 22ms.
But when I do it like this (without scaling before!)
c.drawBitmap(game.moon, new Rect(0,0,game.moon.getWidth(),game.moon.getHeight()), new Rect((int)(x-r),(int)(y-r),(int)(x+r),(int)(y+r)), null);
I get 17ms render time...
When drawing just a circle
c.drawCircle(x, y, r, color);
I get 24ms
What the ...?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
渲染速度最快的第三行是因为您重复使用相同的位图,而不是用您之前尝试预加载的所有位图填充视频内存;它不是创造一个新的,它只是为你想要画的东西设定界限。至于绘制一个圆需要 24 毫秒,我最好的选择是计算和绘制点比绘制一个简单的矩形需要更长的时间。
your 3rd line that renders the fastest is because your reusing the same bitmap instead of filling video memory with all the bitmaps you tried preloading before; Its not making a new one, its just setting the bounds for what you want to draw. As for 24ms for drawing a circle, my best bet is that it takes longer to calculate and draw the points then a simple rectangle.