這一篇,咱們來講說Bitmap
相關的繪製以及顏色濾鏡java
Bitmap
繪製Bitmap
的繪製,主要有如下4個方法,其中二、3能夠說是同樣的算法
drawBitmap(Bitmap bitmap, float left, float top, Paint paint)
canvas
drawBitmap(Bitmap bitmap, Rect src, RectF dst,Paint paint)
spa
drawBitmap(Bitmap bitmap, Rect src, Rect dst,Paint paint)
3d
drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint)
code
drawBitmap(Bitmap bitmap, float left, float top, Paint paint)
其中,left,top
爲開始繪製起點的左上角的座標cdn
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStrokeWidth(3);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.woman);
//左上角的座標left,top
canvas.drawBitmap(bitmap, 100, 100, paint);
複製代碼
drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)
其中,src
本次繪製的原區域,dst
本次繪製的目標區域,簡單來講,就是把src
區域的圖像繪製到dst
區域blog
Rect rectF = new Rect(100, 300, 300, 100);
canvas.drawRect(rectF, paint);
canvas.drawBitmap(bitmap, null, rectF, paint);
Rect dst = new Rect(0, 0, 300, 300);
Rect src = new Rect(0, 0, bitmap.getWidth() / 2, bitmap.getHeight() / 2);
canvas.drawRect(src, paint);
//src 本次繪製的原區域 dst 本次繪製的目標區域
canvas.drawBitmap(bitmap, src, dst, paint);
canvas.drawBitmap(bitmap, 0, bitmap.getHeight() / 2, paint);
複製代碼
drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint)
其中,matrix
爲矩陣,能夠對圖像進行一些簡單的處理圖片
Matrix matrix = new Matrix();
// //縮放
matrix.setScale(0.5f, 0.5f);
// //平移
// matrix.setTranslate(100, 100);
// //旋轉
// matrix.setRotate(30);
// //傾斜
// matrix.setSkew(0.5f, 0.5f);
//
canvas.drawBitmap(bitmap, matrix, paint);
複製代碼
ColorFilter
顏色濾鏡首先,對於色彩的存儲,Bitmap
類使用一個32位的數值來保存。紅(R)、綠(G)、藍(B)及透明度(A)各佔8位,也就是所謂的RGBA,每個色彩份量的取值範圍是0-255。透明度爲0表示徹底透明,爲255時,色彩徹底可見。ip
Android
中的顏色矩陣是一個 4x5
的數字矩陣,它用來對圖片的色彩進行處理。
R1 = aR + bG + cB + dA + e;
G1 = fR + gG + hB + iA + j;
B1 = kR + lG + mB + nA + o;
A1 = pR + qG + rB + sA + t;
複製代碼
這也解釋了爲啥用五階矩陣,好比說只是在原R
上添加一些偏移量,使用四階矩陣難以實現這個效果,(四階矩陣只能實現乘法效果) 下面咱們結合一些實例,再來深刻了解下
去掉紅色和綠色
ColorMatrix colorMatrix = new ColorMatrix(new float[]{
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 1, 0,
});
paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
canvas.drawBitmap(bitmap, 400, 0, paint);
複製代碼
//反相
ColorMatrix colorMatrix = new ColorMatrix(new float[]{
-1, 0, 0, 0, 255,
0, -1, 0, 0, 255,
0, 0, -1, 0, 255,
0, 0, 0, 1, 0,
});
paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
canvas.drawBitmap(bitmap, 400, 0, paint);
複製代碼
對R、G、B、A同時增大
//變亮
ColorMatrix light = new ColorMatrix(new float[]{
1.2f, 0, 0, 0, 0,
0, 1.2f, 0, 0, 0,
0, 0, 1.2f, 0, 0,
0, 0, 0, 1.2f, 0,
});
paint.setColorFilter(new ColorMatrixColorFilter(light));
canvas.drawBitmap(bitmap, 400, 0, paint);
複製代碼
只要把RGB的色彩信息設置成同樣;即:R=G=B,那麼圖像就變成了灰色
//灰度
ColorMatrix grey1 = new ColorMatrix(new float[]{
0.33f, 0.59f, 0.11f, 0, 0,
0.33f, 0.59f, 0.11f, 0, 0,
0.33f, 0.59f, 0.11f, 0, 0,
0, 0, 0, 1, 0,
});
paint.setColorFilter(new ColorMatrixColorFilter(grey1));
canvas.drawBitmap(bitmap, 400, 0, paint);
複製代碼
還有另一種算法,0.213, 0.715, 0.072 這是谷歌給出的比例,跟人眼的色彩學有關係
//灰度2
ColorMatrix grey2 = new ColorMatrix(new float[]{
0.213f, 0.715f, 0.072f, 0, 0,
0.213f, 0.715f, 0.072f, 0, 0,
0.213f, 0.715f, 0.072f, 0, 0,
0, 0, 0, 1, 0,
});
paint.setColorFilter(new ColorMatrixColorFilter(grey2));
canvas.drawBitmap(bitmap, 400, 0, paint);
複製代碼
//紅綠反色
ColorMatrix rtog = new ColorMatrix(new float[]{
0, 1, 0, 0, 0,
1, 0, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 1, 0,
});
paint.setColorFilter(new ColorMatrixColorFilter(rtog));
複製代碼
這位不知名的美女,原諒我吧
//變舊
ColorMatrix old = new ColorMatrix(new float[]{
1 / 2f, 1 / 2f, 1 / 2f, 0, 0,
1 / 3f, 1 / 3f, 1 / 3f, 0, 0,
1 / 4f, 1 / 4f, 1 / 4f, 0, 0,
0, 0, 0, 1, 0
});
paint.setColorFilter(new ColorMatrixColorFilter(old));
canvas.drawBitmap(bitmap, 400, 0, paint);
複製代碼
//粉調
ColorMatrix pink = new ColorMatrix(new float[]{
0.8f, 0, 0, 0, 0,
0, 0.8f, 0, 0, 0,
0, 0.7f, 0.7f, 0, 0,
0, 0, 0, 0.8f, 0,
});
paint.setColorFilter(new ColorMatrixColorFilter(pink));
canvas.drawBitmap(bitmap, 400, 0, paint);
複製代碼