一张100px*100px的图片在内存中会占用多大内存

转: 计算一张100px*100px的图片在内存中会占用多大内存

    转: http://www.cnblogs.com/YuangPong/p/6694512.html

  在实际开发当中我们经常会忽视如题问题,只是知道图片越小越好,甚至根本不知道如何计算,今天笔者就抛砖引玉一把!

  Android中一张图片(Bitmap)占用的内存主要和以下几个因数有关:图片长度,图片宽度,单位像素占用的字节数。

  计算方法:一张图片(Bitmap)占用的内存=图片长度*图片宽度*单位像素占用的字节数

长度和宽度不用多做解释 ,单位是像素;关键是单位像素占用的字节数如何获得呢?

这里不得不提一下Bitmap创建的API

   Bitmap bitmap = BitmapFactory.decodeResource(Resources res,int id,BitmapFactory.Options opts);

  单位像素占用的字节数由其参数BitmapFactory.Options的inPreferredConfig变量决定,默认为Bitmap.Config.ARGB_8888

  inPreferredConfig为Bitmap.Config类型,

  Bitmap.Config类是个枚举类型,它可以为以下值

ALPHA_8Each pixel is stored as a single translucency (alpha) channel. 
This is very useful to efficiently store masks for instance. No color information is stored. With this configuration, each pixel requires 1 byte of memory.
此时图片只有alpha值,没有RGB值,一个像素占用1个字节
ARGB_4444This field is deprecated. Because of the poor quality of this configuration, it is advised to use ARGB_8888instead.  
这种格式的图片,看起来质量太差,已经不推荐使用。
Each pixel is stored on 2 bytes. The three RGB color channels and the alpha channel (translucency) are stored with a 4 bits precision (16 possible values.) This configuration is mostly useful if the application needs to store translucency information but also needs to save memory. It is recommended to use ARGB_8888 instead of this configuration.
一个像素占用2个字节,alpha(A)值,Red(R)值,Green(G)值,Blue(B)值各占4个bites,共16bites,即2个字节
ARGB_8888 Each pixel is stored on 4 bytes. Each channel (RGB and alpha for translucency) is stored with 8 bits of precision (256 possible values.) This configuration is very flexible and offers the best quality. It should be used whenever possible
一个像素占用4个字节,alpha(A)值,Red(R)值,Green(G)值,Blue(B)值各占8个bites,共32bites,即4个字节
这是一种高质量的图片格式,电脑上普通采用的格式。它也是Android手机上一个BitMap的默认格式。
RGB_565 Each pixel is stored on 2 bytes and only the RGB channels are encoded: red is stored with 5 bits of precision (32 possible values), green is stored with 6 bits of precision (64 possible values) and blue is stored with 5 bits of precision. This configuration can produce slight visual artifacts depending on the configuration of the source. For instance, without dithering, the result might show a greenish tint. To get better results dithering should be applied. This configuration may be useful when using opaque bitmaps that do not require high color fidelity.
一个像素占用2个字节,没有alpha(A)值,即不支持透明和半透明,Red(R)值占5个bites ,Green(G)值占6个bites  ,Blue(B)值占5个bites,共16bites,即2个字节.对于没有透明和半透明颜色的图片来说,该格式的图片能够达到比较的呈现效果,相对于ARGB_8888来说也能减少一半的内存开销。因此它是一个不错的选择。另外我们通过android.content.res.Resources来取得一个张图片时,它也是以该格式来构建BitMap的.
从Android4.0开始,该选项无效。即使设置为该值,系统任然会采用 ARGB_8888来构造图片

所以一张100px*100px的图片在内存中占用的内存数为:

图片格式计算公式占用内存大小
ALPHA_8100*10010000b
ARGB_4444100*100*220000b
ARGB_8888100*100*440000b
RGB_565100*100*220000b

 综上就是今天笔者要分享的小知识,一般这样的问题面试官会问。你get到了吗?

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值