0

I am working on a custom view in which a Circle is to be drawn around the view clicked within the screen rectangular bounds. Below is the piece of code written.

@Override
protected void onDraw(final Canvas canvas) {
    super.onDraw(canvas);
    targetPaintOuterThinCircle.setStyle(Paint.Style.STROKE);
    targetPaintOuterThinCircle.setStrokeWidth(thinOuterCircleWidth);
    targetPaintOuterThinCircle.setColor(PRIMARY_GREEN);
    targetPaintOuterThinCircle.setAntiAlias(true);

    targetOuterThinCirclePath.reset();

    targetOuterThinCirclePath.addCircle(targetX, targetY, RADIUS_SIZE_OUTER_THIN_CIRCLE, Path.Direction.CW);
    targetOuterThinCirclePath.op(screenRectPath, Path.Op.INTERSECT);

    canvas.drawPath(targetOuterThinCirclePath, targetPaintOuterThinCircle);
}

My output comes out like this. enter image description here

Expected output - don't want the intersecting line at the bottom.

I tried using the addArc method but on doing the Path.Op.Intersect operation with rectangular bounds it adds the intersect lines as well.

Abhilash Maurya
  • 302
  • 3
  • 18
  • 1
    Does this https://stackoverflow.com/questions/4028270/can-i-draw-outside-the-bounds-of-an-android-canvas answer your question? – Morph21 Apr 15 '22 at 11:27
  • @Szprota21 thanks for your comments, that link does not answered my question but got a method related to ```clipRect``` method to achieve my requirement, below adding my answer. – Abhilash Maurya Apr 18 '22 at 12:24

1 Answers1

0

I achieved my requirement by using clipPath method as below, thought it would be helpful for others like me having a similar problem:

@Override
protected void onDraw(final Canvas canvas) {
    super.onDraw(canvas);
    targetPaintOuterThinCircle.setStyle(Paint.Style.STROKE);
    targetPaintOuterThinCircle.setStrokeWidth(thinOuterCircleWidth);
    targetPaintOuterThinCircle.setColor(PRIMARY_GREEN);
    targetPaintOuterThinCircle.setAntiAlias(true);

    targetOuterThinCirclePath.reset();
    screenRectPath.reset();
    outsidePath.reset();


    //creating the path for only app visible area path
    screenRectPath.addRect(selfRect.left, selfRect.top + statusBarHeight, selfRect.right, selfRect.bottom - bottomNavBarHeight, Path.Direction.CW);
    
    //below path is the complete screen path
    fullscreenPath.addRect(selfRect.left, selfRect.top, selfRect.right, selfRect.bottom, Path.Direction.CW);
            
    //keeping only the visible area path
    outsidePath.op(screenRectPath, Path.Op.INTERSECT);
    //clipping the canvas other than screenRectPath
    canvas.clipPath(outsidePath);

    targetOuterThinCirclePath.addCircle(targetX, targetY, RADIUS_SIZE_OUTER_THIN_CIRCLE, Path.Direction.CW);
    targetOuterThinCirclePath.op(screenRectPath, Path.Op.INTERSECT);

    canvas.drawPath(targetOuterThinCirclePath, targetPaintOuterThinCircle);
}

Below is the output - enter image description here

Abhilash Maurya
  • 302
  • 3
  • 18