1

这就是我的自定义可绘制对象默认的外观。

在此处输入图像描述

但是当滚动时,它会与 AppBarLayout 重叠。

在此处输入图像描述

Drawable 的代码如下所示:

@Override
public void draw(@NonNull Canvas canvas) {

    // get drawable dimensions
    Rect bounds = getBounds();

    float width = bounds.right - bounds.left;
    float height = bounds.bottom - bounds.top;
    float w2 = width / 2;
    float h2 = height / 2;
    float radius = Math.min(w2, h2) - mStrokeWidth / 2;

    mPath.reset();
    mPath.addCircle(width / 2, height / 2, radius, Path.Direction.CW);
    canvas.clipPath(mPath);

    // draw background gradient

    float barHeight = height / themeColors.length;
    mRectF.left = 0;
    mRectF.top = 0;
    mRectF.right = width;
    mRectF.bottom = height;
    for (int i = 0; i < themeColors.length; i++) {
        mPaint.setColor(themeColors[i]);

        canvas.drawRect(0, i * barHeight, width, (i + 1) * barHeight, mPaint);
    }

    mRectF.set(0, 0, width, height);
    canvas.clipRect(mRectF, Region.Op.REPLACE);

    if (mStrokeWidth != 0)
        canvas.drawCircle(width / 2, height / 2, width / 2 - mStrokeWidth / 2, mStrokePaint);

}

支持库版本:25.3.1、26.1.0

我试过的: - 剪切路径而不是替换的不同区域值 - 先剪切路径矩形,然后剪切圆形。

我该如何解决 ?

4

1 回答 1

1

我将我的解决方案发布为答案。

它被重叠的原因是因为画布被剪裁了两次而没有保存它。

我删除了这个声明:

canvas.clipRect(mRectF, Region.Op.REPLACE);

在第一次剪裁画布之前,我使用保存了它的状态

canvas.save();
canvas.clipPath(mPath);

然后当我画笔画时,我需要原来的画布,所以我恢复了它

canvas.restore();
if (mStrokeWidth != 0)
    canvas.drawCircle(width / 2, height / 2, width / 2 - mStrokeWidth / 2, mStrokePaint);

这解决了这个问题。

最终可绘制代码:

@Override
public void draw(@NonNull Canvas canvas) {

    // get drawable dimensions
    Rect bounds = getBounds();

    float width = bounds.right - bounds.left;
    float height = bounds.bottom - bounds.top;
    float w2 = width / 2;
    float h2 = height / 2;
    float radius = Math.min(w2, h2) - mStrokeWidth / 2;

    mPath.reset();
    mPath.addCircle(width / 2, height / 2, radius, Path.Direction.CW);
    canvas.save();
    canvas.clipPath(mPath);

    // draw background gradient

    float barHeight = height / themeColors.length;
    mRectF.left = 0;
    mRectF.top = 0;
    mRectF.right = width;
    mRectF.bottom = height;
    for (int i = 0; i < themeColors.length; i++) {
        mPaint.setColor(themeColors[i]);

        canvas.drawRect(0, i * barHeight, width, (i + 1) * barHeight, mPaint);
    }

    mRectF.set(0, 0, width, height);
    //canvas.clipRect(mRectF, Region.Op.REPLACE);
    canvas.restore();

    if (mStrokeWidth != 0)
        canvas.drawCircle(width / 2, height / 2, width / 2 - mStrokeWidth / 2, mStrokePaint);

}
于 2018-02-09T13:25:19.150 回答