參考資料
Android開發(fā)繞不過的坑:你的Bitmap究竟占多大內(nèi)存业岁?
相關(guān)因素
Bitmap 在內(nèi)存當(dāng)中占用的大小其實取決于:
- 色彩格式,前面我們已經(jīng)提到粟焊,如果是 ARGB8888 那么就是一個像素4個字節(jié),如果是 RGB565 那就是2個字節(jié)
- 原始文件存放的資源目錄
- 目標(biāo)屏幕的密度(所以同等條件下,紅米在資源方面消耗的內(nèi)存肯定是要小于三星S6的)
計算公式
int realWidth = (int) (rawWidth * targetDensity / (float) rawDensity + 0.5f)
int realHeight = (int) (rawHeight * targetDensity / (float) rawDensity + 0.5f)
int memory = realWidth * realHeight * bytes_for_current_colorMode;
- rawWidth就是資源圖片的原始寬度
- targetDensity就是當(dāng)前屏幕的density
- rawDensity就是資源圖片所在的資源文件夾對應(yīng)的density
- bytes_for_current_colorMode就是當(dāng)前色彩格式下每個像素對應(yīng)的字節(jié)數(shù)
BitmapFactory.Options:
If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory.