简体   繁体   中英

Android Java - Preventing GC_FOR_ALLOC with large quantity of bitmaps

I would like to know if there are any optimizations that could be used to improve the speed when using a large quantity of bitmaps drawn on a screen.

I use a canvas which I load all my resources at the initialization and use createBitmap when I need to update the bitmap. I use ~10-15kb files with my Galaxy Note 3 and notice a lag (xxhdpi) when I reach around 20 bitmaps which gets nearly unusable around 35+.

I am using createbitmap constantly because the bitmaps use frame animation and matrix to rotate.

So far the only thing i've tried that i've noticed a difference is inBitmap which gives about 5-10% increase in the GC_FOR_ALLOC freed.

Anyone care to chime in on a good answer for what is better? I've heard flash AIR is a good choice to go with using cacheAsBitmapMatrix, but I would like a different option (just personal pref).

EDIT:

(rectf bounds = bitmap bounds)

matrix.setRotate(rotation, rectf.centerX(), rectf.centerY());
ship1 = Bitmap.createBitmap(ship1, 0, 0, ship1.getWidth(), ship1.getHeight(), matrix, true);

I think I understand my problem, I should be calling

canvas.drawBitmap(ship1, matrix, paint);

But in my onDraw method I am using

canvas.drawBitmap(ship1, srcRectf, dstRectf, paint); //srcRectf = null

I use dstRectf to move my bitmap around, but I suppose this can be replaced with setTranslate. I'll try it out, thanks Mehmet!

Bitmap stores the pixel data in the native heap*, not in the java heap, and takes care of managing it by itself. That would mean GC shouldn't give you any serious headaches.

The problem is probably constantly using createBitmap() , which is usually a really costly operation. This will make a disk IO call at worst, or a relatively big memory allocation at best. You would like to use it as little as possible, ie only when initially reading them from the disk.

Instead I advise you to use a Matrix in conjunction with a Canvas . Just change your Matrix and with each step repaint your Bitmap s with it.

EDIT:

*Only correct for Android 2.3.3 <-> Android 3.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM