当前位置:网站首页>Control fillet stroke materialshapedrawable
Control fillet stroke materialshapedrawable
2022-07-29 00:07:00 【Sharp surge】
lately UI There are always inconsistent background colors , Different fillets , The four angles have different background strokes , I can only keep writing drawable Layout , A little different is to create different drawable, Great annoyance . So I wrote a tool class , Always solve differences UI Control problems .
Don't say nonsense Directly above :

quote
implementation "com.google.android.material:material:1.3.0"
Code up The code uses kotlin The top-level method is written Java And kotlin Can be called normally
/**
* Control fillet
* @param Context Context
* @param fillColor Fill color
* @param stroke Stroke width
* @param colorRes // Paint the color
* @param allCorner Quadrilateral angle
* @param topLeftCorner top left corner
* @param topRightCorner Upper right corner
* @param bottomRightCorner The lower left corner
* @param bottomLeftCorner The lower right corner
*/
@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)) // Fill color
paintStyle = Paint.Style.FILL_AND_STROKE
strokeWidth = stroke// Stroke width
strokeColor = ContextCompat.getColorStateList(activity, colorRes)// Paint the color
}
return drawable2
}
kotlin Calling code :
// Quadrilateral circle 80f
aBinding.background = roundedCorners(this,R.color.colorPrimary,10f,R.color.colorAccent,80f);
// Upper left circle 80f
bBinding.background = roundedCorners(this,R.color.light_gray,20f,R.color.red_ff4500,0f,80f);
// Upper right circle 80f
cBinding.background = roundedCorners(this,R.color.red_ff4500,30f,R.color.button_2a73fa,0f,topRightCorner=80f);
// Lower left circle 80f
dBinding.background = roundedCorners(this,R.color.red_ff4500,5f,R.color.button_2a73fa,0f,bottomRightCorner=80f);
// Lower right circle 80f
eBinding.background = roundedCorners(this,R.color.colorPrimary,2f,R.color.colorAccent,0f,bottomLeftCorner=80f);
Java Code calls
Java No, kotlin Calling this multi parameter is convenient , There is no way to specify that parameter , If you want to change the last corner, you must transfer the values of the front ones .
ControlsTheRoundedKt The top-level method is the above roundedCorners Code name
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));Is it very convenient to get it done , No longer have to create drawable Write the layout
————————————————
Copyright notice : This paper is about CSDN Blogger 「 Software Panda 」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/Originality_special/article/details/118222330
边栏推荐
- DevOps在物联网解决方案中的应用
- Urease -- Characteristics and determination scheme of Worthington jack bean urease
- Genomic DNA isolation Worthington ribonuclease A
- centos7安装mysql8
- 器利而工善,以RPA+LCAP赋能企业司库管理数字化升级
- Leetcode63. 不同路径 II
- Compose 的声明式代码如此简洁?
- 【C】 Replace spaces and realize binary parity bit exchange of integers by macros
- CMake 基础学习
- Multi sensor fusion positioning (I) -- 3D laser odometer
猜你喜欢

【C】atoi和offsetof的介绍和模拟实现

Multi sensor fusion positioning (II) -- map based positioning

Is the declarative code of compose so concise?

GhostNets on Heterogeneous Devices via Cheap Operations

Powercl batch creates and manages virtual switches

Leetcode59. 螺旋矩阵 II

迅为IMX6开发板QT系统创建AP热点基于RTL8723-交叉编译iptables

EN 1873 assembly accessories for roofing - plastic single roof lamps - CE certification

Leetcode60. 排列序列

【微服务】Nacos集群搭建以及加载文件配置
随机推荐
[data mining engineer - written examination] Dahua shares in 2022
实时数仓:网易严选基于Flink的实时数仓实践
Powercli VMware vCenter deploys conventional new VMS in batch through self built PXE server with one click
Tyrosine decarboxylase -- characteristics of tyrosine decarboxylase of Streptococcus faecalis in Worthington
考过HCIP入职心仪公司,分享华为认证学习经历及心得
软件设计师的错题汇总
[detailed and super simple] how to use websocket links
1-6 state与绑定事件
After SAP Oracle replicates a new instance, the remote connection of the database reports an error ora-01031
Dual for loop optimization
JS advanced ES6 ~ es13 new features
Urease -- Characteristics and determination scheme of Worthington jack bean urease
Leetcode 763. partition labels divide alphabetic intervals (medium)
EN 1873屋面用装配附件.塑料单个屋面灯—CE认证
【C】atoi和offsetof的介绍和模拟实现
Jincang database kingbasees client programming interface guide ODBC (5. Development process)
Eight performance analysis indicators of effective supply chain management (Part 1)
Review of categories 1-4
leetcode 763. Partition Labels 划分字母区间(中等)
Kingbasees client programming interface guide ODBC (4. Create data source)