20

I need to create a border with rounded corners programatically by extending ShapeDrawable. I need to have a black border with rounded corners with the pixels on the outside being white and the inner pixels being transparent. The code I have at the moment has multiple problems, of which are that it does not create a smooth corner that is the same thickness as the border and that the outer pixels of the border are transparent and not white.

Here is a picture of the corners I am currently getting corner

Here is the code where I am passing Color.TRANSPARENT for 'fill' in the constructor:

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

}

4

6 回答 6

29

如果您需要均匀的圆角(从您的示例看来您确实需要),您可以简单地使用带有纯色的 GradentDrawable

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]。这些角按左上角、右上角、右下角、左下角的顺序排列。仅当形状为 RECTANGLE 类型时才使用此属性。

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

于 2015-01-16T09:27:36.350 回答
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 保存在项目的可绘制文件夹中。现在将它用作任何小部件的普通可绘制对象。例如:android:background="R.drawable.round_shape"

此示例在以下链接中引用。

于 2013-11-24T18:04:25.920 回答
0

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

GradientDrawable d = new GradientDrawable();
d.setCornerRadii({5.0f,5.0f,5.0f,5.0f});
textViewExample.setBackgroundResource(d);
于 2017-06-13T09:40:51.873 回答
0
 setCornerRadii(new float[] {
                    topLeftRadius, topLeftRadius,
                    topRightRadius, topRightRadius,
                    bottomRightRadius, bottomRightRadius,
                    bottomLeftRadius, bottomLeftRadius
            });
于 2020-06-22T08:33:47.370 回答
0
GradientDrawable drawable = (GradientDrawable)image.getBackground();
drawable.setGradientRadius(radiuspx);
于 2017-06-14T05:51:03.930 回答
0

而不是GradientDrawable,它更好地与 一起使用ShapeDrawableRoundRectShape这里是它的一个例子:

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

或者

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

然后drawable本身

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

更多关于RoundRectShape

于 2021-09-13T15:52:33.080 回答