setColor(int color);
setARGB(int a, int r, int g, int b);
复制代码
setShader(Shader shader);
复制代码
注:设置后,setColor/ARGB() 方法无效。canvas
// x0 y0 x1 y1:渐变的两个端点的位置
// color0 color1 是端点的颜色
// tile:端点范围以外的着色规则
LinearGradient(float x0, float y0, float x1, float y1, int color0, int color1, Shader.TileMode tile);
复制代码
// centerX centerY:辐射中心的坐标
// radius:辐射半径
// edgeColor:辐射边缘的颜色
// tileMode:辐射范围以外的着色模式。
RadialGradient(float centerX, float centerY, float radius, int centerColor, int edgeColor, TileMode tileMode);
复制代码
// cx cy :扫描的中心
// color0:扫描的起始颜色
// color1:扫描的终止颜色
SweepGradient(float cx, float cy, int color0, int color1);
复制代码
着色规则:数组
// bitmap:用来作模板的 Bitmap 对象
// tileX:横向的 TileMode
// tileY:纵向的 TileMode。
BitmapShader(Bitmap bitmap, Shader.TileMode tileX, Shader.TileMode tileY);
复制代码
// shaderA, shaderB:两个相继使用的 Shader
// mode: 两个 Shader 的叠加模式,即 shaderA 和 shaderB 应该怎样共同绘制。
ComposeShader(Shader shaderA, Shader shaderB, PorterDuff.Mode mode);
复制代码
例:bash
// 第二个 Shader:从上到下的线性渐变(由透明到黑色)
Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.batman_logo);
Shader shader2 = new BitmapShader(bitmap2, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
// ComposeShader:结合两个 Shader
Shader shader = new ComposeShader(shader1, shader2, PorterDuff.Mode.SRC_OVER);
paint.setShader(shader);
...
canvas.drawCircle(300, 300, 300, paint);
复制代码
源图像和目标图像 post
// colorFilter颜色过滤类
setColorFilter(ColorFilter colorFilter);
复制代码
// mul 用来和目标像素相乘
// add 用来和目标像素相加
LightingColorFilter(int mul, int add);
复制代码
PorterDuffColorFilter(int color, PorterDuff.Mode mode);
复制代码
复制代码
可以使用 setSaturation(float sat);设置饱和度。ui
指的是你要绘制的内容和 Canvas 的目标位置的内容应该怎样结合计算出最终的颜色。但通俗地说,其实就是要你以绘制的内容做为源图像,以 View 中已有的内容做为目标图像,选取一个 PorterDuff.Mode 做为绘制内容的颜色处理方案。spa
例:3d
Xfermode xfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
...
canvas.drawBitmap(rectBitmap, 0, 0, paint); // 画方
paint.setXfermode(xfermode); // 设置 Xfermode
canvas.drawBitmap(circleBitmap, 0, 0, paint); // 画圆
paint.setXfermode(null); // 用完及时清除 Xfermode
复制代码
使用注意rest
Canvas.saveLayer();code
int saved = canvas.saveLayer(null, null, Canvas.ALL_SAVE_FLAG);
canvas.drawBitmap(rectBitmap, 0, 0, paint); // 画方
paint.setXfermode(xfermode); // 设置 Xfermode
canvas.drawBitmap(circleBitmap, 0, 0, paint); // 画圆
paint.setXfermode(null); // 用完及时清除 Xfermode
canvas.restoreToCount(saved);
复制代码
View.setLayerType()cdn
复制代码
// FILL 是填充模式,
// STROKE 是画线模式(即勾边模式)
// FILL_AND_STROKE 是两种模式一并使用:既画线又填充。
// 它的默认值是 FILL,填充模式。
setStyle(Paint.Style style);
复制代码
// width 宽度,单位像素
setStrokeWidth(float width);
复制代码
// BUTT 平头、ROUND 圆头、SQUARE 方头
setStrokeCap(Paint.Cap cap);
复制代码
// MITER 尖角、 BEVEL 平角和 ROUND 圆角
settrokeJoin(Paint.Join join);
复制代码
对 setStrokeJoin() 的补充,设置 MITER 拐角的延长线的最大值。 线条在 Join 类型为 MITER 时对于 MITER 的长度限制。
setStrokeMiter(float miter);
复制代码
setAntiAlias(boolean aa);
//或在new Paint()的时添加 ANTI_ALIAS_FLAG参数;
复制代码
图像从较高色彩深度(便可用的颜色数)向较低色彩深度的区域绘制时,在图像中有意地插入噪点,经过有规律地扰乱图像来让图像对于肉眼更加真实的作法。
setDither(boolean dither);
复制代码
setFilterBitmap(boolean filter);
复制代码
// effect 轮廓效果
setPathEffect(PathEffect effect);
复制代码
// radius 是圆角的半径
CornerPathEffect(float radius);
复制代码
例
PathEffect pathEffect = new CornerPathEffect(20);
paint.setPathEffect(pathEffect);
canvas.drawPath(path, paint);
复制代码
// segmentLength 是用来拼接的每一个线段的长度
// deviation 是偏离量
DiscretePathEffect(float segmentLength, float deviation);
复制代码
例
PathEffect pathEffect = new DiscretePathEffect(20, 5);
paint.setPathEffect(pathEffect);
canvas.drawPath(path, paint);
复制代码
// ntervals 指定了虚线的格式:数组中元素必须为偶数(最少是 2 个)
// phase 虚线的偏移量
DashPathEffect(float[] intervals, float phase);
复制代码
例
PathEffect pathEffect = new DashPathEffect(new float[]{20, 10, 5, 10}, 0);
paint.setPathEffect(pathEffect);
canvas.drawPath(path, paint);
复制代码
// shape 参数是用来绘制的 Path
// advance 是两个相邻的 shape 段之间的间隔,不过注意,这个间隔是两个 shape 段的起点的间隔,而不是前一个的终点和后一个的起点的距离;
// phase 虚线的偏移
// style 指定拐弯改变的时候 shape 的转换方式
PathDashPathEffect(Path shape, float advance, float phase, PathDashPathEffect.Style style);
复制代码
style 类型
PathEffect dashEffect = new DashPathEffect(new float[]{20, 10}, 0);
PathEffect discreteEffect = new DiscretePathEffect(20, 5);
pathEffect = new SumPathEffect(dashEffect, discreteEffect);
canvas.drawPath(path, paint);
复制代码
// innerpe 是先应用的
// outerpe 是后应用的
ComposePathEffect(PathEffect outerpe, PathEffect innerpe);
复制代码
PathEffect dashEffect = new DashPathEffect(new float[]{20, 10}, 0);
PathEffect discreteEffect = new DiscretePathEffect(20, 5);
pathEffect = new ComposePathEffect(dashEffect, discreteEffect);
...
canvas.drawPath(path, paint);
复制代码
// radius 是阴影的模糊范围;
// dx dy 是阴影的偏移量;
// shadowColor 是阴影的颜色。
setShadowLayer(float radius, float dx, float dy, int shadowColor)
复制代码
清除阴影层,使用 clearShadowLayer();
setMaskFilter(MaskFilter maskfilter);
复制代码
// radius 模糊的范围,
// style 是模糊的类型
BlurMaskFilter(float radius, BlurMaskFilter.Blur style);
复制代码
模糊类型
// direction 3 个元素的数组,指定了光源的方向;
// ambient 环境光的强度,数值范围是 0 到 1;
// specular 炫光的系数;
// blurRadius 应用光线的范围
EmbossMaskFilter(float[] direction, float ambient, float specular, float blurRadius);
复制代码
例:
paint.setMaskFilter(new EmbossMaskFilter(new float[]{0, 1, 1}, 0.2f, 8, 10));
...
canvas.drawBitmap(bitmap, 100, 100, paint);
复制代码
// src 是原 Path
// dst 实际 Path 的保存位置
getFillPath(Path src, Path dst);
复制代码
reset();
复制代码
set(Paint src);
复制代码
setFlags(int flags);
复制代码