蹒跚走路的程序猿

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 }
复制代码

  效果图如下:

 

posted on   蹒跚走路的程序猿  阅读(500)  评论(0编辑  收藏  举报

编辑推荐:
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
阅读排行:
· Deepseek官网太卡,教你白嫖阿里云的Deepseek-R1满血版
· 2分钟学会 DeepSeek API,竟然比官方更好用!
· .NET 使用 DeepSeek R1 开发智能 AI 客户端
· DeepSeek本地性能调优
· autohue.js:让你的图片和背景融为一体,绝了!
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

导航

统计

点击右上角即可分享
微信分享提示