如何在Android中使用ShapeDrawable编程创建具有圆角边框?

20

我需要通过扩展ShapeDrawable来编写程序,创建带有圆角的边框。我需要一个黑色带有圆角的边框,边框外部的像素为白色,内部像素为透明。目前我的代码存在多个问题,其中之一是它不能创建与边框相同厚度的平滑圆角,另一个问题是边框的外部像素是透明而不是白色。

这里是我目前得到的圆角图片:corner

以下是我正在构造函数中使用Color.TRANSPARENT作为'fill'参数的代码:

public class CustomShape extends ShapeDrawable {
 private final Paint fillpaint, strokepaint;
public CustomShape(int fill, int strokeWidth,int radius) {

    super(new RoundRectShape(new float[] { radius, radius, radius, radius, radius, radius, radius, radius }, null, null));
    fillpaint = new Paint(this.getPaint());
    fillpaint.setColor(fill);
    strokepaint = new Paint(fillpaint);
    strokepaint.setStyle(Paint.Style.STROKE);
    strokepaint.setStrokeWidth(strokeWidth);
    strokepaint.setColor(Color.BLACK);
}



@Override
protected void onDraw(Shape shape, Canvas canvas, Paint paint) {
    shape.draw(canvas, fillpaint);
    shape.draw(canvas, strokepaint);
}

}


尝试扩展Drawable(并进行自定义绘制),而不是ShapeDrawable? - pskink
6个回答

30

如果您需要均匀的圆角(从您的示例中看来确实如此),您可以使用纯色的GradientDrawable

GradientDrawable gd = new GradientDrawable();
gd.setColor(Color.RED);
gd.setCornerRadius(10);
gd.setStroke(2, Color.WHITE);

view.setBackground(gd);

GradientDrawable的文档可以在这里找到。

编辑:对于每个角分别处理

您可以使用setCornerRadii(float[] radii)方法分别指定每个角的半径。 "对于每个角,该数组包含2个值,[X_radius,Y_radius]。 角按左上,右上,右下,左下的顺序排列。仅当形状为矩形类型时才使用此属性。

建议在更改此属性之前调用mutate()


有没有可能指定左侧圆角半径和右侧圆角半径? - Chandru
此外,还添加了一条注释,说明如何分别将每个角落进行圆角处理。 - SGal

1

使用ShapeDrawableRoundRectShape代替GradientDrawable更好,这里有一个示例:

// individualRoundedCorners
val roundCorners = floatArrayOf(
    topLeftRadius, topLeftRadius,
    topRightRadius, topRightRadius,
    bottomRightRadius, bottomRightRadius,
    bottomLeftRadius, bottomLeftRadius
)

或者

// similarCornerRadius
val roundCorners = FloatArray(8) { cornerRadius }

然后是图形本身

val shapeDrawable = ShapeDrawable().apply {
        shape = RoundRectShape(roundCorners, null, null)
        paint.color = Color.RED
    }

更多关于RoundRectShape的信息。

0

除了指定圆角尺寸之外,您可以使用GradientDrawable和setCornerRadii()方法。

GradientDrawable d = new GradientDrawable();
d.setCornerRadii({5.0f,5.0f,5.0f,5.0f});
textViewExample.setBackgroundResource(d);

0
GradientDrawable drawable = (GradientDrawable)image.getBackground();
drawable.setGradientRadius(radiuspx);

0
你可以实现一个自定义的可绘制对象。以下是xml的示例。
<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="#ffffffff"/>    

    <stroke android:width="3dp"
            android:color="#ff000000"
            />

    <padding android:left="1dp"
             android:top="1dp"
             android:right="1dp"
             android:bottom="1dp"
             /> 

    <corners android:bottomRightRadius="7dp" android:bottomLeftRadius="7dp" 
     android:topLeftRadius="7dp" android:topRightRadius="7dp"/> 
</shape>

将此xml保存在项目的drawable文件夹中。现在可以像普通的drawable一样用于任何小部件。例如:android:background="R.drawable.round_shape"

此示例是参考以下链接link


0
 setCornerRadii(new float[] {
                    topLeftRadius, topLeftRadius,
                    topRightRadius, topRightRadius,
                    bottomRightRadius, bottomRightRadius,
                    bottomLeftRadius, bottomLeftRadius
            });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接