Android實(shí)現(xiàn)制作灰色圖片

色彩矩陣

要想實(shí)現(xiàn)Android圖像特效處理需要了解色彩矩陣:

色彩處理需要三個(gè)方面調(diào)整圖片顏色:
1、色調(diào)-------------物體傳播的顏色

2、飽和度--------------------顏色的純度

3、亮度-------------------顏色的相對(duì)明暗

Android系統(tǒng)封裝了ColorMatrix類,通過這個(gè)類可以很簡(jiǎn)單通過修改矩陣值來修改圖片顏色效果。

實(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;

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容