偏心圆偏离中心

时间:2014-02-03 09:43:21

标签: android android-canvas

我正在尝试绘制2个同心圆(第2个切出一个洞),但它们没有呈现为同心圆(见图)。

我正在使用Canvas#drawArc方法和RectF,如下所示:

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

日志显示为:

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

对我来说看起来不错,但结果并不理想:

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

绘制例程:

//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);

和“paint”

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

我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

内部矩形不在外部矩形内居中。左/上距离为2.0,但右/下距离为4.0。变量mInnerCircleRect应定义为:

mInnerCircleRect = RectF(2.0, 2.0, 34.0, 34.0)