Search code examples
androidsurfaceview

Android game using surfaceview, canvas - Drawable or bitmap


I am developing a game in SurfaceView. It involves around 15 - 20 images of some are large having average resolution of 320X400. I have two questions-

  1. I am using images as Drawable and using draw(canvas) method of drawable to draw images on screen. I see in some games people use Bitmap instead of Drawable to draw images on screen, what is the difference between using images as Drawable or Bitmap in android game. Which is more memory efficient and which has good performance while drawing on canvas?

  2. Right now I am creating arrays of Drawable and creating Drawable object of all images used in game and putting them into there respective array, as all images are required while gameplay. I want to know which is better - loading all images in Drawable on start or creating Drawable Object when we need it and making null others that are not shown and repeating this process each time images are shown or removed from screen. Will repeating the process of creating and nulling objects have any bad effect or is it better than loading all images at once?

Thanks


Solution

    1. According to my experience Bitmap is faster than drawable. I'm testing an app that works at 40-45 fps with drawable and with bitmap I get 57-59 fps. That's because people use them for games.

    2. If you have enough memory I recommend you to load all the images first. If you start creating and destroying objects it will call the garbage collector often and it's really slow. If you don't need a hight performance, create and destroy objects will reduce the use of memory. If you are trying to make a game maybe this link could help.

    Google I/O 2009 - Writing Real-Time Games for Android especially min 22:30 Fast java code