色彩矩陣
要想實(shí)現(xiàn)Android圖像特效處理需要了解色彩矩陣:
色彩處理需要三個(gè)方面調(diào)整圖片顏色:
1、色調(diào)-------------物體傳播的顏色
2玫荣、飽和度--------------------顏色的純度
3形用、亮度-------------------顏色的相對(duì)明暗
Android系統(tǒng)封裝了ColorMatrix類臼疫,通過(guò)這個(gè)類可以很簡(jiǎn)單通過(guò)修改矩陣值來(lái)修改圖片顏色效果。
實(shí)例化:
ColorMatrix colorMatrix =newColorMatrix();
1融撞、setRotate(int axis勉耀,float degree)設(shè)置色調(diào)指煎,參數(shù)一用0、1便斥、2代表red至壤、Green、Blue三種顏色枢纠,參數(shù)二表示需要處理的值像街;
2、setSaturation(float sat)設(shè)置顏色飽和度晋渺,參數(shù)表示飽和度值镰绎,參數(shù)為0就是灰色頭像了
3、setScale(foat lum,float lum,float lum,1)設(shè)置亮度木西,當(dāng)lum為0時(shí)畴栖,圖片就變?yōu)楹谏?/p>
4.postConcat()方法將矩陣效果混合,從而疊加處理效果户魏。
效果展示:
附上代碼:
**
* Created by MSI on 2017/6/21.
*/
public class GrayPicture {
Bitmap bitmap;
/**
*
*@param btm 需要變色的圖片
*@param mHue 色調(diào)值驶臊,該出默認(rèn)為0
*@param mStauration 飽和度值,該出默認(rèn)為0
*@param mLum 亮度值叼丑,該出默認(rèn)為1,亮度值為0則會(huì)出現(xiàn)黑屏
*/
public Bitmap setBitmap(Bitmap btm,floatmHue,floatmStauration ,floatmLum){
ColorMatrix colorMatrix =newColorMatrix();
colorMatrix.setRotate(0, mHue);
colorMatrix.setRotate(1, mHue);
colorMatrix.setRotate(2, mHue);
ColorMatrix colorMatrix1 =newColorMatrix();
colorMatrix1.setSaturation(mStauration);
ColorMatrix colorMatrix2 =newColorMatrix();
//? ? ? ? colorMatrix2.setScale(mLum, mLum, mLum, 1);
colorMatrix2.setScale(1,1,1,1);
ColorMatrix colorMatrixs =newColorMatrix();
colorMatrixs.postConcat(colorMatrix);
colorMatrixs.postConcat(colorMatrix1);
colorMatrixs.postConcat(colorMatrix2);
bitmap= Bitmap.createBitmap(btm.getWidth(), btm.getHeight(), Bitmap.Config.ARGB_8888);
finalPaint paint =newPaint();
paint.setAntiAlias(true);
Canvas canvas =newCanvas(bitmap);
paint.setColorFilter(newColorMatrixColorFilter(colorMatrixs));
canvas.drawBitmap(btm,0,0, paint);
returnbitmap;
}
}