android 利用代码实现圆角button(shape)

来源:互联网 发布:windows开发是什么工作 编辑:程序博客网 时间:2024/04/29 02:02

最近项目需求需要利用网络获取颜色,而不可以使用xml文件来实现shape ,也就是圆角矩形,搜集了一些资料,发现了RoundRectShape类,可以实现我的需要,nice。

其实他的用法也非常简单,直接贴代码

/**
* 外部矩形弧度
*/
float[] outerRadian = new float[]{20, 20, 20, 20, 20, 20, 20, 20};
/**
* 内部矩形与外部矩形的距离
*/
RectF insetDistance = new RectF(100, 100, 50, 50);
/**
* 内部矩形弧度
* */
float[] insideRadian = new float[]{20, 20, 20, 20, 20, 20, 20, 20};
/**
* 如果insetDistance与insideRadian设为null亦可
*/
RoundRectShape roundRectShape = new RoundRectShape(outerRadian, insetDistance, insideRadian);
ShapeDrawable drawable = new ShapeDrawable(roundRectShape);
/**
* 指定填充颜色
*/
drawable.getPaint().setColor(Color.YELLOW);
/**
* 指定填充模式
*/
drawable.getPaint().setStyle(Paint.Style.FILL);
all_bankCard_Round_layout.setBackground(drawable);


其实圆角矩形 的设置 就是通过RoundRectShape roundRectShape  = new RoundRectShape(outerRadian, null, null); 即可

2 0
原创粉丝点击