我如何根据笔划帽计算从哪里开始我的线?

How can I calculate where to start my line based on the stroke cap?

我正在尝试画一条线,但我需要考虑笔划帽的宽度,以免溢出容器。

我有这样的代码...

private void initialize() {
    this.paint = new Paint();
    this.paint.setColor(Color.BLACK);
    this.paint.setStyle(Paint.Style.STROKE);
    this.paint.setStrokeCap(Paint.Cap.ROUND);
    this.paint.setStrokeWidth(1);
}

public void setStrokeWidth(float strokeWidth) {
    this.paint.setStrokeWidth(strokeWidth);
}

public void onDraw(Canvas canvas) {
    int x_start, x_end, y, cap_width;
    y = this.getHeight() / 2;
    x_start = cap_width; //Need to compensate for cap.
    x_end = this.getWidth() - cap_width; //Need to compensate for cap.

    canvas.drawLine(x_start, y, x_end, y, paint);
}

注意在上面代码的onDraw方法中,我需要计算帽的宽度。我该如何计算?

在写问题时,我意识到答案对某些人来说可能相当明显。对于那些没有清晰思考的人(我没有),这里是解决方案。

笔帽的半径始终是笔画宽度的一半。

cap_width = (int)paint.getStrokeWidth / 2;