当前位置:网站首页>控件 圆角描边 MaterialShapeDrawable
控件 圆角描边 MaterialShapeDrawable
2022-07-28 21:54:00 【锐湃】
最近UI 一直出现不统一的背景色,不同的圆角,四个角度不同背景描边都不同,我只能不停的去写drawable布局,稍微有点不同就是就去创建不同的drawable,巨烦.所以写了这了一个工具类,永远解决不同UI控件的问题.
不说废话 直接上图:

引用
implementation "com.google.android.material:material:1.3.0"
上代码 代码是用kotlin 顶层方法写的 Java与 kotlin 都可以正常调用
/**
* 控件圆角
* @param Context 上下文
* @param fillColor 填充颜色
* @param stroke 描边宽度
* @param colorRes //描边颜色
* @param allCorner 四边角度
* @param topLeftCorner 左上角
* @param topRightCorner 右上角
* @param bottomRightCorner 左下角
* @param bottomLeftCorner 右下角
*/
@JvmOverloads
fun roundedCorners(activity: Context, fillColor: Int, stroke: Float, colorRes: Int, allCorner: Float,
topLeftCorner: Float = 0f, topRightCorner: Float = 0f, bottomRightCorner: Float = 0f, bottomLeftCorner: Float = 0f): MaterialShapeDrawable {
val shapeAppearanceModel2 = ShapeAppearanceModel.builder().apply {
setAllCorners(RoundedCornerTreatment())
//setAllCornerSizes(allCorner)
setTopLeftCornerSize(if (topLeftCorner == 0f) allCorner else topLeftCorner)
setTopRightCornerSize(if (topRightCorner == 0f) allCorner else topRightCorner)
setBottomRightCornerSize(if (bottomRightCorner == 0f) allCorner else bottomRightCorner)
setBottomLeftCornerSize(if (bottomLeftCorner == 0f) allCorner else bottomLeftCorner)
}.build()
val drawable2: MaterialShapeDrawable = MaterialShapeDrawable(shapeAppearanceModel2).apply {
setTint(ContextCompat.getColor(activity, fillColor)) //填充颜色
paintStyle = Paint.Style.FILL_AND_STROKE
strokeWidth = stroke//描边宽度
strokeColor = ContextCompat.getColorStateList(activity, colorRes)//描边颜色
}
return drawable2
}
kotlin 调用代码:
//四边圆80f
aBinding.background = roundedCorners(this,R.color.colorPrimary,10f,R.color.colorAccent,80f);
//左上边圆80f
bBinding.background = roundedCorners(this,R.color.light_gray,20f,R.color.red_ff4500,0f,80f);
//右上边圆80f
cBinding.background = roundedCorners(this,R.color.red_ff4500,30f,R.color.button_2a73fa,0f,topRightCorner=80f);
//左下边圆80f
dBinding.background = roundedCorners(this,R.color.red_ff4500,5f,R.color.button_2a73fa,0f,bottomRightCorner=80f);
//右下边圆80f
eBinding.background = roundedCorners(this,R.color.colorPrimary,2f,R.color.colorAccent,0f,bottomLeftCorner=80f);
Java 代码调用
Java没有kotlin调用这种多参方便, 没办法指定传那个参数,如果想改变最后一个角就必须把前面的也都传值.
ControlsTheRoundedKt 是顶层方法就是上面的 roundedCorners代码名字
maidian.setBackground(ControlsTheRoundedKt.roundedCorners(this,R.color.colorPrimary,10f,R.color.colorAccent,80f));
maidian.setBackground(ControlsTheRoundedKt.roundedCorners(this,R.color.colorPrimary,10f,R.color.colorAccent,80f,20f));
maidian.setBackground(ControlsTheRoundedKt.roundedCorners(this,R.color.colorPrimary,10f,R.color.colorAccent,80f,20f,10f));
maidian.setBackground(ControlsTheRoundedKt.roundedCorners(this,R.color.colorPrimary,10f,R.color.colorAccent,80f,20f,10f,20f));
maidian.setBackground(ControlsTheRoundedKt.roundedCorners(this,R.color.colorPrimary,10f,R.color.colorAccent,80f,20f,10f,20f,30f));搞定是不是非常方便,再也不用一直创建drawable写布局了
————————————————
版权声明:本文为CSDN博主「软件熊猫」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Originality_special/article/details/118222330
边栏推荐
- 经典双栈实现队列,注意遍历栈的判定条件修改。
- Codeforces Round #810 (Div. 2) A - C
- Combination of smart TV and applet
- 经典的拓扑排序问题——LeetCode207 课程表+LeetCode210 课程表II
- A new generation of ultra safe cellular battery, Sihao aipao, will be available from 139900 yuan
- How to embed AI digital human function in VR panorama? Create a cloud experience
- 剑指 Offer 64. 求1+2+…+n,逻辑运算符短路效应
- Neglected smart TV applet field
- 2022 R2 mobile pressure vessel filling test question simulation test platform operation
- 可视化全链路日志追踪
猜你喜欢

With the "integration of driving and parking", freytek's high-performance domain controller leads the new track

剑指 Offer 55 - I. 二叉树的深度

Why did "you" become a test / development programmer? The value of your existence

Runloop principle (I)

Samba service setup

【CNN】为什么CNN的卷积核大小一般都是奇数

如何开一家盈利的健身房?我用1年回本的经验告诉你,别谈恋爱

编译原理研究性学习专题 2——递归下降语法分析设计原理与实现

Optimization and implementation of custom MVC

My second uncle is angry and swipes the screen all over the network. How can he cure my spiritual internal friction?
随机推荐
2022年R2移动式压力容器充装考题模拟考试平台操作
搭载新一代超安全蜂窝电池,思皓爱跑上市13.99万元起售
可视化全链路日志追踪
Compatibility description between kingbasees and Oracle (2. Data type)
[self] - brush questions BFS
Classic topological sorting problem -- leetcode207 curriculum +leetcode210 curriculum II
Arduino uno driver universe 1.8 'TFT SPI screen example demonstration (including data package)
In order for digital retail to continue to play its role, we need to give new connotation and significance to digital retail
这款全网热评的无线路由器,到底有什么特别?
英特尔数据中心GPU正式发货,以开放灵活提供强劲算力
【CNN】为什么CNN的卷积核大小一般都是奇数
[self] - question brushing - peak value
金仓数据库 KingbaseES与Oracle的兼容性说明(2. 数据类型)
Compatibility description between kingbasees and Oracle (3. Common functions)
解决线程安全问题&&单例模式
General addition, deletion, modification and query of custom MVC
Merkle tree
What if win11 quick copy and paste cannot be used? Win11 shortcut copy and paste cannot be used
CV实例分割模型小抄(1)
Manufacturing steps of interactive slide screen in exhibition hall