1.七种路径效果
1.1 效果图
1.2 具体方法
-
PathEffect();
默认效果,就是直线
-
CornerPathEffect(radius);
将各线段之间的夹角变成圆角,radius是圆角的半径
-
new DashPathEffect(new float[]{10f, 5f, 20f, 15f},10);绘制虚线,intervals最少为2个,phase是偏移量 依次绘制为:实线10f + 虚线5f + 实线20f + 虚线10f
-
new PathDashPathEffect(p, 60, 0, PathDashPathEffect.Style.ROTATE);
和DashPathEffect差不多,只是能自定义图形来绘制Path。 shape是绘制的图形、adwance是图形之间的间距、phase是偏移量、样式
-
new SumPathEffect(pathEffects[1], pathEffects[2]);
将两种效果分别展示再重合
-
new DiscretePathEffect(5f, 10f);
切断线段,segmentLength是指切断线段的长度。deviation是偏移量,随机,小于等于deviation
-
new ComposePathEffect(pathEffects[3], pathEffects[5]);
将多个效果组合起来
1.3 Demo代码
private void initPaint() {
//字体画笔
textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
textPaint.setColor(Color.WHITE);
textPaint.setTextSize(40f);
//线画笔
linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
linePaint.setColor(Color.RED);
linePaint.setStrokeWidth(5f); //设置线宽
linePaint.setStyle(Paint.Style.STROKE); //设置为空心
//设置路径
path = new Path();
path.moveTo(0,0); //设置起点
for (int i = 0; i < 37; i++) { //设置连接的点
path.lineTo(i*30,(float) (Math.random() * 100));
}
//初始化路径效果 - 共有七种
pathEffects = new PathEffect[7];
pathEffects[0] = new PathEffect(); //1.默认效果
pathEffects[1] = new CornerPathEffect(20f);
pathEffects[2] = new DashPathEffect(new float[]{10f, 5f, 20f, 15f},10);
Path p = new Path();
p.addRect(0, 0, 30, 5, Path.Direction.CCW);
pathEffects[3] = new PathDashPathEffect(p, 60,0,PathDashPathEffect.Style.ROTATE);
pathEffects[4] = new SumPathEffect(pathEffects[1], pathEffects[2]);
pathEffects[5] = new DiscretePathEffect(5f, 10f);
pathEffects[6] = new ComposePathEffect(pathEffects[3], pathEffects[5]);
setBackgroundColor(Color.BLUE);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//绘制路径
for (int i = 0; i < pathEffects.length; i++) {
linePaint.setPathEffect(pathEffects[i]); //设置路径效果
canvas.drawPath(path,linePaint);
canvas.drawText(pathEffectName[i],0,140,textPaint);
//每绘制一条将画布向下平移180像素
canvas.translate(0,150);
}
invalidate(); //绘制刷新
}