当前位置:网站首页>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
边栏推荐
- 1-7 solve the problem of this pointing of methods in classes
- How NAT configures address translation
- Uricase - Characteristics of uricase in Worthington pig liver:
- curl (7) Failed connect to localhost8080; Connection refused
- PIP image download
- [TA frost wolf \u may - "hundred people plan"] Figure 3.6 texture compression - inclusion slimming
- 1-8 basic use of props
- @Detailed explanation of the use of transactional annotation
- Linux之yum安装MySQL
- 【MySQL 8】Generated Invisible Primary Keys(GIPK)
猜你喜欢

The failure rate is as high as 80%. How to correctly complete the strategic planning of digital transformation?

How NAT configures address translation

多传感器融合定位(三)——惯性技术

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

Leetcode60. 排列序列

Leetcode64. Minimum path sum

EN 1873屋面用装配附件.塑料单个屋面灯—CE认证

DoIP测试开发实践

【C】 Reverse string (two recursive ideas)

【TA-霜狼_may-《百人计划》】美术2.2 模型基础
随机推荐
Worthington RNA determination detailed introduction
Is the declarative code of compose so concise?
JS four formulas for judging data types
@Detailed explanation of the use of transactional annotation
Briefly introduce the working principle and characteristics of block cipher encryption block link mode (cryptography shift cipher encryption and decryption)
Eight performance analysis indicators of effective supply chain management (Part 1)
1-7 solve the problem of this pointing of methods in classes
Multi sensor fusion positioning (II) -- map based positioning
Leetcode62. 不同路径
Kingbasees client programming interface guide ODBC (4. Create data source)
Urease -- Characteristics and determination scheme of Worthington jack bean urease
[TA frost wolf \u may - "hundred people plan"] Figure 3.6 texture compression - inclusion slimming
pip镜像下载
curl (7) Failed connect to localhost8080; Connection refused
多传感器融合定位(一)——3D激光里程计
【TA-霜狼_may-《百人计划》】美术2.2 模型基础
以JSP为视图解析器搭建SSM项目
Please briefly describe the respective characteristics of list, set and map type sets (briefly describe three different inheritance methods)
Leetcode 763. partition labels divide alphabetic intervals (medium)
Multi sensor fusion positioning (I) -- 3D laser odometer