【问题标题】:drawing circle on top of rectangle with delete in canvas在画布中删除矩形顶部画圆
【发布时间】:2015-12-20 08:35:18
【问题描述】:

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

代码:

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

如上图所示圆形笔划仍未被截断,有没有办法截掉部分笔划?

【问题讨论】:

    标签: java android canvas


    【解决方案1】:

    canvas.drawArc(...)方法

    它可以让你绘制椭圆的一部分

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2016-11-11
        • 1970-01-01
        • 2011-03-25
        • 1970-01-01
        • 2013-05-28
        • 2019-03-07
        • 2020-04-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多