Android - 如何正确绘制一个带箭头的箭头?

问题描述:

我想绘制一个箭头,但我得到了一个非常奇怪的结果。 This is how it looks like问题很明显 - 重叠部分。Android - 如何正确绘制一个带箭头的箭头?

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

好吧,我知道一切都太迟了你,但我会回答反正万一有人遇到同样的问题。

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

leftArrowPaint.setStrokeJoin(Paint.Join.BEVEL); 

您还可以使用Paint.Join.ROUND,这取决于什么看起来对你更好。