drawable如何只让两个叫圆角_如何在android中使用ShapeDrawable以编程方式创建一个圆形的角落边框?...

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

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

}

}

解决方案

If you need evenly rounded corners (and from your example it seems you do) you can simply use GradentDrawable with a solid color

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.

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.

It is recommended to invoke mutate() before changing this property.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值