在画布上删除矩形顶部绘制圆圈

时间:2015-12-20 08:35:19

标签: java android canvas

我目前正在画布中绘制圆形和矩形现在我想要的是删除部分圆形笔划,使其看起来像是与矩形结合。

enter image description here

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

正如您在上面所看到的那样,圆形笔划仍然没有被切断,是否有办法切断部分笔划?

2 个答案:

答案 0 :(得分:0)

在设置方法中,添加以下代码。

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

在onDraw方法中,添加此行。

canvas.drawRect(mEraserRect, mEraserPaint);

答案 1 :(得分:0)

请参阅canvas.drawArc(...)方法

它可以让你绘制部分椭圆