安卓:Canvas.DrawBitmap VS Drawable.Draw - 巨大的性能提升巨大、性能、DrawBitmap、Canvas

2023-09-05 02:33:35 作者:活着 ‰

我刚刚发现了一些东西,我想知道如何和为什么。 我正在开发一个小的街机游戏为Android。我决定忽略OpenGL和使用标准SurfaceView和可绘做到这一点,因为它的假设是轻(10精灵左右)。 我有我加载可绘,而我使用的方法绘制,并通过他们我的画布。这每一个精灵如何绘制到屏幕上。 那么事实证明,绘制4-5大精灵(200X400左右)需要很长的时间低于全新的手机型号。足够长的时间让我的游戏无法播放。我们谈论的是50-60毫秒绘制使用这种方法的一个帧。我真的不从图纸做任何事情有分开,我无处可降低成本。所以我决定尝试使用位图来代替。然而,这里,我需要$ P $对设定的尺寸,因为有在一个位图没有的setBounds'方法。没有概率,我调整它们的大小以适合我的负载电流屏幕上,问题就解决了​​。

I just found out something and I was wondering about how and why. I'm developing a small arcade game for Android. I decided to ignore OpenGL and use the standard SurfaceView and Drawables to do it, since it's suppose to be light (10 sprites or so). I have drawables that I load, and I use the method Draw and passing them my canvas. This how every sprite is drawn to the screen. Well it turns out that drawing 4-5 big sprites (200X400 or so) takes a long time on less-than-brand-new phone models. Long enough to make my game unplayable. We're talking about 50-60 milliseconds to draw a single frame using this method. And I really don't do anything there apart from drawing, nowhere I can cut costs. So I decided to try and use Bitmaps instead. Here, however, I need to pre-set the size, since there's no 'setBounds' method in a bitmap. No prob, I resize them to fit my current screen on load, problem solved.

确定。所以,我得到了位图。我用Canvas.DrawBitmap现在画画。我长椅新的绘制方法..和我收到了百日咳 400%的性能提升!而是50-60ms的,整个抽签循环现在需要8-12ms。 什么是地狱? 为了排除这种可能,我计时了的setBounds也一样,它需要与LT; 1毫秒,所以它不是太不争气。这是该会减慢速度实际Drawable.Draw。

OK. So I got bitmaps. I use Canvas.DrawBitmap now to draw. I bench the new draw method.. and I get a whooping 400% performance boost! Instead of 50-60ms, the entire draw loop now takes 8-12ms. What the hell?? To rule it out, I timed the setBounds too, it takes <1ms so it's not too blame. It's the actual Drawable.Draw that slows things down.

对于我来说,这是个好消息,因为我真的不想学的OpenGL,使我的游戏可玩的,但我不能停止想知道它 - 它是罚款?是否有问题,我的方法是什么?为什么不提及任何地方?

For me this is great news, since I really didn't want to learn OpenGL to make my game playable, but I can't stop wondering about it - Is it fine? are there problems with my method? Why isn't it mentioned anywhere?

推荐答案

SurfaceView 你的画布的意思使用时,你应该不断地迭代和绘制对象不是用于这一目的。

The SurfaceView of your Canvas is meant to be used when you should iterate constantly and Drawable is not for that purpose.