色彩H由绕V轴的旋转角给定。红色对应于角度0°,绿色对应于角度120°,蓝色对应于角度240°。
在HSV颜色模型中,每一种颜色和它的补色相差180°。饱和度S取值从0到1,所以圆锥顶面的半径为1。
5.看一下黄色的几种表达方式:
RGB:R:255 G:255 B:0 #ffff00
CMYK:C:10% M:0 Y:83% K:0
HSV:H:60° S:100% V:100%
好了,科普结束,下面进入正题
一、Android中的Color
颜色使用场景:
1.基本使用:背景、阴影、文字颜色
2.基于Color创建的Bitmap以及叠合模式:Xfermode
3.paint中的着色、颜色过滤器 4.ColorMatrix的使用
1.常量:
2.构造函数
可见只有无参数构造可以用
3.常用方法:
int blue = Color.BLUE;//第一种获取蓝色方法
blue = Color.parseColor(“#0000FF”);//第二种获取蓝色方法
blue = Color.rgb(0, 0, 255);//第三种获取蓝色方法
blue = Color.argb(255, 0, 0, 255);//第四种获取蓝色方法
blue = Color.HSVToColor(new float[]{240.0f, 1.0f, 1.0f});//第五种获取蓝色方法
blue = 0xff0000FF;//第六种获取蓝色方法
//(吐槽:怎么有种孔乙己说茴香豆的茴字有多少种写法一样…,看哪个顺手就用哪个吧)
float[] hsv = {0, 0, 0};//hsv数组
Color.RGBToHSV(0, 0, 255, hsv);//将RGB转为hsv
Log.e(TAG, “onDraw: " + hsv[0]+”,“+hsv[1]+”,"+hsv[2]);
//onDraw: 240.0,1.01.0
其实Color的本身并没有太多的知识点,毕竟就是一个int而已,难点在于颜色的拼合与变换
二、Android位图封装类:Bitmap
什么是位图,前面讲过颜色是按位存储的,ARGB_8888每种颜色占8位
相信大家都知道一张jpg或png放大后会是一个个小格子,称为一个像素(px),而且一个小格子是一种颜色
也就是一张jpg或png图片就是很多颜色的合集,而这些合集信息都被封装到了Bitmap类中
你可以使用Bitmap获取任意像素点,并修改它,对与某像素点而言,颜色信息是其主要的部分
1.重新认识Bitmap
我们一般使用Bitmap是都是用BitmapFactory来decode资源,所以并未设计太多Bitmap的操作,以致认为Bitmap=图片
Bitmap实际是一个封装图片像素信息的类,它能显示出来是因为View及手机的硬件
1).创建一个Bitmap:
注意区别bitmapCanvas和View中OnDraw中Canvas的区别:
这里:bitmapCanvas是负责在位图(Bitmap)上绘制,让位图记录像素点位信息的
OnDraw中Canvas是用来在View上绘制,显示在屏幕上的。
打个不恰当的比方:
你是bitmapCanvas,负责画一张图(Bitmap),你画完后不能直接交给印刷人员(View)去印
需要交给审稿员(OnDraw中canvas),经过他允许才能给印刷人员
/**
- 创建一个Bitmap
- @param color 背景色
- @return bitmap
*/
private Bitmap createBitmap(int color) {
//创建一个ARGB_8888,宽高200的bitmap
Bitmap bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
//使用Bitmap创建一个canvas画板,画板上的一切都会保留在bitmap上
Canvas bitmapCanvas = new Canvas(bitmap);
//接下来就是在画板上操作
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
p.setColor(color);
Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
bitmapCanvas.drawRect(rect, p);
p.setColor(Color.GRAY);
p.setStrokeWidth(3);
bitmapCanvas.drawLine(0, 0, 200, 200, p);
bitmapCanvas.drawLine(200, 0, 0, 200, p);
return bitmap;
}
OnDraw中使用Bitmap,使用Bitmap,使用Bitmap…
//审稿人统一,印刷到View上
canvas.drawBitmap(mBitmap, 100, 100, mMainPaint);
三、Xfermode:图片叠合时的处理方式
终于写到这里了,总算与Xfermode相遇了,最喜欢分析很多的情况,这里有18种模式,想想都激动…。
做开发的,我们应该知道src和dst吧src是源,dst是目标,在react里就有src的源文件,和dest的输出文件
图片叠合顾名思义,必须有两个图片才行,这里原图src用蓝色正方形,目标dst用绿色圆形
1.src和dst图片
/**
- 创建源图片
- @return bitmap
*/
private Bitmap createSrcBitmap() {
//创建一个ARGB_8888,宽高200的bitmap
Bitmap bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
//使用Bitmap创建一个canvas画板,画板上的一切都会保留在bitmap上
Canvas bitmapCanvas = new Canvas(bitmap);
//接下来就是在画板上操作
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
p.setColor(0x882045F3);
Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
bitmapCanvas.drawRect(rect, p);
return bitmap;
}
/**
- 创建目标
- @return bitmap
*/
private Bitmap createDstBitmap() {
//创建一个ARGB_8888,宽高200的bitmap
Bitmap bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
//使用Bitmap创建一个canvas画板,画板上的一切都会保留在bitmap上
Canvas bitmapCanvas = new Canvas(bitmap);
//接下来就是在画板上操作
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
p.setColor(0xff43F41D);
bitmapCanvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getHeight() / 2,p);
return bitmap;
}
2.叠合模式18种:android.graphics.PorterDuff.Mode
别怕,别怕,一幅图展示一下:
public enum Mode {
CLEAR (0),
SRC (1),
DST (2),
SRC_OVER (3),
DST_OVER (4),
SRC_IN (5),
DST_IN (6),
SRC_OUT (7),
DST_OUT (8),
SRC_ATOP (9),
DST_ATOP (10),
XOR (11),
DARKEN (16),
LIGHTEN (17),
MULTIPLY (13),
SCREEN (14),
ADD (12),
OVERLAY (15);
Mode(int nativeInt) {
this.nativeInt = nativeInt;
}
public final int nativeInt;
}
3.如何优雅地绘制下面一幅图:
注意:测试了一下,开不开硬件加速对这东西有影响,下面在无有硬件加速:
android:hardwareAccelerated="false"
mMainPaint.setXfermode(XXX);放置的顺序也很重要,在下面的是叠合的源
网上有一组图,不过没有透明度,我对源(蓝色)加了88的透明度,显示的更清楚些
注意:看正方形框里的内容,看正方形框里的内容,看正方形框里的内容!因为它是被叠合的对象
private void init() {
mMainPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mMainPaint.setStyle(Paint.Style.FILL);
mMainPaint.setStrokeCap(Paint.Cap.ROUND);
src = createSrcBitmap();
dst = createDstBitmap();
//背景图层的笔
mLayerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mLayerPaint.setStyle(Paint.Style.FILL);
mLayerPaint.setFilterBitmap(false);
//文字的笔
mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mTextPaint.setTextSize(45);
Typeface typeface = Typeface.create(Typeface.MONOSPACE, Typeface.BOLD);
mTextPaint.setTypeface(typeface);
mTextPaint.setColor(0xffF98D1F);
//虚线画笔
mDashPaint = new Paint();
mDashPaint.setStrokeWidth(3);
mDashPaint.setColor(Color.RED);
mDashPaint.setStyle(Paint.Style.STROKE);
//设置虚线效果new float[]{可见长度, 不可见长度},偏移值
mDashPaint.setPathEffect(new DashPathEffect(new float[]{10, 5}, 0));
mModes = new PorterDuffXfermode[]{
new PorterDuffXfermode(PorterDuff.Mode.CLEAR),//0
new PorterDuffXfermode(PorterDuff.Mode.SRC),//1
new PorterDuffXfermode(PorterDuff.Mode.DST),//2
new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER),//3
new PorterDuffXfermode(PorterDuff.Mode.DST_OVER),//4
new PorterDuffXfermode(PorterDuff.Mode.SRC_IN),//5
new PorterDuffXfermode(PorterDuff.Mode.DST_IN),//6
new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT),//7
new PorterDuffXfermode(PorterDuff.Mode.DST_OUT),//8
new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP),//9
new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP),//10
new PorterDuffXfermode(PorterDuff.Mode.XOR),//11
new PorterDuffXfermode(PorterDuff.Mode.DARKEN),//12
new PorterDuffXfermode(PorterDuff.Mode.LIGHTEN),//13
new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY),//14
new PorterDuffXfermode(PorterDuff.Mode.SCREEN),//15
new PorterDuffXfermode(PorterDuff.Mode.ADD),//16
new PorterDuffXfermode(PorterDuff.Mode.OVERLAY),//17
};
mModeText = new String[]{“CLEAR”, “SRC”, “DST”, “SRC_OVER”, “DST_OVER”, “SRC_IN”,
“DST_IN”, “SRC_OUT”, “DST_OUT”, “SRC_ATOP”, “DST_ATOP”, “XOR”, “DARKEN”,
“LIGHTEN”, “MULTIPLY”, “SCREEN”, “ADD”, “OVERLAY”
};
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//创建一个图层,在图层上演示图形混合后的效果
int sc = 0;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
sc = canvas.saveLayer(new RectF(0, 0, 2500, 2500), mLayerPaint);
}
for (int i = 0; i < 18; i++) {
int line = i % 6;
int row = i / 6;
canvas.drawBitmap(dst, 350 * line, row * 350, mMainPaint);//目标图象
mMainPaint.setXfermode(mModes[i]);//设置对源的叠合模式
canvas.drawBitmap(src, 100 + 350 * line, 100 + row * 350, mMainPaint);
//辅助信息
canvas.drawText(mModeText[i],100 + 350 * line, 300 + row * 350,mTextPaint);
canvas.drawCircle(100 + 350 * line, 100 + row * 350, 100, mDashPaint);
canvas.drawRect(100 + 350 * line, 100 + row * 350, 100 + 200 + 350 * line, 100 + 200 + row * 350, mDashPaint);
}
canvas.restoreToCount(sc);
}
四、着色器:Shader(本节在Paint篇也有,为保全Color篇的完整性,这里cv了一下)
一个很简单的类,有5个子类:
1.线性渐变:
1).new LinearGradient(渐变起点x,y,渐变终点x,y,渐变色1,渐变色2,渐变模式)
渐变模式:
Shader.TileMode.
[MIRROR
|CLAMP
|REPEAT
] (图中很形象,就不解释了)
int colorStart = Color.parseColor(“#84F125”);
int colorEnd = Color.parseColor(“#5825F1”);
canvas.save();
canvas.translate(mCoo.x, mCoo.y);
mRedPaint.setStyle(Paint.Style.FILL);
mRedPaint.setShader(
new LinearGradient(
-200, 0, 200, 0,
colorStart, colorEnd,
Shader.TileMode.MIRROR
));
canvas.drawRect(-400,-200,400,-100,mRedPaint);
canvas.translate(0, 150);
mRedPaint.setShader(
new LinearGradient(
-100, 0, 100, 0,
colorStart, colorEnd,
Shader.TileMode.CLAMP
));
canvas.drawRect(-400,-200,400,-100,mRedPaint);
canvas.translate(0, 150);
mRedPaint.setShader(
new LinearGradient(
-100, 0, 100, 0,
colorStart, colorEnd,
Shader.TileMode.REPEAT
));
canvas.drawRect(-400,-200,400,-100,mRedPaint);
2).多色多点渐变:LinearGradient(渐变起点x,y,渐变终点x,y,颜色数组,位置百分点数组0~1,渐变模式)
int[] colors = new int[]{
Color.parseColor(“#F60C0C”),//红
Color.parseColor(“#F3B913”),//橙
Color.parseColor(“#E7F716”),//黄
Color.parseColor(“#3DF30B”),//绿
Color.parseColor(“#0DF6EF”),//青
Color.parseColor(“#0829FB”),//蓝
Color.parseColor(“#B709F4”),//紫
};
float[] pos = new float[]{
1.f / 7, 2.f / 7, 3.f / 7, 4.f / 7, 5.f / 7, 6.f / 7, 1
};
canvas.translate(0, 150);
mRedPaint.setShader(
new LinearGradient(
-300, 0, 300, 0,
colors, pos,
Shader.TileMode.CLAMP
));
canvas.drawRect(-400, -200, 400, -100, mRedPaint);
2.径向渐变:RadialGradient
1).两色渐变:RadialGradient(渐变中心,渐变半径,颜色1,颜色2,渐变模式)
canvas.translate(mCoo.x, mCoo.y);
int colorStart = Color.parseColor(“#84F125”);
int colorEnd = Color.parseColor(“#5825F1”);
mRedPaint.setStyle(Paint.Style.FILL);
mRedPaint.setShader(
new RadialGradient(
0,0,50,
colorStart, colorEnd,
Shader.TileMode.MIRROR
));
canvas.drawCircle(0, 0, 150, mRedPaint);
canvas.translate(350, 0);
mRedPaint.setShader(
new RadialGradient(
0,0,50,
colorStart, colorEnd,
Shader.TileMode.CLAMP
));
canvas.drawCircle(0, 0, 150, mRedPaint);
canvas.translate(350, 0);
mRedPaint.setShader(
new RadialGradient(
0,0,50,
colorStart, colorEnd,
Shader.TileMode.REPEAT
));
canvas.drawCircle(0, 0, 150, mRedPaint);
2).多色多点径向渐变:
RadialGradient(渐变中心,渐变半径,渐变模式,颜色数组,位置百分点数组0~1,渐变模式)
int[] colors = new int[]{
Color.parseColor(“#F60C0C”),//红
Color.parseColor(“#F3B913”),//橙
Color.parseColor(“#E7F716”),//黄
Color.parseColor(“#3DF30B”),//绿
Color.parseColor(“#0DF6EF”),//青
Color.parseColor(“#0829FB”),//蓝
Color.parseColor(“#B709F4”),//紫
};
float[] pos = new float[]{
1.f / 7, 2.f / 7, 3.f / 7, 4.f / 7, 5.f / 7, 6.f / 7, 1
};
mRedPaint.setStyle(Paint.Style.FILL);
mRedPaint.setShader(
new RadialGradient(
0, 0, 200,
colors, pos,
Shader.TileMode.CLAMP
));
canvas.drawCircle(0, 0, 250, mRedPaint);
3.扫描渐变:SweepGradient
这个要比上面的简单一点,没有渐变的模式 双色扫描渐变:SweepGradient(中心点x,y,颜色1,颜色2) 多色扫描渐变:SweepGradient(中心点x,y,颜色数组,位置百分点数组0~1)
int colorStart = Color.parseColor(“#84F125”);
int colorEnd = Color.parseColor(“#5825F1”);
mRedPaint.setStyle(Paint.Style.FILL);
mRedPaint.setShader(
new SweepGradient(0, 0, colorStart, colorEnd));
canvas.drawCircle(0, 0, 150, mRedPaint);
canvas.translate(400, 0);
int[] colors = new int[]{
Color.parseColor(“#F60C0C”),//红
Color.parseColor(“#F3B913”),//橙
Color.parseColor(“#E7F716”),//黄
Color.parseColor(“#3DF30B”),//绿
Color.parseColor(“#0DF6EF”),//青
Color.parseColor(“#0829FB”),//蓝
Color.parseColor(“#B709F4”),//紫
};
float[] pos = new float[]{
1.f / 7, 2.f / 7, 3.f / 7, 4.f / 7, 5.f / 7, 6.f / 7, 1
};
mRedPaint.setShader(
new SweepGradient(0, 0, colors, pos));
canvas.drawCircle(0, 0, 150, mRedPaint);
4.图片着色器:BitmapShader(图片,着色模式x,着色模式y)
用图片的所有像素点作为画笔的颜色
1).文字的图片底色:
//加载图片,生成图片着色器
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.menu_bg);
BitmapShader bs = new BitmapShader(bitmap, Shader.TileMode.CLAMP,Shader.TileMode.CLAMP);
mRedPaint.setShader(bs);
mRedPaint.setTextSize(150);
mRedPaint.setStrokeWidth(10);
mRedPaint.setStyle(Paint.Style.FILL_AND_STROKE);
canvas.drawText(“张风捷特烈”, 0, 500, mRedPaint);
2)路径+图片着色器实现裁剪图片:路径Path相关知识见上一篇:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.menu_bg);
BitmapShader bs = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
mRedPaint.setShader(bs);
mRedPaint.setStyle(Paint.Style.FILL);
Path path = CommonPath.nStarPath(8, 500, 250);
canvas.drawPath(path, mRedPaint);
还有一个ComposeShader比较复杂,以后有需求会专门写一篇
七、颜色过滤器:(Paint篇有,但本篇更加深入)
ColorFilter只有三个子类
1.LightingColorFilter(颜色1,颜色2):
Bitmap mainBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.menu_bg);
mRedPaint.setStyle(Paint.Style.FILL);
mRedPaint.setColorFilter(new LightingColorFilter(
Color.parseColor(“#F00000”),//红
Color.parseColor(“#0000ff”)//蓝
));
canvas.drawBitmap(mainBitmap, 0, 0, mRedPaint);
canvas.translate(350, 0);
mRedPaint.setColorFilter(new LightingColorFilter(
Color.parseColor(“#FF0000”),//红
Color.parseColor(“#00ff00”)//绿
));
canvas.drawBitmap(mainBitmap, 0, 0, mRedPaint);
canvas.translate(350, 0);
mRedPaint.setColorFilter(new LightingColorFilter(
Color.parseColor(“#FF0000”),//红
Color.parseColor(“#000000”)//黑
));
canvas.drawBitmap(mainBitmap, 0, 0, mRedPaint);
canvas.restore();
下面分析一下红蓝配的结果:打开LightingColorFilter源码:
/**
- Create a colorfilter that multiplies the RGB channels by one color,
- and then adds a second color. The alpha components of the mul and add
- arguments are ignored.
创建一个颜色过滤器:用mul颜色乘以RGB通道的颜色,再加上add颜色,mul和add的透明度将被忽略
*/
public LightingColorFilter(@ColorInt int mul, @ColorInt int add) {
mMul = mul;
mAdd = add;
}
//看了没什么感觉,又是native的方法,往上一看,有注释
- Given a source color RGB, the resulting R’G’B’ color is computed thusly:
- R’ = R * colorMultiply.R + colorAdd.R
- G’ = G * colorMultiply.G + colorAdd.G
- B’ = B * colorMultiply.B + colorAdd.B
这下明白了,就是颜色变换嘛----草稿纸准备好,要演算了:
注意:当相乘数大于255时,便会溢出,相当于8位容不下那么多数,后面再进来,前面的就被推出来了
这里为了区别,特意用#F00000来测试,结果有一点点偏差,毕竟两次选点的点位可能有偏差
活了这么大,第一次对颜色进行乘法和加法,对于一张图片,加上绿色就是对每个像素点进行这样的运算
2.PorterDuffColorFilter(颜色,模式–PorterDuff.Mode):
PorterDuff.Mode是不是很熟悉,看上面的叠加模式吧
Bitmap mainBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.menu_bg);
mRedPaint.setStyle(Paint.Style.FILL);
mRedPaint.setColorFilter(new PorterDuffColorFilter(
Color.parseColor(“#0000ff”), PorterDuff.Mode.DARKEN));
canvas.drawBitmap(mainBitmap, 0, 0, mRedPaint);
canvas.translate(350, 0);
mRedPaint.setColorFilter(new PorterDuffColorFilter(
Color.parseColor(“#0000ff”),PorterDuff.Mode.LIGHTEN
));
canvas.drawBitmap(mainBitmap, 0, 0, mRedPaint);
canvas.translate(350, 0);
mRedPaint.setColorFilter(new PorterDuffColorFilter(
Color.parseColor(“#0000ff”),PorterDuff.Mode.SCREEN
));
canvas.drawBitmap(mainBitmap, 0, 0, mRedPaint);
canvas.translate(350, 0);
mRedPaint.setColorFilter(new PorterDuffColorFilter(
Color.parseColor(“#0000ff”),PorterDuff.Mode.OVERLAY
));
canvas.drawBitmap(mainBitmap, 0, 0, mRedPaint);
3.ColorMatrixColorFilter(颜色变换矩阵或20个float数)
本文的重中之重便是ColorMatrix:
它是有一个5*4的矩阵对某个颜色进行运算,是不是有种众星捧月的但觉,没错,20个数,是不是很开心
1.关闭RGB颜色通道(变为黑色)
颜色ARBG占了int的四个字节,所以不可能是负数,至于如何处理负数,要看ColorMatrix的处理
测试了一下,应该是0,ARGB都没了
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!
));
canvas.drawBitmap(mainBitmap, 0, 0, mRedPaint);
3.ColorMatrixColorFilter(颜色变换矩阵或20个float数)
本文的重中之重便是ColorMatrix:
它是有一个5*4的矩阵对某个颜色进行运算,是不是有种众星捧月的但觉,没错,20个数,是不是很开心
[外链图片转存中…(img-Zp2KoSl6-1714499393618)]
1.关闭RGB颜色通道(变为黑色)
颜色ARBG占了int的四个字节,所以不可能是负数,至于如何处理负数,要看ColorMatrix的处理
测试了一下,应该是0,ARGB都没了
[外链图片转存中…(img-bQDRx47h-1714499393620)]
[外链图片转存中…(img-q1exoukl-1714499393622)]
[外链图片转存中…(img-d39xm0ac-1714499393623)]
[外链图片转存中…(img-XontMica-1714499393624)]
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!