編輯:關於 Android 程式設計
上一篇談過用色彩矩陣改圖片顏色,這次換個思路——直接動每個像素的 RGBA 數值。
原理其實很樸素:一張圖片由無數緊密排列的像素點組成,每個像素點攜帶 R、G、B、A 四個通道的值。只要逐一改寫這些值,整體色調就跟著改變了。本文用這個思路實作三個效果:底片、懷舊、浮雕。

建立一個版面配置檔,用兩層 LinearLayout 均分四個 ImageView,分別顯示原圖與三種處理結果:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:id="@+id/image1"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
<ImageView
android:id="@+id/image2"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
<ImageView
android:id="@+id/image3"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
<ImageView
android:id="@+id/image4"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
四個格子依序用來放:原圖、底片、懷舊、浮雕。

底片最簡單,把每個通道的值用 255 減掉即可。
public Bitmap handleNegative(Bitmap bt) {
int width = bt.getWidth();
int height = bt.getHeight();
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
int[] oldPix = new int[width * height];
int[] newPix = new int[width * height];
// 把原圖像素全部取出來
bt.getPixels(oldPix, 0, width, 0, 0, width, height);
for (int i = 0; i < oldPix.length; i++) {
int color = oldPix[i];
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
int a = Color.alpha(color);
// 底片變換:255 減去各通道值
r = 255 - r;
g = 255 - g;
b = 255 - b;
newPix[i] = Color.argb(a, clamp(r), clamp(g), clamp(b));
}
bmp.setPixels(newPix, 0, width, 0, 0, width, height);
return bmp;
}
// 運算結果必須夾在 0~255 之間,否則會出現色塊異常
private int clamp(int v) {
return v < 0 ? 0 : (v > 255 ? 255 : v);
}
懷舊(老照片)效果靠一組加權係數把 RGB 往暖色調拉,畫面會呈現泛黃的褐色調。
public Bitmap handleOld(Bitmap bt) {
int width = bt.getWidth();
int height = bt.getHeight();
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
int[] oldPix = new int[width * height];
int[] newPix = new int[width * height];
bt.getPixels(oldPix, 0, width, 0, 0, width, height);
for (int i = 0; i < oldPix.length; i++) {
int color = oldPix[i];
int r0 = Color.red(color);
int g0 = Color.green(color);
int b0 = Color.blue(color);
int a = Color.alpha(color);
// 注意:右邊必須用原始的 r0/g0/b0,不可邊算邊覆寫
int r = (int) (0.393 * r0 + 0.769 * g0 + 0.189 * b0);
int g = (int) (0.349 * r0 + 0.686 * g0 + 0.168 * b0);
int b = (int) (0.272 * r0 + 0.534 * g0 + 0.131 * b0);
newPix[i] = Color.argb(a, clamp(r), clamp(g), clamp(b));
}
bmp.setPixels(newPix, 0, width, 0, 0, width, height);
return bmp;
}
浮雕的思路是讓每個像素與它前一個像素做差值,再平移到中間灰階(127)附近,於是邊緣被凸顯、平坦區域趨於單色,看起來就像壓印上去的浮雕。
public Bitmap handleRelief(Bitmap bt) {
int width = bt.getWidth();
int height = bt.getHeight();
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
int[] oldPix = new int[width * height];
int[] newPix = new int[width * height];
bt.getPixels(oldPix, 0, width, 0, 0, width, height);
for (int i = 0; i < oldPix.length; i++) {
int color = oldPix[i];
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
int a = Color.alpha(color);
// 取前一個像素;i 為 0 時避免陣列越界,直接取自己
int prev = oldPix[i == 0 ? 0 : i - 1];
int r1 = Color.red(prev);
int g1 = Color.green(prev);
int b1 = Color.blue(prev);
int nr = r1 - r + 127;
int ng = g1 - g + 127;
int nb = b1 - b + 127;
newPix[i] = Color.argb(a, clamp(nr), clamp(ng), clamp(nb));
}
bmp.setPixels(newPix, 0, width, 0, 0, width, height);
return bmp;
}
Color.red()、Color.green()、Color.blue() 各自對應一個通道,寫成連續三次 red() 是常見筆誤。把這三個函式串起來,一個陽春的「手機 PS」雛形就出來了。理解了像素這一層,後續要加亮度、對比度、伽瑪校正都只是換公式而已。
SweetAlertDialog 在 Android Studio 的導入與常見錯誤
SweetAlertDialog 是什麼?SweetAlertDialog 是一個常見的 Android 對話框庫,提供比系統 AlertDialog 更鮮明的視覺效果,
Android 事件分發機制實測:從 Logcat 逐行拆解 dispatchTouchEvent
事件分發機制是 Android 面試與實務中的必考題,但只看結論很容易記錯。最可靠的方式,是自己寫幾個自訂 ViewGroup,在每個方法裡打 Log,然後拿真實的輸出對
在 Android 上使用 iconfont 的便捷實踐方案
近來我在研究 AIOSO(Alibaba Internal Open Source Organization,即阿里巴巴內部開源組織)的一個子專案 MMCherryUI。
破解 Android 設定中的隱藏彩蛋:隱藏項實現原理解析
做程式的人偶爾也會惡搞一下。Android 的設定裡就藏了這樣一個趣味彩蛋:連續快速點擊「Android 版本」,會冒出一張圖片;而在 4.0 系統上按住 Android