1. Basic knowledge
Note: Here inDensity represents the DPI of the target image (under which resource folder), inTargetDensity represents the DPI of the target screen, so you can see that inDensity and inTargetDensity stretch the width and height of the Bitmap. To change the memory footprint of the Bitmap.
ARGB_8888/RGB_565
- ARGB_8888 takes up memory, has high resolution and supports transparency
- RGB_565 saves memory, has moderate clarity and does not support transparency
Enumeration variable
public static final Bitmap.Config ALPHA_8
public static final Bitmap.Config ARGB_4444
public static final Bitmap.Config ARGB_8888
public static finalBitmap.Config RGB_565 bitmap. Config ARGB_4444:// Each pixel has four bits, i.e. A=4, R=4, G=4, B=4, then A pixel has 4+4+4+4=16 bitsBitmap. Config ARGB_8888:// Each pixel has four bits, i.e. A=8, R=8, G=8, B=8, then A pixel has 8+8+8+8=32 bitsBitmap. Config RGB_565:// Each pixel has four bits, i.e. R=5, G=6, B=5, and no opacity, so a pixel has 5+6+5=16 bitsBitmap. Config ALPHA_8:// Each pixel is four bits, only transparency is saved, no color is saved.
Copy the code
Use the point
ARGB_4444 is not recommended and the effect of ALPHA_8 is unknown. Most of us still use ARGB_8888 and RGB_565. RGB_565 is a solution to the OOM by reducing memory overhead while maintaining image quality. ** It is important to note that RGB_565 has no transparency. ** If the image itself needs to be transparent, then RGB_565 should not be used.
How to calculate the size of memory occupied by Bitmap
- Bitamp Memory size = Width Pixel x (inTargetDensity) x Height Pixel x (inTargetDensity) x Memory occupied by a pixel
Method to get the size of the memory footprint
- GetByteCount () : API12 is added to represent the minimum amount of memory required to store Bitmap pixels.
- GetAllocationByteCount () : Added by API19 to represent the size of memory allocated for bitmaps in memory, replacing the getByteCount() method.
It is safer to use Bitmap with proper use of API
- BitmapFactory. Options. InPreferredConfig: ARGB_8888 RGB_565 instead, change the encoding, save memory.
- BitmapFactory. Options. InSampleSize: scaling, can the library reference Luban, wide high calculate the appropriate scaling according to the pictures.
- BitmapFactory. Options. InPurgeable: can make the system out of memory when the recovery of memory.
2:How to load large hd images without compression?
-
Using BitmapRegionDecoder
-
SetInputStream to get the true width and height of the image, and initialize our mDecoder
-
The onMeasure assigns the recT of our display area to the size of the view
-
In onTouchEvent, we listen for the move gesture, change the rect parameter in the callback, perform boundary checking, and finally invalidate
-
In onDraw, you get the bitmap according to recT and draw it
-