0
votes

I am currently drawing circle and rectangle in canvas now what I want is to remove part of the circle stroke so it would look like it is combined with the rectangle.

enter image description here

code:

public void setup() {
    mLinePaint = new Paint();
    mLinePaint.setAntiAlias(true);
    mLinePaint.setStyle(Paint.Style.STROKE);
    mLinePaint.setColor(ContextCompat.getColor(getContext(), R.color.white));

    mCirclePaint = new Paint();
    mCirclePaint.setAntiAlias(true);
    mCirclePaint.setStyle(Paint.Style.FILL_AND_STROKE);
    mCirclePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    mCirclePaint.setColor(ContextCompat.getColor(getContext(), R.color.white));

    mLineRect = new Rect(100, 0, 200, 300);

    mBackgroundColor = ContextCompat.getColor(getContext(), R.color.colorPrimaryDark);
}

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

    //the background color of the canvas
    canvas.drawColor(mBackgroundColor);

    //the rectangle line in the background only border
    canvas.drawRect(mLineRect, mLinePaint);

    canvas.drawCircle(viewWidth / 2, viewHeight / 2, LINE_HEIGTH * 2, mCirclePaint);
}

As you can see above the circle stroke is still not cut off, is there a way to cut off part of the stroke?

2

2 Answers

0
votes

see canvas.drawArc(...) method

It enable you to draw portion of oval

0
votes

In setup method, add following code.

int w = (int) mLinePaint.getStrokeWidth();
mEraserRect = new Rect(mLineRect.left-w, mLineRect.top-w, mLineRect.right+w, mLineRect.bottom+w);
mEraserPaint = new Paint();
mEraserPaint.setColor(mBackgroundColor);
mEraserPaint.setStyle(Paint.Style.FILL);

In onDraw method, add this line.

canvas.drawRect(mEraserRect, mEraserPaint);