Question

I'm trying to draw 2 concentric circles (2nd to cut-out a hole) but they are not rendered concentric (see the image).

I'm using Canvas#drawArc method with the RectF as follows:

mCircumference = Math.min(mWidth, mHeight);
float halfCircumference = mCircumference / 2;
float halfWidth = mWidth / 2;
float halfHeight = mHeight / 2;
mOuterCircleRect = new RectF(halfWidth - halfCircumference, halfHeight - halfCircumference, mCircumference, mCircumference);
mInnerCircumference = mCircumference - 2 * mThickness;
float halfInnerCircumference = mInnerCircumference / 2;
mInnerCircleRect = new RectF(halfWidth - halfInnerCircumference, halfHeight - halfInnerCircumference, mInnerCircumference, mInnerCircumference);

the logs shows as:

circumference=36
innerCircumference=32
mOuterCircleRect=RectF(0.0, 0.0, 36.0, 36.0)
mInnerCircleRect=RectF(2.0, 2.0, 32.0, 32.0)

which looks OK to me but result is not fine:

2 concentric circles creating ring shape, middle circle is off-center

the draw routines:

//debug
canvas.drawColor(0xFFFF0000);

//background
mOverlayCanvas.drawArc(mOuterCircleRect, 360, 360, false,
        mPaintBackground);
if(mInnerCircumference > 0)
{
    mOverlayCanvas.drawArc(mInnerCircleRect, 360, 360, false, mPaintEraser);
}

//draw on canvas
canvas.drawBitmap(mOverlay, 0, 0, null);

and "paints"

if (mPaintBackground == null)
    mPaintBackground = new Paint();
mPaintBackground.setColor(mBackgroundColor);
mPaintBackground.setAntiAlias(true);
mPaintBackground.setStyle(Style.FILL);

Log.i(TAG, "background color=" + Integer.toHexString(mBackgroundColor));

if (mPaintEraser == null)
    mPaintEraser = new Paint();
mPaintEraser.setColor(Color.TRANSPARENT);
mPaintEraser.setAlpha(0);
mPaintEraser.setAntiAlias(true);
//      mPaintEraser.setStyle(Style.FILL);
mPaintEraser.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

What am I doing wrong here?

Was it helpful?

Solution

Inner rectangle is not centered within outer rectangle. Left/top distance is 2.0, but right/bottom distance is 4.0. Variable mInnerCircleRect should be defined as:

mInnerCircleRect = RectF(2.0, 2.0, 34.0, 34.0)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top