Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 技術基礎

Paint API 之 ColorFilter(顏色過濾器):ColorMatrixColorFilter(1/3)

編輯:Android 技術基礎

本節引言:

上節我們學習了 MaskFilter(面具),用它的兩個子類 BlurMaskFilter 做出模糊效果、EmbossMaskFilter 做出浮雕效果。本節我們來學習另一個 API——ColorFilter(顏色過濾器)。和 MaskFilter 一樣,我們並不直接使用該類,而是使用它的三個子類:

顏色矩陣顏色過濾器:ColorMatrixColorFilter

光照色彩顏色過濾器:LightingColorFilter

混排顏色過濾器:PorterDuffColorFilter

本節我們先來學習第一個 ColorMatrixColorFilter 的用法。打開 ColorMatrixColorFilter 的文件:

大意是:透過一個 4×5 的顏色矩陣來變換顏色,可以修改像素的飽和度、把 YUV 轉換成 RGB 等。構造方法中的 ColorMatrix 就是顏色矩陣,也是我們學習的核心。下面聽我娓娓道來。

PS:ColorMatrix 的 API 文件


1.相關常識的普及:

RGBA 模型:

RGBA 不知道你聽過沒。紅綠藍(光的三基色)大家都知道,而 RGBA 則是在此基礎上多了一個透明度:R(Red 紅色)、G(Green 綠色)、B(Blue 藍色)、A(Alpha 透明度)。另外要和顏料的三原色區分開來,最明顯的區別就是顏料三原色中用黃色替換了光三基色中的綠色。知道就好,有興趣可自行搜尋。

一些名詞:

  • 色調/色相——物體傳遞的顏色
  • 飽和度——顏色的純度,從 0(灰)到 100%(飽和)來描述
  • 亮度/明度——顏色的相對明暗程度

2.ColorMatrix 的解讀

如題,顏色矩陣是 4×5 的,我們可以修改矩陣中的值,來實現黑白照、泛黃老照片、高對比度等效果。手撕顏色矩陣解釋圖如下:

不知道你看懂了沒?如果你學過高數,對此肯定很熟悉,無非是矩陣的叉乘而已;沒學過也沒關係,計算方法就是右下角那個——拿顏色矩陣的每一行去 × 顏色矩陣分量的每一列。很典型的一個例子,處理前後的結果比較,我們還可以讓某個顏色值 × 一個常數,比如讓第三行(藍)乘以 2,效果就變成泛藍色了。當然,我們一定要寫程式碼來驗證上面的結果。


3.寫程式碼來驗證 ColorMatrix 所起的作用

這裡來寫個爛大街的例子:一個 ImageView、4×5 個 EditText、一個重置按鈕和一個生成按鈕。先看效果圖:

依序是原圖、泛黃、泛綠、泛紅、高對比度、色相變換,以及黃色復古:

接下來我們寫程式碼完成上述效果。程式碼實現:

首先是布局檔 activity_main.xml:

 <?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" android:padding="5dp"> <ImageView android:id="@+id/img_show" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2" /> <GridLayout android:id="@+id/gp_matrix" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="3" android:columnCount="5" android:rowCount="4"></GridLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btn_reset" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="重置" /> <Button android:id="@+id/btn_Change" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="變換" /> </LinearLayout> </LinearLayout> 

接著是 MainActivity.java:

 public class MainActivity extends AppCompatActivity implements View.OnClickListener { private ImageView img_show; private GridLayout gp_matrix; private Button btn_reset; private Button btn_Change; private Bitmap mBitmap; private int mEtWidth, mEtHeight; private EditText[] mEts = new EditText[20]; private float[] mColorMatrix = new float[20]; private Context mContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = MainActivity.this; bindViews(); gp_matrix.post(new Runnable() { @Override public void run() { mEtWidth = gp_matrix.getWidth() / 5; mEtHeight = gp_matrix.getHeight() / 4; //添加5 * 4個EditText for (int i = 0; i < 20; i++) { EditText editText = new EditText(mContext); mEts[i] = editText; gp_matrix.addView(editText, mEtWidth, mEtHeight); } initMatrix(); } }); } private void bindViews() { img_show = (ImageView) findViewById(R.id.img_show); gp_matrix = (GridLayout) findViewById(R.id.gp_matrix); btn_reset = (Button) findViewById(R.id.btn_reset); btn_Change = (Button) findViewById(R.id.btn_Change); mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.img_meizi); img_show.setImageBitmap(mBitmap); btn_reset.setOnClickListener(this); btn_Change.setOnClickListener(this); } //定義一個初始化顏色矩陣的方法 private void initMatrix() { for (int i = 0; i < 20; i++) { if (i % 6 == 0) { mEts[i].setText(String.valueOf(1)); } else { mEts[i].setText(String.valueOf(0)); } } } //定義一個獲取矩陣值得方法 private void getMatrix() { for (int i = 0; i < 20; i++) { mColorMatrix[i] = Float.valueOf(mEts[i].getText().toString()); } } //根據顏色矩陣的值來處理圖片 private void setImageMatrix() { Bitmap bmp = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), Bitmap.Config.ARGB_8888); android.graphics.ColorMatrix colorMatrix = new android.graphics.ColorMatrix(); colorMatrix.set(mColorMatrix); Canvas canvas = new Canvas(bmp); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix)); canvas.drawBitmap(mBitmap, 0, 0, paint); img_show.setImageBitmap(bmp); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_Change: getMatrix(); setImageMatrix(); break; case R.id.btn_reset: initMatrix(); getMatrix(); setImageMatrix(); break; } } } 

程式碼非常簡單:載入布局,往 GridLayout 裡塞 5×4 個 EditText。這裡用 post() 方法,是為了保證 GridLayout 載入完畢後才去取得寬高,否則在取得 GridLayout 寬高時會拿不到值。接著定義了三個方法:初始化矩陣、取得矩陣值、以及根據矩陣值處理圖片。是不是很簡單呢?

不過到這裡你可能有一個疑問:

「難道處理圖像我們只能這樣修改顏色矩陣嗎?每次都這樣肯定很麻煩,誰會去記矩陣裡該填什麼值?有沒有更簡單的處理圖片的方法?」

答:當然有。我們可以看回文件,會發現幾個很常用的方法:

setRotate(int axis, float degrees):設定色調

setSaturation(float sat):設定飽和度

setScale(float rScale, float gScale, float bScale, float aScale):設定亮度

下面我們寫個例子來試試這三個方法。


4.使用 ColorMatrix 的三個方法處理圖像

運行效果圖:

程式碼實現:

首先編寫一個圖片處理工具類,傳入 Bitmap、色相、飽和度與亮度,處理後返回處理後的圖片:ImageHelper.java:

 /** * Created by Jay on 2015/10/28 0028. */ public class ImageHelper { /** * 該方法用來處理圖像,根據色調,飽和度,亮度來調節 * * @param bm:要處理的圖像 * @param hue:色調 * @param saturation:飽和度 * @param lum:亮度 * */ public static Bitmap handleImageEffect(Bitmap bm, float hue, float saturation, float lum) { Bitmap bmp = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bmp); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); ColorMatrix hueMatrix = new ColorMatrix(); hueMatrix.setRotate(0, hue); //0代表R,紅色 hueMatrix.setRotate(1, hue); //1代表G,綠色 hueMatrix.setRotate(2, hue); //2代表B,藍色 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)); canvas.drawBitmap(bm, 0, 0, paint); return bmp; } } 

接下來把布局也寫出來,activity_main.xml:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="5dp"> <ImageView android:id="@+id/img_meizi" android:layout_width="300dp" android:layout_height="300dp" android:layout_centerHorizontal="true" android:layout_marginBottom="24dp" android:layout_marginTop="24dp" /> <TextView android:id="@+id/txt_hue" android:layout_width="wrap_content" android:layout_height="32dp" android:layout_below="@id/img_meizi" android:gravity="center" android:text="色調 :" android:textSize="18sp" /> <SeekBar android:id="@+id/sb_hue" android:layout_width="match_parent" android:layout_height="32dp" android:layout_below="@id/img_meizi" android:layout_toRightOf="@id/txt_hue" /> <TextView android:id="@+id/txt_saturation" android:layout_width="wrap_content" android:layout_height="32dp" android:layout_below="@id/txt_hue" android:gravity="center" android:text="飽和度:" android:textSize="18sp" /> <SeekBar android:id="@+id/sb_saturation" android:layout_width="match_parent" android:layout_height="32dp" android:layout_below="@id/sb_hue" android:layout_toRightOf="@id/txt_saturation" /> <TextView android:id="@+id/txt_lun" android:layout_width="wrap_content" android:layout_height="32dp" android:layout_below="@id/txt_saturation" android:gravity="center" android:text="亮度 :" android:textSize="18sp" /> <SeekBar android:id="@+id/sb_lum" android:layout_width="match_parent" android:layout_height="32dp" android:layout_below="@id/sb_saturation" android:layout_toRightOf="@id/txt_lun" /> </RelativeLayout> 

最後是 MainActivity.java:

 public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener{ private ImageView img_meizi; private SeekBar sb_hue; private SeekBar sb_saturation; private SeekBar sb_lum; private final static int MAX_VALUE = 255; private final static int MID_VALUE = 127; private float mHue = 0.0f; private float mStauration = 1.0f; private float mLum = 1.0f; private Bitmap mBitmap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.img_meizi); bindViews(); } private void bindViews() { img_meizi = (ImageView) findViewById(R.id.img_meizi); sb_hue = (SeekBar) findViewById(R.id.sb_hue); sb_saturation = (SeekBar) findViewById(R.id.sb_saturation); sb_lum = (SeekBar) findViewById(R.id.sb_lum); img_meizi.setImageBitmap(mBitmap); sb_hue.setMax(MAX_VALUE); sb_hue.setProgress(MID_VALUE); sb_saturation.setMax(MAX_VALUE); sb_saturation.setProgress(MID_VALUE); sb_lum.setMax(MAX_VALUE); sb_lum.setProgress(MID_VALUE); sb_hue.setOnSeekBarChangeListener(this); sb_saturation.setOnSeekBarChangeListener(this); sb_lum.setOnSeekBarChangeListener(this); } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { switch (seekBar.getId()) { case R.id.sb_hue: mHue = (progress - MID_VALUE) * 1.0F / MID_VALUE * 180; break; case R.id.sb_saturation: mStauration = progress * 1.0F / MID_VALUE; break; case R.id.sb_lum: mLum = progress * 1.0F / MID_VALUE; break; } img_meizi.setImageBitmap(ImageHelper.handleImageEffect(mBitmap, mHue, mStauration, mLum)); } @Override public void onStartTrackingTouch(SeekBar seekBar) {} @Override public void onStopTrackingTouch(SeekBar seekBar) {} } 

程式碼同樣很簡單,就不多講解了。


5.本節程式碼示例下載:

ColorMatrixDemo.zip

ColorMatrixDemo2.zip


本節小結:

本節為大家介紹了 ColorFilter 中的第一個子類 ColorMatrixColorFilter(顏色矩陣過濾器),核心其實還是 ColorMatrix。我們透過該類處理圖片,可以自己設定 4×5 矩陣的值,又或者直接呼叫 ColorMatrix 提供的設定色調、飽和度、亮度的方法。圖像處理無非就這樣,還有一種是修改像素點形式的,後面也會講。本節內容參考自徐宜生老師的慕客網視頻「Android 圖像處理-打造美圖秀秀從它開始」,不想看文字的可以去看視頻,講得還是很讚的。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved