美文网首页
计算Bitmap占用的内存大小

计算Bitmap占用的内存大小

作者: yuansip | 来源:发表于2016-10-28 12:13 被阅读0次

参考资料

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

相关因素

Bitmap 在内存当中占用的大小其实取决于:

  • 色彩格式,前面我们已经提到,如果是 ARGB8888 那么就是一个像素4个字节,如果是 RGB565 那就是2个字节
  • 原始文件存放的资源目录
  • 目标屏幕的密度(所以同等条件下,红米在资源方面消耗的内存肯定是要小于三星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就是当前屏幕的density
  • rawDensity就是资源图片所在的资源文件夹对应的density
  • bytes_for_current_colorMode就是当前色彩格式下每个像素对应的字节数

BitmapFactory.Options:

If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory.

相关文章

网友评论

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

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