編輯:關於 Android 程式設計
最近在做圖片相關的應用,累積了一些常用做法。圖片色彩變換大致有三條路:一是色度變換,二是使用 ColorMatrix 顏色矩陣,三是直接操作像素點。今天複習第一種——它比較單一,能得到的效果類型也有限,但正好對應「換色度、飽和度、亮度」這種最常見的風格調整需求。
• 抽出一個圖片操作工具類
• 建立一個用於運算的 Bitmap 物件
• 使用 Canvas 與 Paint
• 以矩陣做調色處理,參數由外部控制
• 畫出結果 Bitmap 並回傳
• 供各個方法呼叫取得效果
三支 SeekBar 分別控制色度、飽和度與亮度,即時反映到上方的 ImageView。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageview"
android:layout_width="match_parent"
android:layout_height="320dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:text="色 度"
android:textSize="18dp"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<SeekBar
android:id="@+id/hueBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:text="飽和度"
android:textSize="18dp"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<SeekBar
android:id="@+id/saturationBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:text="亮 度"
android:textSize="18dp"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<SeekBar
android:id="@+id/lumBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5" />
</LinearLayout>
</LinearLayout>
三個 ColorMatrix 各自負責色度旋轉、飽和度與亮度縮放,再用 postConcat 串接成最終矩陣,套進 ColorMatrixColorFilter 後一次畫出結果。
public static Bitmap handleImageLikePS(Bitmap bp, float hue, float saturation, float lum) {
Bitmap bitmap = Bitmap.createBitmap(bp.getWidth(), bp.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
ColorMatrix hueMatrix = new ColorMatrix();
hueMatrix.setRotate(0, hue);
hueMatrix.setRotate(1, hue);
hueMatrix.setRotate(2, hue);
ColorMatrix saturationMatrix = new ColorMatrix();
saturationMatrix.setSaturation(saturation);
ColorMatrix lumMatrix = new ColorMatrix();
lumMatrix.setScale(lum, lum, lum, 1);
ColorMatrix imageMatrix = new ColorMatrix();
imageMatrix.postConcat(hueMatrix);
imageMatrix.postConcat(saturationMatrix);
imageMatrix.postConcat(lumMatrix);
paint.setColorFilter(new ColorMatrixColorFilter(imageMatrix));
// 此處若換成 bitmap 會只套用一次,圖像將無法再被反覆編輯
canvas.drawBitmap(bp, 0, 0, paint);
return bitmap;
}
package com.example.colormatrixdemo;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SeekBar;
public class MainActivity extends Activity implements SeekBar.OnSeekBarChangeListener {
private Bitmap bitmap;
private ImageView imageview;
private SeekBar hueBar, saturationBar, lumBar;
private float mHue, mSaturation, mLum;
private static int MAXVALUE = 255, MIDVALUE = 127;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.masuo);
imageview = (ImageView) findViewById(R.id.imageview);
hueBar = (SeekBar) findViewById(R.id.hueBar);
saturationBar = (SeekBar) findViewById(R.id.saturationBar);
lumBar = (SeekBar) findViewById(R.id.lumBar);
hueBar.setOnSeekBarChangeListener(this);
saturationBar.setOnSeekBarChangeListener(this);
lumBar.setOnSeekBarChangeListener(this);
hueBar.setMax(MAXVALUE);
hueBar.setProgress(MIDVALUE);
saturationBar.setMax(MAXVALUE);
saturationBar.setProgress(MIDVALUE);
lumBar.setMax(MAXVALUE);
lumBar.setProgress(MIDVALUE);
imageview.setImageBitmap(bitmap);
}
@Override
public void onProgressChanged(SeekBar seekbar, int progress, boolean arg2) {
switch (seekbar.getId()) {
case R.id.hueBar:
mHue = (progress - MIDVALUE) * 1.0F / MIDVALUE * 180;
break;
case R.id.saturationBar:
mSaturation = progress * 1.0F / MIDVALUE;
break;
case R.id.lumBar:
mLum = progress * 1.0F / MIDVALUE;
break;
}
imageview.setImageBitmap(ImageTools.handleImageLikePS(bitmap, mHue, mSaturation, mLum));
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
}
@Override
public void onStopTrackingTouch(SeekBar arg0) {
}
}
工具類最後一定要拿「傳進來的參數 bp」去畫,而不是自己宣告的那個 bitmap,否則永遠拿不到即時變化的結果。我們宣告的 bitmap 只是一塊用來承接輸出的畫布,真正要處理的對象是參數。
總結
圖片處理的方法很多,其中直接操作像素點能做出最多花樣,老照片、浮雕、底片之類的效果都靠它;而顏色矩陣則是另一條相當經典又輕量的路線。這些技巧都值得花時間研究,能讓你的應用呈現更豐富的視覺表現。
從微信逆向案例看行動隱私保護:加密資料庫與授權分析的重要性
從一個逆向案例看敏感資料保護 行動裝置上的聊天記錄與通訊錄,往往包含大量私人關係、工作資訊與生活細節。正因為如此,應用通常會將這類資料儲存在本地資料庫,並透過加密、權限控
用 ViewFlipper 實現 Android 螢幕動畫切換
ViewFlipper 是一個專門用於「多子視圖輪番展示」的容器。它同一時刻只顯示一個子 View,切換時可以套用進出動畫,配上滑動手勢判斷,就能做出類似幻燈片的翻頁
Android 響應式編程(一)RxJava 前篇:入門基礎全解
1. RxJava 概述ReactiveX 與 RxJava談到 RxJava,首先要弄清什麼是 ReactiveX,因為 RxJava 正是 ReactiveX 的一種
Android 广播发送流程源码剖析:从 AMS 到 Receiver 的完整链路
前面聊过广播的注册过程,这篇来看发送。相比之下,发送流程要复杂得多。 整体分为四步 发送者把一个特定类型的广播交给 ActivityManagerService(AMS