Android - 如何正确绘制带有路径的箭头?

标签 android path ondraw

我正在尝试绘制箭头,但得到了一个非常奇怪的结果。 This is how it looks like 问题很明显——重叠部分。

int radius = 100;  //Radius of blue circle to the right
Path leftArrow = new Path();
Paint leftArrowPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

leftArrowPaint.setStyle(Paint.Style.STROKE);
leftArrowPaint.setColor(ContextCompat.getColor(getContext(), R.color.buttonText));
leftArrowPaint.setAlpha(80);
leftArrowPaint.setStrokeWidth(8);

在 onDraw 方法中:

//Start point
leftArrow.moveTo(touchX-(radius+5),  (int)touchY);
//Line to left
leftArrow.lineTo(touchX-(radius+60), (int)touchY);
//Line up
leftArrow.lineTo(touchX-(radius+30), (int)touchY-30);
//Move back to the middle
leftArrow.moveTo(touchX-(radius+60), (int)touchY);
//Line down
leftArrow.lineTo(touchX-(radius+30), (int)touchY+30);
canvas.drawPath(leftArrow, leftArrowPaint);
leftArrow.reset();

最佳答案

好吧,我知道这对你来说已经太晚了,但无论如何我都会回答,以防有人遇到同样的问题。

您需要指定 Paint 的 Join 属性。 https://developer.android.com/reference/android/graphics/Paint.Join.html

leftArrowPaint.setStrokeJoin(Paint.Join.BEVEL);

您还可以使用Paint.Join.ROUND,具体取决于哪种方式更适合您。

关于Android - 如何正确绘制带有路径的箭头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43293820/

相关文章:

java - 如何从 Android 中每个动态创建的 EditText 获取数据?

Android 翻译文本未显示在应用程序上

url - 如何摆脱 URI 或 URL 中的起始斜杠?

android - 在应用计费 v3 中 - 为什么我在调用 queryPurchases(skuID) 时收到 DEVELOPER_ERROR?

android - 为什么大多数人在retrofit中使用拦截器添加认证头?

ruby-on-rails - 无法使用 rvm 安装从命令行运行 rails

Linux 开发 |程序文件的良好实践

c++ - 如何清除ActiveX OCX控件的绘图区?

android - 如何避免在onDraw上花费很长时间?

android - 从 onDraw() 调用 invalidate() 会导致堆栈溢出吗?