編輯:關於 Android 程式設計
在 Android 開發裡,圖片往往是最佔用記憶體的資源。無論是相機拍攝還是從網路下載,原始圖片尺寸龐大,直接載入容易造成 OOM。實務上常見的處理方式有兩大類:一類是「質量壓縮」,在不改變像素尺寸的情況下降低 JPEG 編碼品質;另一類是「按比例縮放」,透過 inSampleSize 直接縮小解碼後的位圖尺寸。下面分別說明。
核心思路是迴圈呼叫 compress(),每次降低 quality 參數,直到檔案大小符合目標(例如 100KB)為止。
private Bitmap compressImage(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 100 表示不壓縮
int options = 100;
while (baos.toByteArray().length / 1024 > 100) { // 大於 100kb 就繼續壓
baos.reset(); // 清空緩衝區
image.compress(Bitmap.CompressFormat.JPEG, options, baos);
options -= 10; // 每次遞減 10
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
return bitmap;
}
關鍵在於先用 inJustDecodeBounds = true 只讀取圖片的寬高資訊而不真正分配像素記憶體,計算出縮放比例後再正式解碼。
private Bitmap getimage(String srcPath) {
BitmapFactory.Options newOpts = new BitmapFactory.Options();
newOpts.inJustDecodeBounds = true; // 只讀邊界,不載入像素
Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);// 此時 bitmap 為空
newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
float hh = 800f; // 目標高度
float ww = 480f; // 目標寬度
int be = 1; // be=1 表示不縮放
if (w > h && w > ww) {
be = (int) (newOpts.outWidth / ww);
} else if (w < h && h > hh) {
be = (int) (newOpts.outHeight / hh);
}
if (be <= 0) be = 1;
newOpts.inSampleSize = be; // 設定縮放比例
bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
return compressImage(bitmap); // 縮放後再做一次質量壓縮
}
這個版本先判斷位圖是否超過 1MB,若是則先降質避免後續 decodeStream 時記憶體溢出,接著同樣透過邊界讀取計算比例並縮放。
private Bitmap comp(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
if (baos.toByteArray().length / 1024 > 1024) { // 大於 1M 先降質防爆
baos.reset();
image.compress(Bitmap.CompressFormat.JPEG, 50, baos);
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
BitmapFactory.Options newOpts = new BitmapFactory.Options();
newOpts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
float hh = 800f;
float ww = 480f;
int be = 1;
if (w > h && w > ww) {
be = (int) (newOpts.outWidth / ww);
} else if (w < h && h > hh) {
be = (int) (newOpts.outHeight / hh);
}
if (be <= 0) be = 1;
newOpts.inSampleSize = be;
isBm = new ByteArrayInputStream(baos.toByteArray());
bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
return compressImage(bitmap);
}
小提醒:質量壓縮只對 JPEG 有效(PNG 無損),而真正能大幅降低記憶體佔用的是縮放解碼尺寸。兩者搭配使用,才能在畫質與效能之間取得平衡。
Android 自定義字體樣式實現
在 Android 中設置自定義字體,通常通過 Typeface 來完成。先看看官方提供的方法: 顧名思義,我們可以通過項目 assets 目錄下的字體文件,或者 A
Android AlertDialog 對話框詳解與多種實例
Android AlertDialog 全面解析 Android 主要提供四種對話框: 1. AlertDialog:功能最豐富、應用最廣泛的對話框; 2. Prog
Android 自定義開關按鈕:從思路到完整實現
自定義控制項是 Android 開發中很能訓練思維的主題。以下以開關按鈕為例,說明如何從效果分析、屬性定義、測量、繪製到事件處理,一步步完成一個可重複使用的自定義 Vie
Android ListView 分頁加載與下拉刷新的實作方案
點擊按鈕刷新 1、效果如下: 實例如下:上圖的「添加數據」按鈕,在正式場景中可以換成一個進度條——因為從伺服器拉取數據通常需要一些時間,用進度條能給使用者提示。由於沒有