【问题标题】:Android - how to properly draw an arrow with Path?Android - 如何用路径正确绘制箭头?
【发布时间】:2017-04-08 11:59:15
【问题描述】:

我正在尝试画一个箭头,但我得到了一个非常奇怪的结果。 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();

【问题讨论】:

    标签: android path ondraw


    【解决方案1】:

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

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

    leftArrowPaint.setStrokeJoin(Paint.Join.BEVEL);
    

    你也可以使用Paint.Join.ROUND,这取决于你觉得哪个更好。

    【讨论】:

    • 迟到总比没有好,你只是让自己免于搜索几个小时,因为我无法找出为什么会发生同样的事情。你应该得到一颗星 :-)
    猜你喜欢
    • 2011-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    • 2017-08-30
    • 1970-01-01
    • 2018-01-14
    • 1970-01-01
    相关资源
    最近更新 更多