在弧的起点和终点上绘制圆

时间:2016-06-03 06:17:43

标签: android view draw android-custom-view ondraw

我很难在弧的两端(开始和结束)绘制点

虽然我可以在画布上绘制弧线。下面是我绘制弧线的示例代码。

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    float width = (float) getWidth();
    float height = (float) getHeight();
    float radius;

    if (width > height) {
        radius = height / 4;
    } else {
        radius = width / 4;
    }

    float center_x, center_y;
    final RectF oval = new RectF();

    center_x = width / 2;
    center_y = height / 2;

    oval.set(center_x - radius,
            center_y - radius,
            center_x + radius,
            center_y + radius);

    float percent = 25;
    float arcRadius = 360;
    float angle = arcRadius * (percent/100);

    canvas.drawArc(oval, 270, 360, false, trackpaint);
    canvas.drawArc(oval, 270, angle, false, arcPaint);  
}

唯一缺失的是在圆弧的起点和终点上放置圆圈。我已经尝试过此链接,但它确实有效Calculate Arc Center Point, Knowing It's Start and End Degrees。任何帮助都感激不尽。谢谢

1 个答案:

答案 0 :(得分:10)

起点的坐标为:

double startX = Math.cos(Math.toRadians(270)) * radius + center_x;

double startY = Math.sin(Math.toRadians(270)) * radius + center_y;

终点的坐标为:

double endX = Math.cos(Math.toRadians(270 + angle)) * radius + center_x;

double endY = Math.sin(Math.toRadians(270 + angle)) * radius + center_y;

然后您可以使用起点和终点绘制圆圈:

canvas.drawCircle(startX, startY, 10, paint);

canvas.drawCircle(endX, endY, 10, paint);