-
Notifications
You must be signed in to change notification settings - Fork 0
/
SlickRoundedCornerShape.kt
181 lines (168 loc) · 5.9 KB
/
SlickRoundedCornerShape.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package com.zhangke.compose.ext.ui
import androidx.compose.foundation.shape.CornerSize
import androidx.compose.ui.geometry.CornerRadius
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.geometry.RoundRect
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.geometry.toRect
import androidx.compose.ui.graphics.Outline
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import kotlin.math.sqrt
class SlickRoundedCornerShape(
private val topStart: CornerSize,
private val topEnd: CornerSize,
private val bottomEnd: CornerSize,
private val bottomStart: CornerSize,
) : Shape {
override fun createOutline(
size: Size,
layoutDirection: LayoutDirection,
density: Density,
): Outline {
return createOutline(
size = size,
topStart = topStart.toPx(size, density),
topEnd = topEnd.toPx(size, density),
bottomEnd = bottomEnd.toPx(size, density),
bottomStart = bottomStart.toPx(size, density),
layoutDirection = layoutDirection
)
}
private fun createOutline(
size: Size,
topStart: Float,
topEnd: Float,
bottomEnd: Float,
bottomStart: Float,
layoutDirection: LayoutDirection,
): Outline {
val height = size.height
val width = size.width
return if (topStart + topEnd + bottomEnd + bottomStart == 0.0f) {
Outline.Rectangle(size.toRect())
} else if (topStart == bottomStart && width < (topStart * 2F)) {
val radius = topStart
val path = Path()
if (height > radius * 2) {
buildSlickRoundCornerPath(path, size, radius)
} else {
buildSingleArcPath(path, size, radius)
}
Outline.Generic(path)
} else {
Outline.Rounded(
RoundRect(
rect = size.toRect(),
topLeft = CornerRadius(if (layoutDirection == LayoutDirection.Ltr) topStart else topEnd),
topRight = CornerRadius(if (layoutDirection == LayoutDirection.Ltr) topEnd else topStart),
bottomRight = CornerRadius(if (layoutDirection == LayoutDirection.Ltr) bottomEnd else bottomStart),
bottomLeft = CornerRadius(if (layoutDirection == LayoutDirection.Ltr) bottomStart else bottomEnd)
)
)
}
}
private fun buildSlickRoundCornerPath(path: Path, size: Size, radius: Float) {
val width = size.width
val height = size.height
if (width > radius) {
path.addRoundRect(
RoundRect(
rect = size.toRect(),
topLeft = CornerRadius(radius),
topRight = CornerRadius(0F),
bottomRight = CornerRadius(0F),
bottomLeft = CornerRadius(radius),
)
)
return
}
val arcHeight = sqrt(radius * radius - (radius - width) * (radius - width))
val yOffset = radius - arcHeight
path.arcTo(
rect = Rect(
left = 0F,
top = yOffset,
right = width * 2F,
bottom = yOffset + arcHeight * 2F,
),
startAngleDegrees = 180F,
sweepAngleDegrees = 90F,
forceMoveTo = true,
)
val bottomArcBottom = height - yOffset
path.lineTo(x = width, y = bottomArcBottom)
path.arcTo(
rect = Rect(
left = 0F,
top = bottomArcBottom - arcHeight * 2,
right = width * 2F,
bottom = bottomArcBottom,
),
startAngleDegrees = 90F,
sweepAngleDegrees = 90F,
forceMoveTo = true,
)
path.lineTo(0F, yOffset + arcHeight)
path.close()
}
private fun buildSingleArcPath(path: Path, size: Size, radius: Float) {
path.moveTo(size.width, 0F)
path.arcTo(
rect = Rect(0F, 0F, radius * 2F, radius * 2F),
startAngleDegrees = 90F,
sweepAngleDegrees = 180F,
forceMoveTo = true,
)
path.close()
}
}
fun SlickRoundedCornerShape(corner: CornerSize) =
SlickRoundedCornerShape(corner, corner, corner, corner)
fun SlickRoundedCornerShape(size: Dp) =
SlickRoundedCornerShape(CornerSize(size))
fun SlickRoundedCornerShape(size: Float) =
SlickRoundedCornerShape(CornerSize(size))
fun SlickRoundedCornerShape(percent: Int) =
SlickRoundedCornerShape(CornerSize(percent))
fun SlickRoundedCornerShape(
topStart: Dp = 0.dp,
topEnd: Dp = 0.dp,
bottomEnd: Dp = 0.dp,
bottomStart: Dp = 0.dp
) = SlickRoundedCornerShape(
topStart = CornerSize(topStart),
topEnd = CornerSize(topEnd),
bottomEnd = CornerSize(bottomEnd),
bottomStart = CornerSize(bottomStart)
)
fun SlickRoundedCornerShape(
topStart: Float = 0.0f,
topEnd: Float = 0.0f,
bottomEnd: Float = 0.0f,
bottomStart: Float = 0.0f
) = SlickRoundedCornerShape(
topStart = CornerSize(topStart),
topEnd = CornerSize(topEnd),
bottomEnd = CornerSize(bottomEnd),
bottomStart = CornerSize(bottomStart)
)
fun SlickRoundedCornerShape(
/*@IntRange(from = 0, to = 100)*/
topStartPercent: Int = 0,
/*@IntRange(from = 0, to = 100)*/
topEndPercent: Int = 0,
/*@IntRange(from = 0, to = 100)*/
bottomEndPercent: Int = 0,
/*@IntRange(from = 0, to = 100)*/
bottomStartPercent: Int = 0
) = SlickRoundedCornerShape(
topStart = CornerSize(topStartPercent),
topEnd = CornerSize(topEndPercent),
bottomEnd = CornerSize(bottomEndPercent),
bottomStart = CornerSize(bottomStartPercent)
)