Android ShapeDrawable
今天做项目碰到一个这样的情况,就是颜色指示框,用的是正方形边框是黑色的,里面填充颜色,颜色值是动态的,为了解决这个问题,查了好多资料,终于找到解决的方法,利用ShapeDrawable,我们自定义一个CustomShapeDrawable类继承ShapeDrawable,代码:
1 public class CustomShapeDrawable extends ShapeDrawable { 2 private final Paint fillpaint, strokepaint; 3 4 public CustomShapeDrawable(Shape s, int fill, int stroke, int strokeWidth) { 5 super(s); 6 fillpaint = new Paint(this.getPaint()); 7 fillpaint.setColor(fill); 8 strokepaint = new Paint(fillpaint); 9 strokepaint.setStyle(Paint.Style.STROKE); 10 strokepaint.setStrokeWidth(strokeWidth); 11 strokepaint.setColor(stroke); 12 } 13 14 @Override 15 protected void onDraw(Shape shape, Canvas canvas, Paint paint) { 16 shape.draw(canvas, fillpaint); 17 shape.draw(canvas, strokepaint); 18 } 19 }
效果图如下:
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步