Bitmap内存占用计算

作者: 风风风筝 | 来源:发表于2016-09-16 18:03 被阅读571次

densityDpi表示一英寸有多少个像素点

densityDpi density drawable目录 ic_launcher大小
640 4 drawable-xxxhdpi 192x192
480 3 drawable-xxhdpi 144x144
320 2 drawable-xhdpi 96x96
240 1.5 drawable-hdpi 72x72
160 1 drawable-mdpi 48x48
160 1 drawable -
- - drawable-nodpi -

xml布局使用R.drawable.xx相当于代码
BitmapFactory.decodeResource(getResources(), R.drawable.xx)
假设当前设备的dpi是560,那么系统匹配drawable目录的顺序是:

  1. drawable-xxxhdpi
  2. drawable-nodpi
  3. drawable-xxhdpi
  4. drawable-xhdpi
  5. drawable-hdpi
  6. drawable-mdpi
  7. drawable

加载图片会经过缩放,内存占用计算公式如下

float scale = (float) deviceDensityDpi / drawableDirDensityDpi;
int scaledWidth = (int) (imgWidth * scale + 0.5f);
int scaledHeight = (int) (imgHeight * scale + 0.5f);
// 这里为什么是一个像素点占4字节,下面再说
int byteCount = scaledWidth * scaledHeight * 4;

如果最终选择的是drawable目录,经过测试,其实就和放在drawable-mdpi是一样的,从上面的公式和表格,可以得出drawableDirDensityDpi越小,最终byteCount越大。所以最坏的情况就是本该放在drawable-xxxhdpi目录的图片放到了drawable目录
如果最终选择的是drawable-nodpi目录,那么是不会经过缩放的,也就是

int byteCount = imgWidth * imgHeight * 4

再说下,为什么说一个像素点是4个字节
其实加载图片的时候还要根据BitmapFactory.Options,如果options.inPreferredConfig = Bitmap.Config.ARGB_8888那么一个像素就是占4字节,而默认就是ARGB_8888

ALPHA_8就是Alpha由8位组成=1字节
ARGB_4444就是由4个4位组成即16位=2字节
ARGB_8888就是由4个8位组成即32位=4字节
RGB_565就是R为5位,G为6位,B为5位共16位=2字节

ARGB_4444已经不建议使用,质量太差,官方有这样一句注释

@deprecated Because of the poor quality of this configuration, it is advised to use {@link #ARGB_8888} instead.


关于jpg和png
对于Android来说并没有区别,比如一张100x100的jpg,大小10k,一张100x100的png,大小15k,如果放在同一个drawable目录下,它们加载进来占用的内存是一摸一样的,只跟分辨率有关。通常jpg图片的文件大小会比png图片小,是因为jpg是有损压缩,而png是无损压缩,而且jpg没有alpha通道


Demo

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;

        // 同一张图片,每个目录都存一份
        // drawable-nodpi
        Bitmap nodpi = BitmapFactory.decodeResource(getResources(), R.drawable.nodpi);
        // drawable
        Bitmap _default = BitmapFactory.decodeResource(getResources(), R.drawable._default);
        // drawable-mhdpi
        Bitmap mhdpi = BitmapFactory.decodeResource(getResources(), R.drawable.mhdpi);
        // drawable-hdpi
        Bitmap hdpi = BitmapFactory.decodeResource(getResources(), R.drawable.hdpi);
        // drawable-xhdpi
        Bitmap xhdpi = BitmapFactory.decodeResource(getResources(), R.drawable.xhdpi);
        // drawable-xxhdpi
        Bitmap xxhdpi = BitmapFactory.decodeResource(getResources(), R.drawable.xxhdpi);
        // drawable-xxxhdpi
        Bitmap xxxhdpi = BitmapFactory.decodeResource(getResources(), R.drawable.xxxhdpi);

        Log.d("TEST", "nodpi=" + nodpi.getByteCount());
        Log.d("TEST", "default=" + _default.getByteCount());
        Log.d("TEST", "mhdpi=" + mhdpi.getByteCount());
        Log.d("TEST", "hdpi=" + hdpi.getByteCount());
        Log.d("TEST", "xhdpi=" + xhdpi.getByteCount());
        Log.d("TEST", "xxhdpi=" + xxhdpi.getByteCount());
        Log.d("TEST", "xxxhdpi=" + xxxhdpi.getByteCount());

        int realDensityDpi = getResources().getDisplayMetrics().densityDpi;
        // 根据densityDpi从不同的目录加载图片
        int drawableDirDensityDpi;
        if (realDensityDpi < 240) {
            drawableDirDensityDpi = 160;
        } else if (realDensityDpi < 320) {
            drawableDirDensityDpi = 240;
        } else if (realDensityDpi < 480) {
            drawableDirDensityDpi = 320;
        } else if (realDensityDpi < 560) {
            drawableDirDensityDpi = 480;
        } else {
            drawableDirDensityDpi = 640;
        }
        float scale = (float) realDensityDpi / drawableDirDensityDpi;
        // 默认是Bitmap.Config.ARGB_8888,所以是4字节
        // 我的设备dpi是560,所以是在xxxhdpi目录下
        int byteCount = (int) (192 * scale + 0.5) * (int) (192 * scale + 0.5) * 4;
        Bitmap ic_launcher = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        Log.d("TEST", ic_launcher.getByteCount() + " " + byteCount);
    }
}

Android 开发绕不过的坑:你的 Bitmap 究竟占多大内存?

相关文章

网友评论

    本文标题:Bitmap内存占用计算

    本文链接:https://www.haomeiwen.com/subject/gqdiettx.html