@@ -51,6 +51,7 @@ import androidx.compose.ui.graphics.Matrix
51
51
import androidx.compose.ui.graphics.Outline
52
52
import androidx.compose.ui.graphics.Path
53
53
import androidx.compose.ui.graphics.Shape
54
+ import androidx.compose.ui.graphics.asComposePath
54
55
import androidx.compose.ui.graphics.drawscope.scale
55
56
import androidx.compose.ui.graphics.drawscope.translate
56
57
import androidx.compose.ui.graphics.graphicsLayer
@@ -87,8 +88,7 @@ fun BasicShapeCanvas() {
87
88
centerX = size.width / 2 ,
88
89
centerY = size.height / 2
89
90
)
90
- val roundedPolygonPath = roundedPolygon.cubics
91
- .toPath()
91
+ val roundedPolygonPath = roundedPolygon.toPath().asComposePath()
92
92
onDrawBehind {
93
93
drawPath(roundedPolygonPath, color = Color .Blue )
94
94
}
@@ -115,8 +115,7 @@ private fun RoundedShapeExample() {
115
115
smoothing = 1f
116
116
)
117
117
)
118
- val roundedPolygonPath = roundedPolygon.cubics
119
- .toPath()
118
+ val roundedPolygonPath = roundedPolygon.toPath().asComposePath()
120
119
onDrawBehind {
121
120
drawPath(roundedPolygonPath, color = Color .Black )
122
121
}
@@ -143,8 +142,7 @@ private fun RoundedShapeSmoothnessExample() {
143
142
smoothing = 0.1f
144
143
)
145
144
)
146
- val roundedPolygonPath = roundedPolygon.cubics
147
- .toPath()
145
+ val roundedPolygonPath = roundedPolygon.toPath().asComposePath()
148
146
onDrawBehind {
149
147
drawPath(roundedPolygonPath, color = Color .Black )
150
148
}
@@ -181,7 +179,7 @@ private fun MorphExample() {
181
179
182
180
val morph = Morph (start = triangle, end = square)
183
181
val morphPath = morph
184
- .toComposePath (progress = 0.5f )
182
+ .toPath (progress = 0.5f ).asComposePath( )
185
183
186
184
onDrawBehind {
187
185
drawPath(morphPath, color = Color .Black )
@@ -228,7 +226,8 @@ private fun MorphExampleAnimation() {
228
226
229
227
val morph = Morph (start = triangle, end = square)
230
228
val morphPath = morph
231
- .toComposePath(progress = morphProgress.value)
229
+ .toPath(progress = morphProgress.value)
230
+ .asComposePath()
232
231
233
232
onDrawBehind {
234
233
drawPath(morphPath, color = Color .Black )
@@ -300,7 +299,7 @@ class MorphPolygonShape(
300
299
matrix.scale(size.width / 2f , size.height / 2f )
301
300
matrix.translate(1f , 1f )
302
301
303
- val path = morph.toComposePath (progress = percentage)
302
+ val path = morph.toPath (progress = percentage).asComposePath( )
304
303
path.transform(matrix)
305
304
return Outline .Generic (path)
306
305
}
@@ -351,42 +350,19 @@ private fun MorphOnClick() {
351
350
}
352
351
353
352
// [START android_compose_shapes_polygon_compose_shape]
354
- @JvmOverloads
355
- fun RoundedPolygon.toPath (path : Path = Path ()): Path {
356
- pathFromCubics(path, cubics)
357
- return path
358
- }
359
- private fun pathFromCubics (
360
- path : Path ,
361
- cubics : List <Cubic >
362
- ) {
363
- var first = true
364
- path.rewind()
365
- for (element in cubics) {
366
- if (first) {
367
- path.moveTo(element.anchor0X, element.anchor0Y)
368
- first = false
369
- }
370
- path.cubicTo(
371
- element.control0X, element.control0Y, element.control1X, element.control1Y,
372
- element.anchor1X, element.anchor1Y
373
- )
374
- }
375
- path.close()
376
- }
377
353
fun RoundedPolygon.getBounds () = calculateBounds().let { Rect (it[0 ], it[1 ], it[2 ], it[3 ]) }
378
354
class RoundedPolygonShape (
379
355
private val polygon : RoundedPolygon ,
380
356
private var matrix : Matrix = Matrix ()
381
357
) : Shape {
382
- private val path = Path ()
358
+ private var path = Path ()
383
359
override fun createOutline (
384
360
size : Size ,
385
361
layoutDirection : LayoutDirection ,
386
362
density : Density
387
363
): Outline {
388
364
path.rewind()
389
- polygon.toPath(path )
365
+ path = polygon.toPath().asComposePath( )
390
366
matrix.reset()
391
367
val bounds = polygon.getBounds()
392
368
val maxDimension = max(bounds.width, bounds.height)
@@ -482,7 +458,7 @@ class CustomRotatingMorphShape(
482
458
matrix.translate(1f , 1f )
483
459
matrix.rotateZ(rotation)
484
460
485
- val path = morph.toComposePath (progress = percentage)
461
+ val path = morph.toPath (progress = percentage).asComposePath( )
486
462
path.transform(matrix)
487
463
488
464
return Outline .Generic (path)
@@ -596,8 +572,7 @@ private fun CartesianPoints() {
596
572
Box (
597
573
modifier = Modifier
598
574
.drawWithCache {
599
- val roundedPolygonPath = polygon.cubics
600
- .toPath()
575
+ val roundedPolygonPath = polygon.toPath().asComposePath()
601
576
onDrawBehind {
602
577
scale(size.width * 0.5f , size.width * 0.5f ) {
603
578
translate(size.width * 0.5f , size.height * 0.5f ) {
0 commit comments