我需要通过扩展 ShapeDrawable 以编程方式创建带有圆角的边框。我需要有一个带有圆角的黑色边框,外部的像素是白色的,内部的像素是透明的。我目前的代码有多个问题,其中包括它没有创建与边框厚度相同的平滑角,并且边框的外部像素是透明的而不是白色的。
Here is a picture of the corners I am currently getting
这是我在构造函数中为“fill”传递 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);
}
}