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.

1
  • 1
    You'll need to post some of that code for people to give optimization guidelines. Commented Oct 28, 2011 at 11:05

2 Answers 2

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.
Sign up to request clarification or add additional context in comments.

Comments

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?

3 Comments

i am taking all those from local drawable only ...i have mentioned in question itself..
I mean you can use SoftReference to avoid keep too much drawable objects in memory, could you post more code?
i am following the following link obviam.net/index.php/sprite-animation-with-android

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.