Android Bitmap memory usage calculation formula:
The calculation method when the picture is stored in the format ARGB_8888 (4 bytes for each pixel, 2 bytes for RGB_565 ),
Occupied memory = int(scaledWidth * scale + 0.5f) * int(scaledHeight * scale + 0.5f) *4 (bytes),
where scaledWidth is the pixel width of the image, scaledHeight is the pixel height of the image, scale = targetDensityDpi / densityDpi
mdpi | hdpi | xhdpi | xxhdpi | xxxhdpi | |
---|---|---|---|---|---|
densityDpi | 160 | 240 | 320 | 480 | 560 |
Taking a mobile phone with a screen density of targetDensityDpi = 160 as an example, the calculated Bitmap memory usage is as follows:
Image position: drawable-hdpi
Image format: ARGB-8888
Image size: 400 * 400
Width: int(400 * (160 / 240) + 0.5) = 267
Height: int(400 * (160 / 240) + 0.5) = 267
Memory size: 267*267*4 = 285156B
Image position: drawable-xhdpi
Image format: ARGB-8888
Image size: 400 * 400
Width: int(400 * (160 / 320) + 0.5) = 201
Height: int(400 * (160 / 320) + 0.5) = 201
Memory size: 201*201*4 = 161604B