相关疑难解决方法(0)

如何在android中使用ShapeDrawable以编程方式创建圆角边框?

我需要通过扩展ShapeDrawable以编程方式创建带圆角的边框.我需要一个带圆角的黑色边框,外面的像素是白色,内部像素是透明的.我目前的代码存在多个问题,其中的问题是它不会创建一个与边框厚度相同的平滑角,并且边框的外部像素是透明的而不是白色.

这是我目前得到的角落的图片 角

这是我在构造函数中为'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);
}
Run Code Online (Sandbox Code Playgroud)

}

java android border drawable shapedrawable

16
推荐指数
2
解决办法
2万
查看次数

标签 统计

android ×1

border ×1

drawable ×1

java ×1

shapedrawable ×1