1

I want use many bitmap for my game canvas.

I can't Recycle bitmaps, because I have to use bitmaps for the whole section.

Do mention some guidelines to optimize the code to handle many bitmap also faster performance for my canvas based Game Application.

Currently i am using the following code to get bitmap from drawable resource,

    BitmapFactory.Options bfOptions=new BitmapFactory.Options();
    bfOptions.inSampleSize = 1;
    bfOptions.inDither=false;                     //Disable Dithering mode
    bfOptions.inPurgeable=true;                   //Tell to gc that whether it needs free memory, the Bitmap can be cleared
    bfOptions.inInputShareable=true;              //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
    bfOptions.inTempStorage=new byte[16 * 1024]; 

    myBitMap=BitmapFactory.decodeResource(getResources(),ID, bfOptions);

I am currently following this code for drawing in canvas.

the images are in sprite so while trying to load many sprite its giving problem.

In this code link MainGamePanel.java is what i am doing exactly . for bitmap decoding i use the above method.

Adam Arold
  • 29,285
  • 22
  • 112
  • 207
Ganapathy C
  • 5,989
  • 5
  • 42
  • 75

2 Answers2

1

If you have to use many bitmaps and the bitmaps can be get easily, for example, it is downloaded from internet, you can try SoftReference to refer it instead of strong reference.Have you tried this way?

jiangyan.lily
  • 932
  • 1
  • 8
  • 16
1

For really useful optimization proposals it would be beneficial to know more details about what exactly you want to optimize (drawing speed, memory footprint) and the requirements which your game canvas should fulfill as well as which kind of images you want to draw (big, small, repetitive, can they be breaked down, is tiling possible).

A few general thoughts which you could consider spring to my mind:

  • Are you sure, you can't reuse your bitmaps? I think in most games this is somehow possible. Maybe you can divide your images further?
  • You can scale the images down (or, if possible use svg images through libsvg - see https://launchpad.net/libsvg-android for details)
  • If you want to optimize the time needed for drawing, preloading your images could improve that
  • You can use a drawing thread to draw your canvas instead of relying on android to redraw the view automatically. See SurfaceHolder.lockCanvas(...) and SurfaceHolder.unlockCanvasAndPost(...) for details on that.
  • If you really need excellent drawing speed, consider using open gl.
nanoquack
  • 949
  • 2
  • 9
  • 26