Java 如何在android中使用ShapeDrawable以编程方式创建圆角边框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20171483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to programmatically create a round cornered border using ShapeDrawable in android?
提问by user1592512
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.
我需要通过扩展 ShapeDrawable 以编程方式创建带圆角的边框。我需要有一个带圆角的黑色边框,外面的像素是白色的,里面的像素是透明的。我目前的代码有多个问题,其中没有创建与边框厚度相同的平滑角,并且边框的外部像素是透明的而不是白色的。
Here is a picture of the corners I am currently getting
这是我目前得到的角落的图片
Here is the code where I am passing Color.TRANSPARENT for 'fill' in the constructor:
这是我在构造函数中为“填充”传递 Color.TRANSPARENT 的代码:
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);
}
}
}
回答by muneikh
You can implement a custom drawable. Following is the example of the xml.
您可以实现自定义 drawable。以下是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>
Save this xml in the drawable folder of your project. Now use it like a normal drawable for any widget. For example: android:background="R.drawable.round_shape"
将此 xml 保存在项目的 drawable 文件夹中。现在将它用作任何小部件的普通可绘制对象。例如:android:background="R.drawable.round_shape"
This example is referenced following link.
此示例在以下链接中引用。
回答by SGal
If you need evenly rounded corners (and from your example it seems you do) you can simply use GradentDrawable with a solid color
如果您需要均匀的圆角(并且从您的示例看来您确实需要),您可以简单地使用带有纯色的 GradentDrawable
GradientDrawable gd = new GradientDrawable();
gd.setColor(Color.RED);
gd.setCornerRadius(10);
gd.setStroke(2, Color.WHITE);
view.setBackground(gd);
GradientDrawable documentation can be found here.
GradientDrawable 文档可以在这里找到。
Edit: For each corner separately
编辑:分别为每个角
You can specify radius of each corner separately using setCornerRadii (float[] radii)
method. "For each corner, the array contains 2 values, [X_radius, Y_radius]. The corners are ordered top-left, top-right, bottom-right, bottom-left. This property is honored only when the shape is of type RECTANGLE.
您可以使用setCornerRadii (float[] radii)
方法分别指定每个角的半径。“对于每个角,该数组包含 2 个值,[X_radius, Y_radius]。角的顺序为左上、右上、右下、左下。仅当形状为 RECTANGLE 类型时,此属性才可用。
It is recommended to invoke mutate()
before changing this property.
建议mutate()
在更改此属性之前调用。
回答by enriquep92
In addition to specify the round corner dimens you can use GradientDrawable and the method setCornerRadii()
除了指定圆角尺寸外,您还可以使用 GradientDrawable 和方法 setCornerRadii()
GradientDrawable d = new GradientDrawable();
d.setCornerRadii({5.0f,5.0f,5.0f,5.0f});
textViewExample.setBackgroundResource(d);
回答by inhogo
GradientDrawable drawable = (GradientDrawable)image.getBackground();
drawable.setGradientRadius(radiuspx);