概念

绘制出来的颜色,具体效果则由Paint确定。

1
mPaint.setStyle(Paint.Style.FILL); //设置画笔模式为填充

前使用的模式一直为填充模式,实际上画笔有三种模式,如下:

  1. STROKE //描边
  2. FILL //填充
  3. FILL_AND_STROKE //描边加填充
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    Paint paint = new Paint();
    paint.setColor(Color.BLUE);
    paint.setStrokeWidth(40); //为了实验效果明显,特地设置描边宽度非常大
    // 描边
    paint.setStyle(Paint.Style.STROKE);
    canvas.drawCircle(200,200,100,paint);
    // 填充
    paint.setStyle(Paint.Style.FILL);
    canvas.drawCircle(200,500,100,paint);
    // 描边加填充
    paint.setStyle(Paint.Style.FILL_AND_STROKE);
    canvas.drawCircle(200, 800, 100, paint);

绘制的paint的相关方法

色彩 setColor setARGB setAlpha 设置颜色,透明度
大小 setTextSize 设置文本字体大小
字体 setTypeface 设置或清除字体样式
样式 setStyle 填充(FILL),描边(STROKE),填充加描边(FILL_AND_STROKE)
对齐 setTextAlign 左对齐(LEFT),居中对齐(CENTER),右对齐(RIGHT)
测量 measureText 测量文本大小(注意,请在设置完文本各项参数后调用)

1
2
3
4
5
Paint.setStyle(Style style) 设置绘制模式
Paint.setColor(int color) 设置颜色
Paint.setStrokeWidth(float width) 设置线条宽度
Paint.setTextSize(float textSize) 设置文字大小
Paint.setAntiAlias(boolean aa) 设置抗锯齿开关

设置Shader

Android 的绘制里使用 Shader ,并不直接用 Shader 这个类,而是用它的几个子类。具体来讲有 LinearGradient RadialGradient SweepGradient BitmapShader ComposeShader 这么几个:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//渐变模式
Shader shader = new LinearGradient(100, 100, 500, 500, Color.parseColor("#E91E63"),
Color.parseColor("#2196F3"), Shader.TileMode.CLAMP);
paint.setShader(shader);
//辐射模式
Shader shader = new RadialGradient(300, 300, 200, Color.parseColor("#E91E63"),
Color.parseColor("#2196F3"), Shader.TileMode.CLAMP);
paint.setShader(shader);
...
canvas.drawCircle(300, 300, 200, paint);
centerX centerY:辐射中心的坐标
radius:辐射半径
centerColor:辐射中心的颜色
edgeColor:辐射边缘的颜色
tileMode:辐射范围之外的着色模式。
//SweepGradient 扫描模式
Shader shader = new SweepGradient(300, 300, Color.parseColor("#E91E63"),
Color.parseColor("#2196F3"));
paint.setShader(shader);
...
canvas.drawCircle(300, 300, 200, paint);
cx cy :扫描的中心
color0:扫描的起始颜色
color1:扫描的终止颜色

ColorFilter

1
2
ColorFilter lightingColorFilter = new LightingColorFilter(0xffffff, 0x003000);
paint.setColorFilter(lightingColorFilter);

setStrokeWidth(float width)

设置线条的宽度 单位像素 默认是0

setStrokeCap(Paint.Cap cap)

设置线头的形状。线头形状有三种:BUTT 平头、ROUND 圆头、SQUARE 方头。默认为 BUTT。

setStrokeJoin(Paint.Join join)

设置拐角的形状。有三个值可以选择:MITER 尖角、 BEVEL 平角和 ROUND 圆角。默认为 MITER。

setPathEffect(PathEffect effect)

1
2
3
4
PathEffect pathEffect = new DashPathEffect(new float[]{10, 5}, 10);
paint.setPathEffect(pathEffect);
...
canvas.drawCircle(300, 300, 200, paint);
  1. CornerPathEffect
    把所有拐角变成圆角。

    1
    2
    3
    4
    PathEffect pathEffect = new CornerPathEffect(20);
    paint.setPathEffect(pathEffect);
    ...
    canvas.drawPath(path, paint);
  2. DiscretePathEffect
    把线条进行随机的偏离,让轮廓变得乱七八糟。乱七八糟的方式和程度由参数决定。

    1
    2
    3
    4
    PathEffect pathEffect = new DiscretePathEffect(20, 5);
    paint.setPathEffect(pathEffect);
    ...
    canvas.drawPath(path, paint);
  3. DashPathEffect
    使用虚线来绘制线条。

    1
    2
    3
    4
    PathEffect pathEffect = new DashPathEffect(new float[]{20, 10, 5, 10}, 0);
    paint.setPathEffect(pathEffect);
    ...
    canvas.drawPath(path, paint);

setShadowLayer

setShadowLayer(float radius, float dx, float dy, int shadowColor)

1
2
paint.setShadowLayer(10, 0, 0, Color.RED);
canvas.drawText(text, 80, 300, paint);

reset()

重置 Paint 的所有属性为默认值。相当于重新 new 一个,不过性能当然高一些啦。