UI绘制-Paint(二)颜色相关方法
2019-06-12 本文已影响0人
tingtingtina
系列文章传送门:
UI绘制-Paint(一)Paint基本属性及方法
UI绘制-Paint(二)颜色相关方法
UI绘制-Paint(三)图层混合模式
UI绘制-Paint(四)颜色过滤器 ColorFilter
本文会介绍Paint与颜色相关的方法,着色器(Shader)的类型及使用。
Paint 颜色相关方法
- 相关 API
Method | Description |
---|---|
setColor() | 设置画笔颜色 |
setARGB() | 通过ARGB设置画笔颜色 |
setShader() | 设置着色器 |
setColorFilter() | 设置颜色过滤器 |
/**
* @param color 参数具体的颜色值,16进制数值,0xFFFF0000
*/
public void setColor(@ColorInt int color)
/**
* @param a The new alpha component (0..255) of the paint's color.
* @param r The new red component (0..255) of the paint's color.
* @param g The new green component (0..255) of the paint's color.
* @param b The new blue component (0..255) of the paint's color.
*/
public void setARGB(int a, int r, int g, int b)
/**
* Set or clear the shader object.
* @param shader 着色器对象
* @return shader
*/
public Shader setShader(Shader shader)
/**
* Set or clear the paint's colorfilter, returning the parameter.
* @param filter May be null. The new filter to be installed in the paint
* @return filter
*/
public ColorFilter setColorFilter(ColorFilter filter)
Shader
Shader(着色器),用于绘制颜色的。它是图形领域里一个通用的概念,它和设置颜色的区别是,着色器设置的是一个颜色方案,或者说是一套着色规则。当设置了 Shader 之后,Paint 在绘制图形和文字时就不使用 setColor/ARGB() 设置的颜色了,而是使用 Shader 的方案中的颜色。
Shader有几个子类: LinearGradient RadialGradient SweepGradient BitmapShader ComposeShader
LinearGradinet 线性渲染
/**
* 线性渲染
*
* @param x0 (x0, y0)渐变起始坐标的x0
* @param y0 (x0, y0)渐变起始坐标的y0
* @param x1 (x1, y1)渐变结束坐标的x1
* @param y1 (x1, y1)渐变结束坐标的y1
* @param color0 渐变开始点颜色,16进制的颜色表示,必须有带有透明度
* @param color1 渐变结束颜色
* @param colors 渐变数组
* @param positions 位置数组,position的取值范围[0,1],作用是指定摸个位置的颜色值,如果传null,渐变就线性变化
* @param tile 用于置顶控件区域大于置顶的渐变区域时,空白区域的颜色填充方式
*/
public LinearGradient(float x0, float y0, float x1, float y1, int colors[], @Nullable float positions[], @NonNull TileMode tile)
public LinearGradient(float x0, float y0, float x1, float y1, int color0, int color1, @NonNull TileMode tile) {
Shader.TileMode 平铺模式
- CLAMP 绘制区域超过渲染区域的部分,会以最后一个像素拉伸排版
- REPEAT 绘制区域超过渲染区域的部分,重复排版
- MIRROR 绘制区域超过渲染区域的部分,镜像翻转排版
mShader = new LinearGradient(0, 0, 500, 500, new int[]{Color.RED, Color.BLUE}, null, Shader.TileMode.MIRROR);
mPaint.setShader(mShader);
canvas.drawRect(0, 0, 1000, 1000, mPaint);

RadialGradinet 环形渲染 (辐射渲染)
/**
* 环形渲染
*
* @param centerX 辐射中心(开始渐变)的x坐标
* @param centerY 辐射中心(开始渐变)的y坐标
* @param radius 渐变的半径
* @param centerColor 中心点渐变颜色
* @param edgeColor 边界的渐变颜色
* @param colors 渐变颜色数组
* @param stops 渐变位置数组,类似扫描渐变的position数组,取值[0, 1],中心点为0,半径到达位置为1.0f
* @param tileMode 未覆盖以外的填充模式
*/
public RadialGradient(float centerX, float centerY, float radius, @NonNull @ColorInt int colors[], @Nullable float stops[], @NonNull TileMode tileMode)
public RadialGradient(float centerX, float centerY, float radius, @ColorInt int centerColor, @ColorInt int edgeColor, @NonNull TileMode tileMode) {
SweepGradient 扫描渲染
/**
* 扫描渲染
*
* @param cx 渐变中心坐标-x
* @param cy 渐变中心坐标-y
* @param color0 渐变开始颜色
* @param color1 渐变结束颜色
* @param colors 渐变数组
* @param positions 位置数组,position的取值范围[0,1],作用是指定摸个位置的颜色值,如果传null,渐变就线性变化
*/
public SweepGradient(float cx, float cy, @NonNull @ColorInt int colors[], @Nullable float positions[]) {
public SweepGradient(float cx, float cy, @ColorInt int color0, @ColorInt int color1)

BitmapShader 位图渲染
用 Bitmap 来着色,就是用 Bitmap 的像素来作为图形或文字的填充。
/**
* 位图渲染
*
* @param bitmap 构造Shader使用的bitmap
* @param tileX X轴方向的TileMode
* @param tileY Y轴方向的TileMode
*/
public BitmapShader(@NonNull Bitmap bitmap, @NonNull TileMode tileX, @NonNull TileMode tileY)
如:
BitmapShader bitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
mPaint.setShader(bitmapShader);
canvas.drawCircle(400, 400, 400, mPaint);


ComposeShader 组合渲染(两种)
可以将两种渲染方式进行组合,如LinearGradient + BitmapShader
/**
* Create a new compose shader, given shaders A, B, and a combining mode.
* When the mode is applied, it will be given the result from shader A as its
* "dst", and the result from shader B as its "src".
*
* @param shaderA The colors from this shader are seen as the "dst" by the mode
* @param shaderB The colors from this shader are seen as the "src" by the mode
* @param mode 组合两种shader颜色的模式
*/
public ComposeShader(@NonNull Shader shaderA, @NonNull Shader shaderB, @NonNull Xfermode mode)
public ComposeShader(@NonNull Shader shaderA, @NonNull Shader shaderB, @NonNull PorterDuff.Mode mode)
BitmapShader bitmapShader = new BitmapShader(mBitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
LinearGradient linearGradient = new LinearGradient(0, 0, 500, 500, new int[]{Color.RED, Color.GREEN}, null, Shader.TileMode.CLAMP);
mShader = new ComposeShader(linearGradient, bitmapShader, PorterDuff.Mode.MULTIPLY);
mPaint.setShader(mShader);
canvas.drawRect(0, 0, 500, 500, mPaint);
