編輯:關於 Android 程式設計
想讓 App 具備簡單的圖色替換能力,例如換掉 T-Shirt 或背景的白底,核心概念其實只有一個:把圖片攤平成一維像素陣列,逐點拆解 ARGB 通道、判斷並改寫,再重新組回 Bitmap。以下示範把白色換成紅色,並順手把常用的圖片工具封裝成一個 Image 類。
這個類負責載入圖片、縮放、讀取像素、由像素陣列重建 Bitmap,以及釋放記憶體。Android 的 Bitmap 佔用量高, recycle 的觀念一定要一起養成。
public class Image {
public Bitmap bitmap; // 圖片資源物件
// 由資源 id 載入
public Image(Context context, int id) {
bitmap = BitmapFactory.decodeResource(context.getResources(), id);
}
// 由 assets 檔名載入,並指定縮放比例
public Image(Context context, String name, float scaleW, float scaleH)
throws IOException {
InputStream is = context.getAssets().open(name);
bitmap = BitmapFactory.decodeStream(is);
is.close();
bitmap = setScaleSize(bitmap, scaleW, scaleH);
}
public int getWidth() { return bitmap.getWidth(); }
public int getHeight() { return bitmap.getHeight(); }
// 將整張圖的像素取出到陣列
public int[] getPixel(int[] array, int x, int y) {
bitmap.getPixels(array, 0, getWidth(), 0, 0, getWidth(), getHeight());
return array;
}
// 由像素陣列還原成 Bitmap(RGB_565 較省記憶體)
public Bitmap createImage(int[] array, int w, int h) {
return Bitmap.createBitmap(array, w, h, Bitmap.Config.RGB_565);
}
private Bitmap setScaleSize(Bitmap bitmap, float arg0, float arg1) {
Matrix matrix = new Matrix();
matrix.postScale(arg0, arg1);
return Bitmap.createBitmap(bitmap, 0, 0, getWidth(), getHeight(),
matrix, true);
}
// 回收圖片佔用的記憶體
public static void free(Image image) {
try {
if (image.bitmap != null && !image.bitmap.isRecycled()) {
image.bitmap.recycle();
}
image.bitmap = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
流程是:載圖 → 取像素陣列 → 用位元遮罩取出 R、G、B → 命中目標色就改寫 → 寫回新陣列 → 重建 Bitmap 顯示。白色是 R、G、B 皆為 255,所以判斷條件相當單純。
image = new Image(this, "Alfa_Romeo_8C_Competizione_Detail.png", 1, 1);
len = image.getWidth() * image.getHeight();
int[] imageARGB = new int[len];
int[] newimage = new int[len];
image.getPixel(imageARGB, 0, 0);
for (int i = 0; i < image.getHeight(); i++) {
for (int j = 0; j < image.getWidth(); j++) {
int index = i * image.getWidth() + j;
if (imageARGB[index] != 0) {
int red = (imageARGB[index] & 0x00ff0000) >> 16;
int green = (imageARGB[index] & 0x0000ff00) >> 8;
int blue = (imageARGB[index] & 0x000000ff);
// 白色 → 紅色
if (red == 255 && green == 255 && blue == 255) {
red = 255; green = 0; blue = 0;
}
newimage[index] = (red << 16) | (green << 8) | blue;
} else {
newimage[index] = imageARGB[index]; // 全透明點直接保留
}
}
}
Bitmap bitmap = image.createImage(newimage,
image.getWidth(), image.getHeight());
((ImageView) findViewById(R.id.imageView1)).setImageBitmap(bitmap);
兩個類搭起來,白底就變紅底了。若要推廣成通用功能,只要把 red == 255 && green == 255 && blue == 255 換成可傳入的目標色與替換色即可;但要注意大圖逐點運算相當吃 CPU,實務上建議先縮圖、或改用 ColorMatrix 交給硬體加速處理。
Android Service 基本用法詳解(含生命週期)
Service 是 Android 四大元件之一,也是最容易被誤解為「執行緒」的元件。本文從分類、生命週期到三種啟動方式的實作,全面梳理 Service 的核心知識。 一
Android 四大組件之 BroadcastReceiver 廣播機制
BroadcastReceiver 是 Android 中負責接收廣播的組件。它可用於監聽系統事件、應用程式內通知或自定義訊息,是組件間低耦合溝通的重要方式。 標準廣
Android 自定義 View 入門:從 onMeasure 到 onDraw 的完整實踐
對不少剛入門的 Android 開發者來說,「自定義 View」幾乎是一道心理門檻;但如果你想在職業發展上再往上走一層,這又是繞不開的必修課。本文先把整體思路梳理清楚,再
Android 表格版面 TableLayout 範例詳解
一、表格版面 TableLayout TableLayout 以列與行的形式管理子元素,每一行通常是一個 TableRow 物件,也可以是普通的 View;TableRo