Android:OvalShape Drawable裁剪边缘

时间:2015-11-17 06:29:37

标签: android android-shapedrawable

我正在尝试创建一个带边框的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);
    }
}

但是,如图所示,绘制的边框会在边缘处被剪裁。此外,随着drawable的尺寸减小,边缘会被剪裁得更多。

Clipped Edges

我应该在代码中做些什么修改来实现完美的圆圈?

0 个答案:

没有答案