之前整了兩章關于Android中的Matrix操作,本章來談一談Android中的另一個Matrix-ColorMatrix。
本文講述的具體內容如圖:
**如果不喜歡看描述的話,項目的demo地址如下:
https://github.com/MartinBZDQSM/Matrix
**
ColorMatrix
談到ColorMatrix,那么先聊聊Android中顏色.
我們在經常調整布局顏色的時候使用的是為八位的十六進制表示法的顏色值,例如:#0xd5d5d5;這種表示法也正對應著Android中使用的ARGB模式,也就是說顏色和透明度 (alpha) 的值都用以十六進制表示法表示。其首兩位代表著透明度剩下的分別對應著紅綠藍三種顏色,其中任何一種顏色的值范圍都是 0到 255(00到 ff)。
而Android支持的顏色模式都有:
顏色模式 | 說明 |
---|---|
ARGB8888 | 四通道高精度(32位) |
ARGB4444 | 四通道低精度(24位) |
RGB565 | 三通道(16位) |
Alpha8 | 透明通道(8位) |
所以看到這里,我們其實就能猜出來,為什么一張好好的透明PNG圖片在某些加載框架中會變黑?正式因為其框架使用的默認通道是RGB565 缺少了一個透明通道,所以當我們修改了圖片的顏色模式為ARGB8888或者ARGB4444后會變正常。
現在我們來看看ColorMatrix 的官方描述:
/**
* 4x5 matrix for transforming the color and alpha components of a Bitmap.
* The matrix can be passed as single array, and is treated as follows:
* 下面這個4x5的矩陣可以表達,為一張圖的顏色以及透明圖信息。
* [ a, b, c, d, e,
* f, g, h, i, j,
* k, l, m, n, o,
* p, q, r, s, t ]
*
* When applied to a color 顏色分量矩陣
* [R, G, B, A]
* the resulting color is computed as:計算結果如下公式:
*
* R’ = a*R + b*G + c*B + d*A + e;
* G’ = f*R + g*G + h*B + i*A + j;
* B’ = k*R + l*G + m*B + n*A + o;
* A’ = p*R + q*G + r*B + s*A + t;
*
* That resulting color
* [R’, G’, B’, A’]
* then has each channel clamped to the 0 to 255 range.計算出來的結果,每個通道都在0-255之間
*
* The sample ColorMatrix below inverts incoming colors by scaling each
* channel by -1, and then shifting the result up by
* 255 to remain in the standard color space.
* 好像用來舉例說明的,前四列代表RGBA的偏向成分,第五列代表著顏色偏移量
*
* [ -1, 0, 0, 0, 255,
* 0, -1, 0, 0, 255,
* 0, 0, -1, 0, 255,
* 0, 0, 0, 1, 0 ]
*/
總得來說就是,一張圖可以通過設置 顏色矩陣 來改變它的顏色效果。
1.顏色偏向
為了測試ColorMatrix能夠影響圖片的顏色效果,我設置了4個SeekBar,來分別代表著R,G,B,A四個通道,其范圍都是0-255.
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//設置偏移量
colorMatrix.setScale(caculate(seekBarR.getProgress()), caculate(seekBarG.getProgress()),
caculate(seekBarB.getProgress()), caculate(seekBarA.getProgress()));
//偏向顏色的值
colorText.setText("顏色值:#" + Integer.toHexString(seekBarA.getProgress())
+ Integer.toHexString(seekBarR.getProgress())
+ Integer.toHexString(seekBarG.getProgress())
+ Integer.toHexString(seekBarB.getProgress()));
//用來顯示偏向的顏色
colorView.setBackgroundColor(Color.argb(seekBarA.getProgress(),
seekBarR.getProgress(),
seekBarG.getProgress(),
seekBarB.getProgress()));
//為組件設置新的過濾器
imageView.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
}
效果演示如下:
2.色相
本來還不是很明白色相指的什么,查查百度才知道:色相是色彩的首要特征,是區別各種不同色彩的最準確的標準。
正如PS中使用的的色相/飽和度功能,它可以通過修改顏色的三個基本屬性:色相、彩度、明度,來進行調節色彩。
那么,在Android中如何做呢?同樣 我寫了三個SeekBar范圍值也分別是0-255。
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
float mHueValue = (seekBarHue.getProgress() - 128f) * 1.0f / 128f * 180;
float mSaturationValue = seekBarSaturation.getProgress() / 128f;
float mLightnessValue = seekBarLightness.getProgress() / 128f;
//設置色相
mHueMatrix.reset();
mHueMatrix.setRotate(0, mHueValue);
mHueMatrix.setRotate(1, mHueValue);
mHueMatrix.setRotate(2, mHueValue);
//設置飽和度
mSaturationMatrix.reset();
mSaturationMatrix.setSaturation(mSaturationValue);
//亮度
mLightnessMatrix.reset();
mLightnessMatrix.setScale(mLightnessValue, mLightnessValue, mLightnessValue, 1);
colorMatrix.reset();// 效果疊加
colorMatrix.postConcat(mLightnessMatrix);
colorMatrix.postConcat(mSaturationMatrix);
colorMatrix.postConcat(mHueMatrix);
imageView.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
}
效果如下:
3.簡單的顏色濾鏡封裝
通過上方的舉例,我們很容易將調節過后的偏移矩陣,封裝成一些濾鏡使用。
我找了一些已經設置好了的偏移矩陣作為濾鏡使用,具體效果如圖:
下面貼出我封裝的固定顏色偏向濾鏡和固定偏移矩陣濾鏡的方法:
/**
* 為imageView設置顏色濾鏡
*
* @param imageView
* @param colormatrix
*/
public static void imageViewColorFilter(ImageView imageView, float[] colormatrix) {
setColorMatrixColorFilter(imageView, new ColorMatrixColorFilter(new ColorMatrix(colormatrix)));
}
/**
* 為imageView設置顏色偏向濾鏡
*
* @param imageView
* @param color
*/
public static void imageViewColorFilter(ImageView imageView, int color) {
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setScale(Color.alpha(color), Color.red(color), Color.green(color), Color.blue(color));
setColorMatrixColorFilter(imageView, new ColorMatrixColorFilter(colorMatrix));
}
/**
* 生成對應顏色偏向濾鏡的圖片,并回收原圖
*
* @param bitmap
* @param color
* @return
*/
public static Bitmap bitmapColorFilter(Bitmap bitmap, int color) {
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setScale(Color.alpha(color), Color.red(color), Color.green(color), Color.blue(color));
return setColorMatrixColorFilter(bitmap, new ColorMatrixColorFilter(colorMatrix), true);
}
/**
* 生成對應顏色濾鏡的圖片,并回收原圖
*
* @param bitmap
* @param colormatrix
* @return
*/
public static Bitmap bitmapColorFilter(Bitmap bitmap, float[] colormatrix) {
return setColorMatrix(bitmap, colormatrix, true);
}
/**
* 生成對應顏色濾鏡的圖片
*
* @param bitmap
* @param colormatrix
* @param isRecycle
* @return
*/
public static Bitmap setColorMatrix(Bitmap bitmap, float[] colormatrix, boolean isRecycle) {
return setColorMatrixColorFilter(bitmap, new ColorMatrixColorFilter(new ColorMatrix(colormatrix)), isRecycle);
}
public static void setColorMatrixColorFilter(ImageView imageView, ColorMatrixColorFilter matrixColorFilter) {
imageView.setColorFilter(matrixColorFilter);
}
public static Bitmap setColorMatrixColorFilter(Bitmap bitmap, ColorMatrixColorFilter matrixColorFilter, boolean isRecycle) {
Bitmap resource = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColorFilter(matrixColorFilter);
Canvas canvas = new Canvas(resource);
canvas.drawBitmap(bitmap, 0, 0, paint);
if (isRecycle)
BitmapUtils.destroyBitmap(bitmap);
return resource;
}
包括之前的Matrix的代碼都在Git上的demo里有詳細的舉例,就先寫到這里把,應該也沒有什么好整理的了。過幾天會可能會把之前的Android海報制作弄完。
喜歡這邊文章的話,別忘了點個贊哈。