編輯:關於 Android 程式設計
先複習一下圖片的 RGBA 模型:R 是紅(Red)、G 是綠(Green)、B 是藍(Blue)、A 是透明度(Alpha)。這四個通道的各種組合構成了我們看到的所有顏色。而色調、飽和度、亮度這三個概念,本質上都是對這組數值所做的線性變換。
Android 提供了 ColorMatrix 這個工具,正好可以對應這三種調整。
public class ImageTools {
/**
* 對圖片進行色調、飽和度與亮度處理
*
* @param bitmap 原圖
* @param sx 色調
* @param bhd 飽和度
* @param ld 亮度
*/
public static Bitmap getColorImage(Bitmap bitmap, float sx, float bhd, float ld) {
Bitmap bmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// 色調:分別對 R、G、B 三軸做旋轉
ColorMatrix sxMatrix = new ColorMatrix();
sxMatrix.setRotate(0, sx);
sxMatrix.setRotate(1, sx);
sxMatrix.setRotate(2, sx);
// 飽和度
ColorMatrix bhdMatrix = new ColorMatrix();
bhdMatrix.setSaturation(bhd);
// 亮度
ColorMatrix ldMatrix = new ColorMatrix();
ldMatrix.setScale(ld, ld, ld, 1);
// 依序串接三種效果
ColorMatrix mixMatrix = new ColorMatrix();
mixMatrix.postConcat(sxMatrix);
mixMatrix.postConcat(bhdMatrix);
mixMatrix.postConcat(ldMatrix);
paint.setColorFilter(new ColorMatrixColorFilter(mixMatrix));
canvas.drawBitmap(bmp, 0, 0, paint);
return bmp;
}
}
這裡有個細節值得說明:範例中把 bmp 同時作為畫布來源與輸出目標。實測可行,但嚴格來說這麼做會有自我覆蓋的疑慮,較穩妥的寫法是先建立一個空白的目標 Bitmap,再把原始 bitmap 畫上去:
Bitmap bmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
canvas.drawBitmap(bitmap, 0, 0, paint); // 畫的是原始 bitmap
public class ColorMainActivity extends Activity
implements SeekBar.OnSeekBarChangeListener {
private ImageView colorIv;
private SeekBar sxBar, bhdBar, ldBar;
private static final int MIN_COLOR = 100;
private static final int MAX_COLOR = 255;
private float sx, bhd, ld;
private Bitmap bmp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.color_main);
initViews();
}
private void initViews() {
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
colorIv = (ImageView) findViewById(R.id.color_iv);
colorIv.setImageBitmap(bmp);
sxBar = (SeekBar) findViewById(R.id.sx_seekbar);
bhdBar = (SeekBar) findViewById(R.id.bhd_seekbar);
ldBar = (SeekBar) findViewById(R.id.ld_seekbar);
sxBar.setOnSeekBarChangeListener(this);
sxBar.setMax(MAX_COLOR);
sxBar.setProgress(MIN_COLOR);
bhdBar.setOnSeekBarChangeListener(this);
bhdBar.setMax(MAX_COLOR);
bhdBar.setProgress(MIN_COLOR);
ldBar.setOnSeekBarChangeListener(this);
ldBar.setMax(MAX_COLOR);
ldBar.setProgress(MIN_COLOR);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
switch (seekBar.getId()) {
case R.id.sx_seekbar:
sx = (progress - MIN_COLOR) * 1.0f / MIN_COLOR * 180;
break;
case R.id.bhd_seekbar:
bhd = progress * 1.0f / MIN_COLOR;
break;
case R.id.ld_seekbar:
ld = progress * 1.0f / MIN_COLOR;
break;
}
colorIv.setImageBitmap(ImageTools.getColorImage(bmp, sx, bhd, ld));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
}
這裡的換算值得留意:飽和度與亮度的中性值是 1.0f,所以把 SeekBar 的 100 當基準點,滑桿值除以 100 剛好讓中間位置對應 1.0;色調則是以角度表示,因此用 (progress - 100) / 100 * 180 映射到 -180°~180° 的範圍。
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/color_iv"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp" />
<SeekBar
android:id="@+id/sx_seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/color_iv" />
<SeekBar
android:id="@+id/bhd_seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/sx_seekbar"
android:layout_marginTop="10dp" />
<SeekBar
android:id="@+id/ld_seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/bhd_seekbar"
android:layout_marginTop="10dp" />
</RelativeLayout>
onProgressChanged 在拖動 SeekBar 時會以極高頻率觸發,每次都重新配置一張 Bitmap,很快就會造成記憶體壓力與卡頓。若要用在正式產品,建議加上節流(例如用 postDelayed 合併同一幀內的多次變更),並在更換圖片時回收舊的 Bitmap。另外若只是要調整亮度,用 PorterDuff 或 ColorMatrixColorFilter 直接套在 ImageView 的 ColorFilter 上會更省資源,不必重新產生一張 Bitmap。
GreenDAO 3.X 搭配 Volley 與 Gson 的資料儲存實作
GreenDAO 是 Android 上常見的 ORM 方案。相較原生 SQLite,它在使用便利性、效能與程式碼生成方面有明顯優勢。GreenDAO 3.X 之後,主要
Android 事件流解析(一):用戶事件的分發與回調機制
任何程序本質上都是靜態代碼。我們把代碼打包放進運行環境,再依靠事件流的驅動,它才會真正跑起來——Android 也不例外。 驅動 Android 程序的事件大致分四類:
Android 系統應用開發(五):Android 輸入法類別與元件說明
前言Android 原始碼中的 packages/inputmethods 收錄了多套輸入法實作,包含 OpenWNN、LatinIME 與 Google PinyinI
用 Android Studio 模板快速生成通用 Adapter
每次寫列表頁,最耗時間的往往不是邏輯,而是那幾乎一模一樣的 Adapter 樣板:繼承 BaseAdapter 或 RecyclerView.Adapter、實作 get