Skip to content

Instantly share code, notes, and snippets.

View akexorcist's full-sized avatar
🔥

Akexorcist akexorcist

🔥
View GitHub Profile
@akexorcist
akexorcist / CouponLayout.kt
Created January 21, 2021 19:21
Build Coupon UI - Draw the UI with Canvas
class CouponLayout : FrameLayout {
/* ... */
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
canvas?.let { _ ->
canvas.drawPath(backgroundPath, backgroundPaint)
canvas.drawPath(backgroundPath, borderPaint)
}
}
}
@akexorcist
akexorcist / CouponLayout.kt
Last active January 21, 2021 19:15
Build Coupon UI - Create corner and semi circle with condition
class CouponLayout : FrameLayout {
private var cornerDirection = /* ... */
private var semiCircleDirection = /* ... */
/* ... */
private fun setupPath() {
val isTopRightRequired = isCornerDirectionRequired(CORNER_TOP_RIGHT)
val isBottomRightRequired = isCornerDirectionRequired(CORNER_BOTTOM_RIGHT)
val isBottomLeftRequired = isCornerDirectionRequired(CORNER_BOTTOM_LEFT)
val isTopLeftRequired = isCornerDirectionRequired(CORNER_TOP_LEFT)
val isRightRequired = isSemiCircleDirectionRequired(SEMI_CIRCLE_RIGHT)
@akexorcist
akexorcist / DirectionChecking.kt
Last active January 21, 2021 19:03
Build Coupon UI - Flag direction checking
const val SEMI_CIRCLE_BOTTOM = 0x08
val semiCircleDirection = 0x0A
val isBottomSemiCicleRequired = semiCircleDirection and SEMI_CIRCLE_BOTTOM == SEMI_CIRCLE_BOTTOM
// true
@akexorcist
akexorcist / CouponLayout.kt
Last active January 21, 2021 19:13
Build Coupon UI - Setup path
class CouponLayout : FrameLayout {
/* ... */
private fun setupPath() {
path.apply {
reset()
moveTo(/* Start point */)
topRightCorner (areaWidth, areaHeight, borderWidth, cornerRadius)
rightSemiCircle (areaWidth, areaHeight, borderWidth, semiCircleRadius)
bottomRightCorner (areaWidth, areaHeight, borderWidth, cornerRadius)
bottomSemiCircle (areaWidth, areaHeight, borderWidth, semiCircleRadius)
@akexorcist
akexorcist / CouponLayout.kt
Last active January 21, 2021 18:15
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 }
/* ... */
@akexorcist
akexorcist / CouponLayout.kt
Last active January 21, 2021 17:37
Build Coupon UI - Setup view attribute
class CouponLayout : FrameLayout {
companion object {
const val SEMI_CIRCLE_NONE = 0x00
const val SEMI_CIRCLE_LEFT = 0x01
const val SEMI_CIRCLE_TOP = 0x02
const val SEMI_CIRCLE_RIGHT = 0x04
const val SEMI_CIRCLE_BOTTOM = 0x08
const val SEMI_CIRCLE_HORIZONTAL = SEMI_CIRCLE_LEFT or SEMI_CIRCLE_RIGHT
const val SEMI_CIRCLE_VERTICAL = SEMI_CIRCLE_TOP or SEMI_CIRCLE_BOTTOM
const val SEMI_CIRCLE_ALL = SEMI_CIRCLE_HORIZONTAL or SEMI_CIRCLE_VERTICAL
@akexorcist
akexorcist / activity_coupon_book.xml
Last active January 21, 2021 19:38
Build Coupon UI - View implementation
<LinearLayout>
<com.lmwn.library.ui.CouponLayout
android:layout_width="300dp"
android:layout_height="200dp"
app:coupon_backgroundColor="#EEEEEE"
app:coupon_borderColor="#20000000"
app:coupon_borderWidth="4dp"
app:coupon_cornerDirection="top"
app:coupon_cornerRadius="16dp"
app:coupon_semiCircleDirection="horizontal"
@akexorcist
akexorcist / attrs.xml
Last active January 21, 2021 16:59
Build Coupon UI - View attributes
<!-- res/values/attrs.xml -->
<resources>
<declare-styleable name="CouponLayout">
<attr name="coupon_borderWidth" format="dimension" />
<attr name="coupon_cornerRadius" format="dimension" />
<attr name="coupon_semiCircleRadius" format="dimension" />
<attr name="coupon_backgroundColor" format="color|reference" />
<attr name="coupon_borderColor" format="color|reference" />
<attr name="coupon_cornerDirection" format="enum">
<enum name="none" value="0x00" />
@akexorcist
akexorcist / CouponLayout.kt
Last active February 1, 2021 14:56
Build Coupon UI - Prepare the class
class CouponLayout : FrameLayout {
constructor(context: Context) : super(context) {
setup(context, null, 0)
}
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
setup(context, attrs, 0)
}
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
### Start point
x = area_width - (corner_radius + (border_width / 2))
y = border_width / 2
### Top right corner
x1 = area_width - ((corner_radius * 2) + (border_width / 2))
y1 = border_width / 2
x2 = area_width - (border_width / 2)
y2 = (corner_radius * 2) + (border_width / 2)
start_angle = 270