当前位置:网站首页>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
边栏推荐
- SAP oracle 复制新实例后数据库远程连接报错 ora-01031
- PIP image download
- Data warehouse: Doris' application practice in meituan
- Where is the DP interface of desktop computer (what if the host has no DP interface)
- Android studio connects to MySQL and completes simple login and registration functions
- VMware VCSA 7.0 Install
- Yolov5 learning notes (I) -- principle overview
- 【C】 Replace spaces and realize binary parity bit exchange of integers by macros
- 1-8 basic use of props
- After SAP Oracle replicates a new instance, the remote connection of the database reports an error ora-01031
猜你喜欢

Leetcode60. permutation sequence

What is a driver signature and how does the driver get a digital signature?

centos7安装mysql8

Doip communication of canoe application case

PowerCLi 批量添加esxi到vCenter

JS advanced ES6 ~ es13 new features

Uricase - Characteristics of uricase in Worthington pig liver:

Leetcode62. 不同路径

E-commerce data model design

【C】 Drink soda and find a single dog
随机推荐
Explanation of history and chemical properties of Worthington ribonuclease B
E-commerce data model design
【TA-霜狼_may-《百人计划》】图形3.6 纹理压缩——包体瘦身术
Leetcode 763. partition labels divide alphabetic intervals (medium)
Worthington RNA determination detailed introduction
[detailed and super simple] how to use websocket links
研发效能的道法术器
websocket心跳机制(保活机制)
Do you know any formal part-time platforms?
Review of categories 1-4
实时数仓:美团点评Flink的实时数仓应用分享
【TA-霜狼_may-《百人计划》】美术2.2 模型基础
@Transactional 注解使用详解
Jincang database kingbasees client programming interface guide ODBC (5. Development process)
Doip test development practice
【C】逆序字符串(俩种递归思路)
Js判断数据类型的4种⽅式
研发效能的道法术器
Tyrosine decarboxylase -- characteristics of tyrosine decarboxylase of Streptococcus faecalis in Worthington
双重for循环优化