Android game using surfaceview, canvas - Drawable or bitmap

I am developing a game in SurfaceView . It includes about 15-20 images, some of which have an average resolution of 320X400. I have two questions -

  • I use images as Drawable and using the draw(canvas) method for drawing to draw images on the screen. In some games, people use Bitmap instead of Drawable to draw images on the screen, what is the difference between using images as Drawable or Bitmap in an Android game. Which is more efficient with memory and has good performance when drawing on canvas?

  • Now I create Drawable arrays and create a Drawable object of all the images used in the game, and put them in the corresponding array, since all the images are needed during the game. I want to know which is better - load all the images into Drawable at startup or create a Drawable Object when we need it, and do zero others that don’t appear, and repeat this process every time the images are displayed or deleted from the screen. Will the repeated process of creating and nullifying objects have any bad effect or better than loading all images at once?

thanks

+4
source share
2 answers
  • In my experience, a bitmap is faster than possible. I am testing an application that runs at a speed of 40-45 frames per second with the ability to draw and with a bitmap. I get 57-59 frames per second. This is because people use them for games.

  • If you have enough memory, I recommend downloading all the images first. If you start to create and destroy objects, it often calls the garbage collector, and it is very slow. If you don't need high performance, creating and destroying objects will reduce memory usage. If you are trying to create a game, perhaps this link may help.

Google I / O 2009 - Writing real-time games for Android , especially min 22:30 Fast Java code

+3
source

Speaking from personal experience, you should go Bitmaps , not Drawables . This will give better results.

The bitmap will suit your best settings, since all you need (as far as I can tell) is the image that will be displayed on the screen.

A Drawable has a wider reach (documentation from Android Dev.):

A Drawable is a general abstraction for "something that can be drawn." Most often, you will consider Drawable as a type of resource retrieved to draw objects on the screen; The Drawable class provides a common API for working with the main visual resource, which can take various forms. Unlike a view, Drawable does not have any means of receiving events or otherwise interacting with the user.

Definitely upload all your bitmaps before the game starts; This is a clean approach. If you do not, your game may lag behind the GC.

You can create a class containing all the static Bitmap fields.

 public static Bitmap foo; 

Then in your activity class, you can load these bitmaps (usually I put my bitmaps in the Assets folder):

Note. It is probably best to create a class called LoadScreen or SplashScreen that takes care of all initialization. If you do this like this, you will have to pass the link to your activity class to this class.

 Assets.foo = readAssetsBitmap("foo.png"); public Bitmap readBitmapFromMemory(String filename) { Bitmap defautBitmap = null; File filePath = getFileStreamPath(filename); FileInputStream fi; try { fi = new FileInputStream(filePath); defautBitmap = BitmapFactory.decodeStream(fi); } catch (FileNotFoundException e) { e.printStackTrace(); } return defautBitmap; } 

When the user decides to exit the game, you must unload all the bitmaps as follows:

 disposeBitmap(Assets.foo); public void disposeBitmap(Bitmap bitmap) { bitmap.recycle(); bitmap = null; } 

Hope this helps.

+2
source

Source: https://habr.com/ru/post/1388333/


All Articles