編輯:Android 程式設計入門
在 Android 開發中,我們經常與 Bitmap 打交道,而不當的操作往往導致 OOM(Out of Memory)。本文將系統介紹如何在保證顯示品質的前提下,盡可能節省內存地使用 Bitmap。
Bitmap 是對位圖的抽象,可從文件系統、資源文件夾、網絡等來源獲取。位圖本質上是像素點的集合,以二進位位描述一張圖片;不同色彩格式用不同數量的位來描述一個像素,因此圖片質量與大小各異。
Bitmap 占用的內存不僅與像素點數、色彩格式有關,還與設備的屏幕密度、所在的 drawable 文件夾有關。這裡涉及兩個概念:
以屏幕密度240dpi、圖片(670×376)放在 drawable-xhdpi 文件夾為例,可透過 getByteCount 取得內存大小:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.size); int size = bitmap.getByteCount();
getByteCount 的計算方式是:像素寬 × 像素高 × 色彩深度。預設色彩格式 ARGB_8888,即一個像素佔32位(4字節)。若不考慮密度,670×376×4=1007680,但實際值卻不同,這是因為還要考慮 inDensity 與 inTargetDensity。縮放系數即 inTargetDensity 除以 inDensity,因此目標設備上的像素數為 (pixWidth / inDensity × inTargetDensity) × (pixHeight / inDensity × inTargetDensity),再乘以4即為內存大小。
BitmapFactory 提供多個靜態方法生產 Bitmap:decodeByteArray、decodeFile、decodeFileDescriptor、decodeResource、decodeStream。以下主要圍繞 decodeResource 說明,透過合理配置它的 options 即可把 Bitmap 調整到理想大小。
Options 是 BitmapFactory 的靜態內部類,關鍵參數包括:inBitmap(用於內存復用)、inSampleSize(采樣率)、inDensity、inTargetDensity、inScaled(是否縮放)、outWidth、outHeight(原始寬高)等。
1 public static class Options { 2 public Options() { 3 inDither = false; 4 inScaled = true; 5 inPremultiplied = true; 6 } 7 ... 8 public Bitmap inBitmap; public int inSampleSize; public boolean inPremultiplied; public boolean inDither; public int inDensity; public int inTargetDensity; public boolean inScaled; public int outWidth; public int outHeight; ... }
Options 中的 inScaled 預設為 true,也就是預設支援縮放。縮放依據縮放系數進行,即 inDensity 除以 inTargetDensity。即使不傳入 Options,decodeResource 也會依縮放系數自動縮放。我們也能手動設置:
BitmapFactory.Options options = new BitmapFactory.Options(); options.inDensity = 160; options.inTargetDensity = 320; Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.size, options); int size = bitmap.getByteCount();
當 inSampleSize 為1時,采樣後大小不變;為2時,寬高各為原來的1/2,大小變為1/4。也就是采樣後大小等於原始大小除以采樣率的平方。官方建議 inSampleSize 取2的非負整數次冪(1、2、4……)。設置時應注意:采樣後的圖片不能小於目標 ImageView,否則拉伸會導致失真。計算步驟通常如下:
第一步,將 inJustDecodeBounds 設為 true,只讀取尺寸而不真正加載:
BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(getResources(), resId, options); //原始寬高已存在 options.outWidth 和 options.outHeight 中
第二步,根據原始寬高計算 inSampleSize:
public static int calSampleSize(BitmapFactory.Options options, int dstWidth, int dstHeight) { int rawWidth = options.outWidth; int rawHeight = options.outHeight; int inSampleSize = 1; if (rawWidth > dstWidth || rawHeight > dstHeight) { float ratioHeight = (float) rawHeight / dstHeight; float ratioWidth = (float) rawWidth / dstHeight; inSampleSize = (int) Math.min(ratioWidth, ratioHeight); } return inSampleSize; }
這個參數用來實現 Bitmap 內存的復用。復用存在限制:Android 4.4 之前只能重用相同大小的 Bitmap 內存,4.4 及以後版本則只要後來的 Bitmap 比之前小即可。使用後,多個 Bitmap 可復用同一塊內存,从而提升效能。示例代碼如下:
private static void addInBitmapOptions(BitmapFactory.Options options, ImageCache cache) { options.inMutable = true; if (cache != null) { Bitmap inBitmap = cache.getBitmapFromReusableSet(options); if (inBitmap != null) { options.inBitmap = inBitmap; } } } static boolean canUseForInBitmap( Bitmap candidate, BitmapFactory.Options targetOptions) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { int width = targetOptions.outWidth / targetOptions.inSampleSize; int height = targetOptions.outHeight / targetOptions.inSampleSize; int byteCount = width * height * getBytesPerPixel(candidate.getConfig()); return byteCount <= candidate.getAllocationByteCount(); } return candidate.getWidth() == targetOptions.outWidth && candidate.getHeight() == targetOptions.outHeight && targetOptions.inSampleSize == 1; }
Android Developer 上的 Displaying Bitmap Efficiently 系列對如何高效使用 Bitmap 做出了權威描述,值得深入研讀。
1. Displaying Bitmap Efficiently
2. Android 開發繞不過的坑:你的 Bitmap 究竟占多大內存?
3. 《Android開發藝術探索》
4. Android Bitmap 面面觀
Android Studio 單元測試設定與執行指南
撰寫測試類別建立測試資料夾與測試套件。新增 ExampleTest,並繼承 InstrumentationTestCase。建立以 test 開頭的方法,例如 testJ
Android Studio 基本使用全面指南
本文總結了一段時間以來對 Android Studio 的使用心得,也希望能給剛接觸的朋友一些參考。此前一直使用 Eclipse,開發體驗也不錯,但看到越來越多人轉向 A
安卓學習之路:用JAVA多線程實現斷點續傳下載
本文示範如何用 JAVA 多線程實現檔案下載(代碼尚未優化,先實現功能)。核心思路是:先取得檔案總長度並建立同大小的佔位檔案,再將檔案切分成若干區塊,每個線程透過 HTT
Android 主題 Theme 的正確玩法
Android 主題通常在 AndroidManifest.xml 的 application 標籤中指定,例如 AppTheme。實際套用哪一份樣式,會受到 value