Paint除了通過setColor()/setARGB()/setAlpha()等方法設置paint的顏色外,還可以通過設置濾鏡ColorFilter來改變繪制出來的顏色。ColorFilter對應以下三個子類。
?顏色矩陣顏色過濾器:ColorMatrixColorFilter
光照色彩過濾器:LightingColorFilter
混排顏色過濾器濾器PorterDuffColorFilter
1 LightingColorFilter
先來看看這個ColorFilter的構造器,它接受兩個參數,一個是mul,一個是add
LightingColorFilter(
int mul,
int add)
復制代碼比如你現在設置paint的color是#1b8fe6,然后通過濾鏡后想要讓其顏色不變,那么可以設置mul為0xffffff,add為0x000000。 前兩個ff表示紅色的十六進制 中間兩個ff表示綠色的十六進制 后邊兩個ff表示藍色的十六進制 轉換成十進制就是0-255的范圍值 值越大 表示對應的顏色就越深??add為0x000000 這是顏色加深的效果 比如 想要 綠色增強 就可以把 中間兩個00改成000100,?000800,?008800??直到255 最大值也是255? 對應十六進制的最大值為0x00ff00??其實這個玩意是這樣去計算的,我們記paint設置的顏色中三基色的值為R,G,B,mul中的三基色的值為mul.R,mul.G,mul.B,add中的三基色的值為add.R,add.G,add.B,那么最終的三基色為jis
經過濾鏡后的R = R * mul.R / 0xff + add.R
經過濾鏡后的G = G * mul.G / 0xff + add.G
經過濾鏡后的B = B * mul.B / 0xff + add.B
復制代碼RGBA模型:
RGBA不知道你聽過沒,黃綠藍知道了吧,光的三基色,而RAGB則是在此的基礎上多了一個透明度! R(Red紅色),G(Green綠色),B(Blue藍色),A(Alpha透明度);另外要和顏料的三 原色區分開來哦,最明顯的區別就是顏料的三原色中用黃色替換了光三基色中的綠色!
接下來 我們使用?
PorterDuff.Mode 圖層混合 和??LightingColorFilter 實現一個濾鏡效果,先上效果圖
正常圖片
使用濾鏡后的圖片
public class PictureView extends View
{private LightingColorFilter lightingColorFilter;private Paint mPaint;private int mWidth, mHeight;Bitmap mBitmap;public PictureView(Context context){this(context, null);}public PictureView(Context context, AttributeSet attrs){this(context, attrs, 0);}public PictureView(Context context, AttributeSet attrs, int defStyleAttr){super(context, attrs, defStyleAttr);init();}private void
init(){//初始化畫筆mPaint = new Paint();mPaint.setColor(Color.RED);mPaint.setStyle(Paint.Style.FILL_AND_STROKE);mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.buteys).copy(Bitmap.Config.ARGB_8888,
true);}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh){super.onSizeChanged(w, h, oldw, oldh);mWidth = w;mHeight = h;}@Overrideprotected void onDraw(Canvas canvas){super.onDraw(canvas);
setBackgroundColor(Color.WHITE);//離屏繪制 這里將圖像合成的處理放到離屏緩存中進行int layerId = canvas.saveLayer(0, 0, getWidth(), getHeight(), mPaint, Canvas.ALL_SAVE_FLAG);//目標圖canvas.drawBitmap(mBitmap, mWidth / 2 - mBitmap.getWidth() / 2-120, mHeight / 3 - mBitmap.getHeight() / 4, mPaint);mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));canvas.drawBitmap(createCircleBitmap(mWidth, mHeight), 0, 0, mPaint);//清除混合模式mPaint.setXfermode(null);canvas.restoreToCount(layerId);}//畫圓srcpublic Bitmap createCircleBitmap(int width, int height){Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);Canvas canvas = new Canvas(bitmap);Paint scrPaint = new Paint(Paint.ANTI_ALIAS_FLAG);scrPaint.setColor(0xFFFFCC44);canvas.drawCircle(width / 2, height / 2, 500, scrPaint);
return bitmap;}/*** 獲取RGB值** @param r* @param g* @param b*/public void changeRGB(int r, int g, int b) {int mr = 0;int mg = 0;int mb = 0;int ar = 0;int ag = 0;int ab = 0;
if (r < 0) {mr = r + 255;ar = 0;}
else if (r == 0) {mr = 255;ar = 0;}
else {mr = 255;ar = r;}
if (g < 0) {mg = g + 255;ag = 0;}
else if (g == 0) {mg = 255;ag = 0;}
else {mg = 255;ag = g;}
if (b < 0) {mb = b + 255;ab = 0;}
else if (b == 0) {mb = 255;ab = 0;}
else {mb = 255;ab = b;}lightingColorFilter = new LightingColorFilter(0xffffff,0x000000);lightingColorFilter = new LightingColorFilter(Integer.valueOf(transString(Integer.toHexString(mr)) + transString(Integer.toHexString(mg)) + transString(Integer.toHexString(mb)), 16),Integer.valueOf(transString(Integer.toHexString(ar)) + transString(Integer.toHexString(ag)) + transString(Integer.toHexString(ab)), 16));mPaint.setColorFilter(lightingColorFilter);invalidate();}/*** 處理RGB** @param s* @
return*/public String transString(String s) {
if (s.length() == 1) {
return "0" + s;}
else {
return s;}}
}
復制代碼
<?xml version=
"1.0" encoding=
"utf-8"?>
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"xmlns:app=
"http://schemas.android.com/apk/res-auto"xmlns:tools=
"http://schemas.android.com/tools"android:layout_width=
"match_parent"android:layout_height=
"match_parent"android:orientation=
"vertical"tools:context=
".MainActivity"><com.rx.myapplication.PictureViewandroid:id=
"@+id/pv"android:layout_width=
"match_parent"android:layout_height=
"350dp" /><LinearLayoutandroid:layout_width=
"match_parent"android:layout_height=
"match_parent"android:orientation=
"vertical"><LinearLayoutandroid:layout_width=
"match_parent"android:layout_height=
"0dp"android:layout_weight=
"1"android:gravity=
"center_vertical"android:orientation=
"horizontal"android:paddingRight=
"10dp"><TextViewandroid:layout_width=
"wrap_content"android:layout_height=
"wrap_content"android:layout_marginLeft=
"10dp"android:layout_marginRight=
"10dp"android:text=
"R"android:textSize=
"18sp" /><SeekBarandroid:id=
"@+id/sb_r"android:layout_width=
"0dp"android:layout_height=
"wrap_content"android:layout_weight=
"8" /><TextViewandroid:id=
"@+id/tv_r"android:layout_width=
"0dp"android:layout_height=
"wrap_content"android:layout_weight=
"1"android:gravity=
"center" /></LinearLayout><LinearLayoutandroid:layout_width=
"match_parent"android:layout_height=
"0dp"android:layout_weight=
"1"android:gravity=
"center_vertical"android:orientation=
"horizontal"android:paddingRight=
"10dp"><TextViewandroid:layout_width=
"wrap_content"android:layout_height=
"wrap_content"android:layout_marginLeft=
"10dp"android:layout_marginRight=
"10dp"android:text=
"G"android:textSize=
"18sp" /><SeekBarandroid:id=
"@+id/sb_g"android:layout_width=
"0dp"android:layout_height=
"wrap_content"android:layout_weight=
"8" /><TextViewandroid:id=
"@+id/tv_g"android:layout_width=
"0dp"android:layout_height=
"wrap_content"android:layout_weight=
"1"android:gravity=
"center" /></LinearLayout><LinearLayoutandroid:layout_width=
"match_parent"android:layout_height=
"0dp"android:layout_weight=
"1"android:gravity=
"center_vertical"android:orientation=
"horizontal"android:paddingRight=
"10dp"><TextViewandroid:layout_width=
"wrap_content"android:layout_height=
"wrap_content"android:layout_marginLeft=
"10dp"android:layout_marginRight=
"10dp"android:text=
"B"android:textSize=
"18sp" /><SeekBarandroid:id=
"@+id/sb_b"android:layout_width=
"0dp"android:layout_height=
"wrap_content"android:layout_weight=
"8" /><TextViewandroid:id=
"@+id/tv_b"android:layout_width=
"0dp"android:layout_height=
"wrap_content"android:layout_weight=
"1"android:gravity=
"center" /></LinearLayout></LinearLayout></LinearLayout>
復制代碼
public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener
{private SeekBar sb_r;private SeekBar sb_g;private SeekBar sb_b;private TextView tv_r;private TextView tv_g;private TextView tv_b;private PictureView pv;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);initView();sb_r.setOnSeekBarChangeListener(this);sb_g.setOnSeekBarChangeListener(this);sb_b.setOnSeekBarChangeListener(this);}private void
initView(){pv = (PictureView) findViewById(R.id.pv);sb_r = (SeekBar) findViewById(R.id.sb_r);sb_g = (SeekBar) findViewById(R.id.sb_g);sb_b = (SeekBar) findViewById(R.id.sb_b);tv_r = (TextView) findViewById(R.id.tv_r);tv_g = (TextView) findViewById(R.id.tv_g);tv_b = (TextView) findViewById(R.id.tv_b);sb_r.setMax(510);sb_g.setMax(510);sb_b.setMax(510);sb_r.setProgress(255);sb_g.setProgress(255);sb_b.setProgress(255);}@Overridepublic void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){pv.changeRGB(sb_r.getProgress() - 255, sb_g.getProgress() - 255, sb_b.getProgress() - 255);switch (seekBar.getId()){
case R.id.sb_r:tv_r.setText(progress - 255 +
"");
break;
case R.id.sb_g:tv_g.setText(progress - 255 +
"");
break;
case R.id.sb_b:tv_b.setText(progress - 255 +
"");
break;}}@Overridepublic void onStartTrackingTouch(SeekBar seekBar){}@Overridepublic void onStopTrackingTouch(SeekBar seekBar){}
}
復制代碼
2 PortDuffColorFilter
PorterDuffColorFilter porterDuffColorFilter=new PorterDuffColorFilter(Color.RED,PorterDuff.Mode.DARKEN);
mPaint.setColorFilter(porterDuffColorFilter);
invalidate();
復制代碼最后的效果 紅色加深
3 ColorMatrixColorFilter? ? ?
1、ColorMatrix處理圖片原理?
ColorMatrix正如我們所翻譯的“顏色矩陣”,它是通過矩陣的方式作用與圖像的像素來實現的圖片的處理。?
?(1)ColorMAtrix顏色矩陣 顏色矩陣M是一個5*4的矩陣,如圖所示。但是在Android中,顏色矩陣M是以一維數組M=[a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t]的方式進行存儲的。
(2)顏色的分量矩陣 顏色的分量矩陣分別有R、G、B、A、1組成,用于調整三原色和透明度 (3)變換過程
第一行將會影響紅色,第二行影響綠色,第三行影響藍色,最后一行操作的是Alpha值。
ps:默認的ColorMatrix如下面所示,它是不會改變圖像的。
1,0,0,0,0
0,1,0,0,0
0,0,1,0,0
0,0,0,1,0
當使用默認矩陣時 ,圖像保持不變,
float[] colorMatrix = {1,0,0,0,0, //red0,1,0,0,0, //green0,0,1,0,0, //blue0,0,0,1,0 //alpha
};ColorMatrixColorFilter colorMatrixColorFilter = new ColorMatrixColorFilter(colorMatrix);
mPaint.setColorFilter(colorMatrixColorFilter);
復制代碼
現在我們修改 RGBA值的系數? 矩陣的紅色變成 2,可以看到紅色加深了
float[] colorMatrix = {2,0,0,0,0, //red0,1,0,0,0, //green0,0,1,0,0, //blue0,0,0,1,0 //alpha
};ColorMatrixColorFilter colorMatrixColorFilter = new ColorMatrixColorFilter(colorMatrix);
mPaint.setColorFilter(colorMatrixColorFilter)
復制代碼
我們修改 RGBA值的系數 矩陣的綠色變成2,可以看到綠色加深了
float[] colorMatrix = {1,0,0,0,0, //red0,2,0,0,0, //green0,0,1,0,0, //blue0,0,0,1,0 //alpha
};ColorMatrixColorFilter colorMatrixColorFilter = new ColorMatrixColorFilter(colorMatrix);
mPaint.setColorFilter(colorMatrixColorFilter);
復制代碼
我們也可以修改矩陣的偏移量 來過濾圖片,我們讓紅色和綠色的偏移量都變成60,照片變成黃色系
float[] colorMatrix = {1,0,0,0,60, //red0,1,0,0,60, //green0,0,1,0,0, //blue0,0,0,1,0 //alpha
};ColorMatrixColorFilter colorMatrixColorFilter = new ColorMatrixColorFilter(colorMatrix);
mPaint.setColorFilter(colorMatrixColorFilter);
復制代碼
// 膠片效果
復制代碼public static final
float colormatrix_fanse[] = {-1.0f, 0.0f, 0.0f, 0.0f, 255.0f,0.0f, -1.0f, 0.0f, 0.0f, 255.0f,0.0f, 0.0f, -1.0f, 0.0f, 255.0f,0.0f, 0.0f, 0.0f, 1.0f, 0.0f};mColorMatrixColorFilter = new ColorMatrixColorFilter(colormatrix_fanse);
mPaint.setColorFilter(mColorMatrixColorFilter);
canvas.drawBitmap(mBitmap, 100, 0, mPaint);
復制代碼
常用效果
// 黑白
public static final
float colormatrix_heibai[] = {0.8f, 1.6f, 0.2f, 0, -163.9f,0.8f, 1.6f, 0.2f, 0, -163.9f,0.8f, 1.6f, 0.2f, 0, -163.9f,0, 0, 0, 1.0f, 0};
// 懷舊
public static final
float colormatrix_huajiu[] = {0.2f, 0.5f, 0.1f, 0, 40.8f,0.2f, 0.5f, 0.1f, 0, 40.8f,0.2f, 0.5f, 0.1f, 0, 40.8f,0, 0, 0, 1, 0};
// 哥特
public static final
float colormatrix_gete[] = {1.9f, -0.3f, -0.2f, 0, -87.0f,-0.2f, 1.7f, -0.1f, 0, -87.0f,-0.1f, -0.6f, 2.0f, 0, -87.0f,0, 0, 0, 1.0f, 0};
// 淡雅
public static final
float colormatrix_danya[] = {0.6f, 0.3f, 0.1f, 0, 73.3f,0.2f, 0.7f, 0.1f, 0, 73.3f,0.2f, 0.3f, 0.4f, 0, 73.3f,0, 0, 0, 1.0f, 0};
// 藍調
public static final
float colormatrix_landiao[] = {2.1f, -1.4f, 0.6f, 0.0f, -71.0f,-0.3f, 2.0f, -0.3f, 0.0f, -71.0f,-1.1f, -0.2f, 2.6f, 0.0f, -71.0f,0.0f, 0.0f, 0.0f, 1.0f, 0.0f};
// 光暈
public static final
float colormatrix_guangyun[] = {0.9f, 0, 0, 0, 64.9f,0, 0.9f, 0, 0, 64.9f,0, 0, 0.9f, 0, 64.9f,0, 0, 0, 1.0f, 0};
// 夢幻
public static final
float colormatrix_menghuan[] = {0.8f, 0.3f, 0.1f, 0.0f, 46.5f,0.1f, 0.9f, 0.0f, 0.0f, 46.5f,0.1f, 0.3f, 0.7f, 0.0f, 46.5f,0.0f, 0.0f, 0.0f, 1.0f, 0.0f};
// 酒紅
public static final
float colormatrix_jiuhong[] = {1.2f, 0.0f, 0.0f, 0.0f, 0.0f,0.0f, 0.9f, 0.0f, 0.0f, 0.0f,0.0f, 0.0f, 0.8f, 0.0f, 0.0f,0, 0, 0, 1.0f, 0};
// 膠片
public static final
float colormatrix_fanse[] = {-1.0f, 0.0f, 0.0f, 0.0f, 255.0f,0.0f, -1.0f, 0.0f, 0.0f, 255.0f,0.0f, 0.0f, -1.0f, 0.0f, 255.0f,0.0f, 0.0f, 0.0f, 1.0f, 0.0f};
// 湖光掠影
public static final
float colormatrix_huguang[] = {0.8f, 0.0f, 0.0f, 0.0f, 0.0f,0.0f, 1.0f, 0.0f, 0.0f, 0.0f,0.0f, 0.0f, 0.9f, 0.0f, 0.0f,0, 0, 0, 1.0f, 0};
// 褐片
public static final
float colormatrix_hepian[] = {1.0f, 0.0f, 0.0f, 0.0f, 0.0f,0.0f, 0.8f, 0.0f, 0.0f, 0.0f,0.0f, 0.0f, 0.8f, 0.0f, 0.0f,0, 0, 0, 1.0f, 0};
// 復古
public static final
float colormatrix_fugu[] = {0.9f, 0.0f, 0.0f, 0.0f, 0.0f,0.0f, 0.8f, 0.0f, 0.0f, 0.0f,0.0f, 0.0f, 0.5f, 0.0f, 0.0f,0, 0, 0, 1.0f, 0};
// 泛黃
public static final
float colormatrix_huan_huang[] = {1.0f, 0.0f, 0.0f, 0.0f, 0.0f,0.0f, 1.0f, 0.0f, 0.0f, 0.0f,0.0f, 0.0f, 0.5f, 0.0f, 0.0f,0, 0, 0, 1.0f, 0};
// 傳統
public static final
float colormatrix_chuan_tong[] = {1.0f, 0.0f, 0.0f, 0, -10f,0.0f, 1.0f, 0.0f, 0, -10f,0.0f, 0.0f, 1.0f, 0, -10f,0, 0, 0, 1, 0};
// 膠片2
public static final
float colormatrix_jiao_pian[] = {0.71f, 0.2f, 0.0f, 0.0f, 60.0f,0.0f, 0.94f, 0.0f, 0.0f, 60.0f,0.0f, 0.0f, 0.62f, 0.0f, 60.0f,0, 0, 0, 1.0f, 0};// 銳色
public static final
float colormatrix_ruise[] = {4.8f, -1.0f, -0.1f, 0, -388.4f,-0.5f, 4.4f, -0.1f, 0, -388.4f,-0.5f, -1.0f, 5.2f, 0, -388.4f,0, 0, 0, 1.0f, 0};
// 清寧
public static final
float colormatrix_qingning[] = {0.9f, 0, 0, 0, 0,0, 1.1f, 0, 0, 0,0, 0, 0.9f, 0, 0,0, 0, 0, 1.0f, 0};
// 浪漫
public static final
float colormatrix_langman[] = {0.9f, 0, 0, 0, 63.0f,0, 0.9f, 0, 0, 63.0f,0, 0, 0.9f, 0, 63.0f,0, 0, 0, 1.0f, 0};
// 夜色
public static final
float colormatrix_yese[] = {1.0f, 0.0f, 0.0f, 0.0f, -66.6f,0.0f, 1.1f, 0.0f, 0.0f, -66.6f,0.0f, 0.0f, 1.0f, 0.0f, -66.6f,0.0f, 0.0f, 0.0f, 1.0f, 0.0f};
復制代碼ColorMatrix 類的使用
正常圖片
ColorMatrix cm = new ColorMatrix();//亮度調節cm.setScale(1,2,1,1);綠色高亮
復制代碼
//飽和度調節0-無色彩 就是黑白圖片, 1- 默認效果, >1飽和度加強//cm.setScale(1,2,1,1);cm.setSaturation(2);
復制代碼
cm.setSaturation(2);
//色調調節
cm.setRotate(0, 45);
復制代碼
轉載于:https://juejin.im/post/5cf71d786fb9a07ef63fcac6
總結
以上是生活随笔為你收集整理的Lightingcolorfilter 滤镜的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。