自定义笔触以具有边框和填充

时间:2018-12-16 12:06:31

标签: java android

如何绘制带有填充颜色和(不同颜色)边框的笔划?

例如我想要这样的东西:

enter image description here

我尝试创建2种绘画-一种是描边样式,另一种是填充样式,但是调用

strokePaint = new Paint();
strokePaint.setStyle(Paint.Style.STROKE);
strokePaint.setColor(Color.parseColor("#A3A3A3"));
fillPaint = new Paint();
fillPaint.setStyle(Paint.Style.FILL);
fillPaint.setColor(Color.WHITE);

canvas.drawPath(totalPath, strokePaint);
canvas.drawPath(totalPath, fillPaint);

不会产生预期的效果,并且看起来很糟糕。

有可能吗?

1 个答案:

答案 0 :(得分:0)

弄清楚了。诀窍是绘制两次,一次绘制为厚1-2像素的背景层,然后绘制为前景层。

即:

strokePaintBackground = new Paint(Paint.ANTI_ALIAS_FLAG);
strokePaintBackground.setStyle(Paint.Style.STROKE);
strokePaintBackground.setColor(Color.BLACK);
strokePaintBackground.setStrokeWidth(8);
strokePaintBackground.setPathEffect(new DashPathEffect(new float[]{30, 15}, 0));
strokePaintBackground.setStrokeCap(Paint.Cap.ROUND);
strokePaintBackground.setStrokeJoin(Paint.Join.ROUND);

strokePaintForground = new Paint(Paint.ANTI_ALIAS_FLAG);
strokePaintForground.setStyle(Paint.Style.STROKE);
strokePaintForground.setColor(Color.WHITE);
strokePaintForground.setStrokeWidth(6);
strokePaintForground.setPathEffect(new DashPathEffect(new float[]{30, 15}, 0));
strokePaintForground.setStrokeCap(Paint.Cap.ROUND);
strokePaintForground.setStrokeJoin(Paint.Join.ROUND);

canvas.drawPath(totalPath, strokePaintBackground);
canvas.drawPath(totalPath, strokePaintForground);