我需要通过扩展ShapeDrawable来编写程序,创建带有圆角的边框。我需要一个黑色带有圆角的边框,边框外部的像素为白色,内部像素为透明。目前我的代码存在多个问题,其中之一是它不能创建与边框相同厚度的平滑圆角,另一个问题是边框的外部像素是透明而不是白色。
这里是我目前得到的圆角图片:
以下是我正在构造函数中使用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);
}
}