1

我正在尝试创建一个OvalShape Drawable带边框的。为了给它一个提升的效果,我在边界周围添加了阴影。

相关代码:

class Badge extends ShapeDrawable{

void Badge(){
    borderPaint = new Paint();
    borderPaint.setColor(borderColor);
    borderPaint.setStyle(Paint.Style.STROKE);
    borderPaint.setStrokeWidth(borderThickness);
    borderPaint.setAntiAlias(true);
    borderPaint.setShadowLayer(4.0f, 0.0f, 2.0f, Color.BLACK);
}

@Override
public void draw(Canvas canvas) {
    super.draw(canvas);
    Rect r = getBounds();


    // draw border if needed
    if (borderThickness > 0) {
        drawBorder(canvas);
    }

    int count = canvas.save();
    canvas.translate(r.left, r.top);

    // draw text inside badge
    int width = this.width < 0 ? r.width() : this.width;
    int height = this.height < 0 ? r.height() : this.height;
    int fontSize = this.fontSize < 0 ? (Math.min(width, height) / 2) : this.fontSize;
    textPaint.setTextSize(fontSize);
    Rect textBounds = new Rect();
    textPaint.getTextBounds(text, 0, text.length(), textBounds);
    canvas.drawText(text, width / 2, height / 2 - textBounds.exactCenterY(), textPaint);

    canvas.restoreToCount(count);

}

private void drawBorder(Canvas canvas) {
    RectF rect = new RectF(getBounds());
    rect.inset(borderThickness / 2, borderThickness / 2);
    canvas.drawOval(rect, borderPaint);
    }
}

但是,正如您在图像中看到的那样,绘制的边框在边缘处被剪裁。此外,随着可绘制对象的大小减小,边缘会被更多地剪裁。

剪裁边缘

我应该在代码中进行哪些修改以实现完美的循环?

4

0 回答 0