Skip to content

Instantly share code, notes, and snippets.

@akexorcist
Last active January 21, 2021 18:15
Show Gist options
  • Save akexorcist/56ea9c160dcc47ec5a41d0e1a2b073d6 to your computer and use it in GitHub Desktop.
Save akexorcist/56ea9c160dcc47ec5a41d0e1a2b073d6 to your computer and use it in GitHub Desktop.
Build Coupon UI - Setup paint
class CouponLayout : FrameLayout {
private var borderWidth = /* ... */
private var backgroundPathColor = /* ... */
private var backgroundPathColorStateList: ColorStateList? = /* ... */
private var borderPathColor = /* ... */
private var borderPathColorStateList: ColorStateList? = /* ... */
/* ... */
private val borderPaint = Paint().apply { isAntiAlias = true }
private val backgroundPaint = Paint().apply { isAntiAlias = true }
/* ... */
private fun setupPaint() {
borderPaint.color = getAvailableColor(borderPathColor, borderPathColorStateList)
borderPaint.strokeWidth = borderWidth
borderPaint.style = Paint.Style.STROKE
backgroundPaint.color = getAvailableColor(backgroundPathColor, backgroundPathColorStateList)
backgroundPaint.style = Paint.Style.FILL
}
@ColorInt
private fun getAvailableColor(@ColorInt color: Int, colorStateList: ColorStateList?): Int = if (colorStateList != null) {
val defaultColor = colorStateList.getColorForState(intArrayOf(), 0)
colorStateList.getColorForState(drawableState, defaultColor)
} else {
color
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment