Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Canvas.DrawBitmap VS Drawable.Draw - Huge performance boost

Tags:

android

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.

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 to blame. It's the actual Drawable.Draw that slows things down.

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?

like image 405
nitzanj Avatar asked Nov 28 '10 11:11

nitzanj


1 Answers

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

like image 145
Wroclai Avatar answered Sep 28 '22 17:09

Wroclai