編輯:關於 Android 程式設計
本文分享 Android 中對圖片做顏色處理的方法,供大家參考。
想做出像美圖秀秀那樣能美化照片的效果嗎?其實你自己也能動手做一個。廢話不多說,直接看效果、上程式碼。
效果圖如下:
處理之前:

處理之後:

MainActivity.java 的程式碼如下:
package net.loonggg.test; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.ColorMatrix; import android.graphics.ColorMatrixColorFilter; import android.graphics.Matrix; import android.graphics.Paint; import android.os.Bundle; import android.widget.ImageView; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; public class MainActivity extends Activity { private SeekBar sb1, sb2, sb3, sb4, sb5; private ImageView iv; private Bitmap bitmap, updateBitmap; private Canvas canvas; private Paint paint; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.iv); sb1 = (SeekBar) findViewById(R.id.sb1); sb2 = (SeekBar) findViewById(R.id.sb2); sb3 = (SeekBar) findViewById(R.id.sb3); sb4 = (SeekBar) findViewById(R.id.sb4); sb5 = (SeekBar) findViewById(R.id.sb5); bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.b); updateBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig()); canvas = new Canvas(updateBitmap); paint = new Paint(); final ColorMatrix cm = new ColorMatrix(); paint.setColorFilter(new ColorMatrixColorFilter(cm)); paint.setColor(Color.BLACK); paint.setAntiAlias(true); final Matrix matrix = new Matrix(); canvas.drawBitmap(bitmap, matrix, paint); iv.setImageBitmap(updateBitmap); /** * RGB三原色 紅色值的設置 */ sb1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { int progress = seekBar.getProgress(); cm.set(new float[] { progress / 128f, 0, 0, 0, 0,// 紅色值 0, 1, 0, 0, 0,// 綠色值 0, 0, 1, 0, 0,// 藍色值 0, 0, 0, 1, 0 // 透明度 }); paint.setColorFilter(new ColorMatrixColorFilter(cm)); canvas.drawBitmap(bitmap, matrix, paint); iv.setImageBitmap(updateBitmap); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { } }); /** * RGB三原色 綠色值的設置 */ sb2.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { int progress = seekBar.getProgress(); cm.set(new float[] { 1, 0, 0, 0, 0,// 紅色值 0, progress / 128f, 0, 0, 0,// 綠色值 0, 0, 1, 0, 0,// 藍色值 0, 0, 0, 1, 0 // 透明度 }); paint.setColorFilter(new ColorMatrixColorFilter(cm)); canvas.drawBitmap(bitmap, matrix, paint); iv.setImageBitmap(updateBitmap); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { } }); /** * RGB三原色 藍色值的設置 */ sb3.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { int progress = seekBar.getProgress(); cm.set(new float[] { 1, 0, 0, 0, 0,// 紅色值 0, 1, 0, 0, 0,// 綠色值 0, 0, progress / 128f, 0, 0,// 藍色值 0, 0, 0, 1, 0 // 透明度 }); paint.setColorFilter(new ColorMatrixColorFilter(cm)); canvas.drawBitmap(bitmap, matrix, paint); iv.setImageBitmap(updateBitmap); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { } }); /** * RGB三原色 三個值都改變為設置飽和度(亮度) */ sb4.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { int progress = seekBar.getProgress(); cm.set(new float[] { progress / 128f, 0, 0, 0, 0,// 紅色值 0, progress / 128f, 0, 0, 0,// 綠色值 0, 0, progress / 128f, 0, 0,// 藍色值 0, 0, 0, 1, 0 // 透明度 }); paint.setColorFilter(new ColorMatrixColorFilter(cm)); canvas.drawBitmap(bitmap, matrix, paint); iv.setImageBitmap(updateBitmap); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { } }); /** * RGB三原色 設置透明度 */ sb5.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { int progress = seekBar.getProgress(); cm.set(new float[] { 1, 0, 0, 0, 0,// 紅色值 0, 1, 0, 0, 0,// 綠色值 0, 0, 1, 0, 0,// 藍色值 0, 0, 0, progress / 128f, 0 // 透明度 }); paint.setColorFilter(new ColorMatrixColorFilter(cm)); canvas.drawBitmap(bitmap, matrix, paint); iv.setImageBitmap(updateBitmap); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { } }); } } 佈局檔程式碼如下:
<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:background="#CDCDCD" android:orientation="vertical" tools:context=".MainActivity" > <ImageView android:id="@+id/iv" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal" android:padding="10dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="紅色:" android:textColor="#FF0000" android:textSize="24sp" /> <SeekBar android:id="@+id/sb1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:max="256" android:progress="128" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal" android:padding="10dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="綠色:" android:textColor="#00FF00" android:textSize="24sp" /> <SeekBar android:id="@+id/sb2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:max="256" android:progress="128" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal" android:padding="10dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="藍色:" android:textColor="#0000FF" android:textSize="24sp" /> <SeekBar android:id="@+id/sb3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:max="256" android:progress="128" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal" android:padding="10dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="飽和度:" android:textColor="#000000" android:textSize="16.5sp" /> <SeekBar android:id="@+id/sb4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:max="256" android:progress="128" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal" android:padding="10dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="透明度:" android:textColor="#000000" android:textSize="16.5sp" /> <SeekBar android:id="@+id/sb5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:max="256" android:progress="128" /> </LinearLayout> </LinearLayout>
核心原理是透過 ColorMatrix 的 4×5 色彩矩陣來調整每個像素的紅、綠、藍與透明度:矩陣前三列分別控制 R、G、B 的縮放係數,最後一列控制 Alpha。搭配五個 SeekBar 即時改變矩陣數值,再用 ColorMatrixColorFilter 套到畫筆上重繪,就能像美圖軟體一樣即時預覽色彩調整的效果。
希望本文所述能對大家的 Android 開發有所幫助。
React Native 混合開發入門:TextInput 文本輸入組件詳解
TextInput TextInput 是允許用戶輸入文本的基礎組件,它提供了一系列屬性來幫助我們處理業務邏輯,例如 onChangeText、onSubmitEdit
Android 自定義 ViewGroup 實作 FlowLayout 流式版面配置(三)
本篇延續自定義 ViewGroup 的系列主題,動手做一個實用的範例:FlowLayout(流式版面配置)。 所謂 FlowLayout,指的是子控制項會依序往右排入容器
ContentProvider 資源共享實例:用 SQLite 打造對外的資料服務
ContentProvider 為資料的存取提供了統一介面。用了它,應用程式之間就能共享資料;Android 內建的許多內容(影片、音訊、圖片、通訊錄)本身就是以 Con
Android 組合式自訂控件:以標題欄為例說明
一般來說,Android 實作自訂控制項無非三種途徑:Ⅰ、繼承現有控制項,擴充它的功能。Ⅱ、把現有控制項組合起來,做出功能更強的複合控制項。Ⅲ、直接重寫 View,打造一