在Android开发中,我现在发现很多人还不会对图片占用内存进行很好的计算。
因此撰写该博文来做介绍,期望达到抛砖引玉的作用。
Android中一张图片(BitMap)占用的内存主要和以下几个因数有关:图片长度,图片宽度,单位像素占用的字节数。 一张图片(BitMap)占用的内存=图片长度*图片宽度*单位像素占用的字节数 注:图片长度和图片宽度的单位是像素。 图片(BitMap)占用的内存应该和屏幕密度(Density)无关,虽然我暂时还拿不出直接证据。 创建一个BitMap时,其单位像素占用的字节数由其参数BitmapFactory.Options的inPReferredConfig变量决定。 inPreferredConfig为Bitmap.Config类型,Bitmap.Config类是个枚举类型,它可以为以下值Enum Values | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Bitmap.Config | ALPHA_8 | Each pixel is stored as a single translucency (alpha) channel. | |||||||||
Bitmap.Config | ARGB_4444 | This field is deprecated. Because of the poor quality of this configuration, it is advised to use ARGB_8888 instead. | |||||||||
Bitmap.Config | 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的默认格式。 | |||||||||
Bitmap.Config | 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 |
新闻热点
疑难解答