Android中有四種,分别是:
ALPHA_8:每個像素占用1byte記憶體
ARGB_4444:每個像素占用2byte記憶體
ARGB_8888:每個像素占用4byte記憶體
RGB_565:每個像素占用2byte記憶體
Android預設的顔色模式為ARGB_8888,這個顔色模式色彩最細膩,顯示品質最高。但同樣的,占用的記憶體也最大。
舉例說明一個32位的PNG也就是ARGB_8888,像素是1204*1024,那麼占用空間是:
1024*1024*(32/8)
因為8bit = 1 byte, 32位就是4byte. 我們在解析圖檔的時候為了方式oom最好使用ARGB_4444模式. 節省一半的記憶體空間.
===================================================================
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. 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值,一個像素占用一個位元組 |
Bitmap.Config | ARGB_4444 | This field is deprecated. Because of the poor quality of this configuration, it is advised to use instead. 這種格式的圖檔,看起來品質太差,已經不推薦使用。 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個位元組 |
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 ,Green(G)值占6個bites ,Blue(B)值占5個bites,共16bites,即2個位元組.對于沒有透明和半透明顔色的圖檔來說,該格式的圖檔能夠達到比較的呈現效果,相對于ARGB_8888來說也能減少一半的記憶體開銷。是以它是一個不錯的選擇。另外我們通過android.content.res.Resources來取得一個張圖檔時,它也是以該格式來建構BitMap的. 從Android4.0開始,該選項無效。即使設定為該值,系統任然會采用 ARGB_8888來構造圖檔 |
注 :A RGB指的是一種色彩模式,裡面A代表Alpha, R 表示 red , G 表示 green , B 表示 blue ,其實所有的可見色都是紅綠藍組成的,是以紅綠藍又稱為三原色。 A R G B
透明度 紅色 綠色 藍色 簡單點說
圖檔格式(Bitmap.Config) | 占用記憶體的計算方向 | 一張100*100的圖檔占用記憶體的大小 |
ALPHA_8 | 圖檔長度*圖檔寬度 | 100*100=10000位元組 |
ARGB_4444 | 圖檔長度*圖檔寬度*2 | 100*100*2=20000位元組 |
ARGB_8888 | 圖檔長度*圖檔寬度*4 | 100*100*4=40000位元組 |
RGB_565 | 圖檔長度*圖檔寬度*2 | 100*100*2=20000位元組 |
另外,需要注意 這裡的圖檔占用記憶體是指在Navtive中占用的記憶體,當然BitMap使用的絕大多數記憶體就是該記憶體。 因為我們可以簡單的認為它就是BitMap所占用的記憶體。 Bitmap對象在不使用時,我們應該先調用 recycle(),然後才它設定為null. 雖然Bitmap在被回收時可以通過BitmapFinalizer來回收記憶體。但是調用recycle()是一個良好的習慣 在Android4.0之前,Bitmap的記憶體是配置設定在Native堆中,調用recycle()可以立即釋放Native記憶體。 從Android4.0開始,Bitmap的記憶體就是配置設定在dalvik堆中,即JAVA堆中的,調用recycle()并不能立即釋放Native記憶體。但是調用recycle()也是一個良好的習慣。 通過dumpsys meminfo指令可以檢視一個程序的記憶體使用情況, 當然也可以通過它來觀察我們建立或銷毀一張BitMap圖檔記憶體的變化,進而推斷出圖檔占用記憶體的大小。 示例: adb shell " dumpsys meminfo com.lenovo.robin " 運作結果。 Applications Memory Usage (kB): Uptime: 18696550 Realtime: 18696541 ** MEMINFO in pid 7985 [com.lenovo.robin] ** native dalvik other total size: 4828 5379 N/A 10207 allocated: 4073 2852 N/A 6925 free: 10 2527 N/A 2537 (Pss): 608 317 1603 2528 (shared dirty): 2240 1896 6056 10192 (priv dirty): 548 36 1276 1860
Objects Views: 0 ViewRoots: 0 AppContexts: 0 Activities: 0 Assets: 2 AssetManagers: 2 Local Binders: 5 Proxy Binders: 11 Death Recipients: 1 OpenSSL Sockets: 0
SQL
heap: 0 MEMORY_USED: 0 PAGECACHE_OVERFLOW: 0 MALLOC_SIZE: 0