首页 > 解决方案 > 自定义笔触以具有边框和填充

问题描述

如何使用填充颜色和(不同颜色)边框绘制笔触?

例如我想要这样的东西:

在此处输入图像描述

我尝试创建 2 种油漆 - 一种具有 Stroke 风格,一种具有 Fill 风格,但调用

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);

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

甚至可能吗?

标签: javaandroid

解决方案


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

IE :

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);

推荐阅读