可绘制资源¶
设置单色背景¶
调用 setSingle
将 GradientDrawable
设置为单色。
getBinding().btn.background = GradientDrawable().apply {
shape = RECTANGLE
cornerRadius = 20f.DP
setSingle(Color.argb(255,2,218,197))
}
设置圆角¶
调用 setCornerRadii
单独设置 GradientDrawable
的四个圆角。
// 设置四个圆角,半径均为50.0
getBinding().btn.background = GradientDrawable().apply {
shape = RECTANGLE
setSingle(Color.BLACK)
cornerRadius = 50f
}
// 设置四个大小不同的圆角
getBinding().btn.background = GradientDrawable().apply {
shape = RECTANGLE
setSingle(Color.BLACK)
setCornerRadii(0f, 0f, 50f, 50f)
}
设置渐变¶
setGradient
目前支持两种渐变颜色形式。
- startColor->endColor
- startColor->centerColor->endColor
// startColor->centerColor->endColor
getBinding().btn.background = GradientDrawable().apply {
shape = RECTANGLE
cornerRadius = 50f
setGradient(
45,
colorHex2Int("#12c2e9"),
colorHex2Int("#c471ed"),
colorHex2Int("#f64f59")
)
}
// startColor->endColor
getBinding().btn.background = GradientDrawable().apply {
shape = RECTANGLE
cornerRadius = 50f
setGradient(
45,
colorHex2Int("#0F2027"),
colorHex2Int("#78ffd6")
)
}