Search code examples
androidviewdrawandroid-custom-viewondraw

Draw circle on start and end point of an Arc


Hi im having difficulties on drawing dots on arc's both ends (start and end)

Although I can draw arc on canvas. Heres my sample code for drawing arc.

@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);  
}

the only missing is putting circles on start and end points of the arc. I've tried this link but it doest work Calculate Arc Center Point, Knowing It's Start and End Degrees. Any help will be much appreciated. Thank you


Solution

  • the coordinate of the start point is:

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

    the coordinate of the end point is:

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

    and then you can draw circle using the start point and end point:

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