在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_8888instead. | |||||||||
| 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 | |||||||||
新聞熱點
疑難解答