Threads and performance with Android games
I've been following this tutorial: http://www.droidnova.com/playing-with-graphics-in-android-part-iii,176.html
To get comfortable with Android. It's been very, very helpful. In it though, he uses one thread to handle input, and another for drawing to the canvas. This is my first time using threads, and I am wondering if it is wise to put the entire onDraw开发者_JS百科() method into a synchronized block. Will this cause performance problems, especially in a game handling a lot of bitmaps?
The question is actually, can you do it without synchronizing it? What will happen if you don't will it screw up the view, or even crash? In that case you will simply be forced to synchronize it. I am not really that much of an expert, but I would almost guess that you can do it in a synchronized block without getting too many problems. That very much depends on the size of the bitmaps though. If you are going to fill up the whole display with one big bitmap I guess that might give you problems indeed.
If you can do it without synchronization I would indeed avoid it. But I wonder why you are trying to do it in a synchronized block? I guess that stuff like canvas.draw will be synchronized in the correct ways by Android anyways. In this case I'd really just try and see if there are problems if you don't synchronize it.
I just took a quick peek at the tutorial though. If I get it right the onDraw() method will be called multiple times. In that case I would worry much more about the BitmapFactory.decodeResource() This will create a new Bitmap object every time and it will be cleaned by the Garbage Collector. If you are going for performance, the garbage collector is your biggest enemy!
If you got comfortable with Android and you want to continue with writing games I would take a look at this video: http://www.youtube.com/watch?v=U4Bk5rmIpic&playnext=1&videos=Ur-J-xjmaLc It is somewhat advanced, but it is pretty helpful!
精彩评论