Skip to content

Commit bb01aa8

Browse files
authored
Improve shapes documentation (#240)
* Remove custom toPath() method in favour of using `toPath().asComposePath()` instead. * Apply Spotless * Remove custom toPath() method in favour of using `toPath().asComposePath()` instead. --------- Co-authored-by: riggaroo <[email protected]>
1 parent 0d7ac09 commit bb01aa8

File tree

1 file changed

+12
-37
lines changed
  • compose/snippets/src/main/java/com/example/compose/snippets/graphics

1 file changed

+12
-37
lines changed

compose/snippets/src/main/java/com/example/compose/snippets/graphics/ShapesSnippets.kt

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import androidx.compose.ui.graphics.Matrix
5151
import androidx.compose.ui.graphics.Outline
5252
import androidx.compose.ui.graphics.Path
5353
import androidx.compose.ui.graphics.Shape
54+
import androidx.compose.ui.graphics.asComposePath
5455
import androidx.compose.ui.graphics.drawscope.scale
5556
import androidx.compose.ui.graphics.drawscope.translate
5657
import androidx.compose.ui.graphics.graphicsLayer
@@ -87,8 +88,7 @@ fun BasicShapeCanvas() {
8788
centerX = size.width / 2,
8889
centerY = size.height / 2
8990
)
90-
val roundedPolygonPath = roundedPolygon.cubics
91-
.toPath()
91+
val roundedPolygonPath = roundedPolygon.toPath().asComposePath()
9292
onDrawBehind {
9393
drawPath(roundedPolygonPath, color = Color.Blue)
9494
}
@@ -115,8 +115,7 @@ private fun RoundedShapeExample() {
115115
smoothing = 1f
116116
)
117117
)
118-
val roundedPolygonPath = roundedPolygon.cubics
119-
.toPath()
118+
val roundedPolygonPath = roundedPolygon.toPath().asComposePath()
120119
onDrawBehind {
121120
drawPath(roundedPolygonPath, color = Color.Black)
122121
}
@@ -143,8 +142,7 @@ private fun RoundedShapeSmoothnessExample() {
143142
smoothing = 0.1f
144143
)
145144
)
146-
val roundedPolygonPath = roundedPolygon.cubics
147-
.toPath()
145+
val roundedPolygonPath = roundedPolygon.toPath().asComposePath()
148146
onDrawBehind {
149147
drawPath(roundedPolygonPath, color = Color.Black)
150148
}
@@ -181,7 +179,7 @@ private fun MorphExample() {
181179

182180
val morph = Morph(start = triangle, end = square)
183181
val morphPath = morph
184-
.toComposePath(progress = 0.5f)
182+
.toPath(progress = 0.5f).asComposePath()
185183

186184
onDrawBehind {
187185
drawPath(morphPath, color = Color.Black)
@@ -228,7 +226,8 @@ private fun MorphExampleAnimation() {
228226

229227
val morph = Morph(start = triangle, end = square)
230228
val morphPath = morph
231-
.toComposePath(progress = morphProgress.value)
229+
.toPath(progress = morphProgress.value)
230+
.asComposePath()
232231

233232
onDrawBehind {
234233
drawPath(morphPath, color = Color.Black)
@@ -300,7 +299,7 @@ class MorphPolygonShape(
300299
matrix.scale(size.width / 2f, size.height / 2f)
301300
matrix.translate(1f, 1f)
302301

303-
val path = morph.toComposePath(progress = percentage)
302+
val path = morph.toPath(progress = percentage).asComposePath()
304303
path.transform(matrix)
305304
return Outline.Generic(path)
306305
}
@@ -351,42 +350,19 @@ private fun MorphOnClick() {
351350
}
352351

353352
// [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-
}
377353
fun RoundedPolygon.getBounds() = calculateBounds().let { Rect(it[0], it[1], it[2], it[3]) }
378354
class RoundedPolygonShape(
379355
private val polygon: RoundedPolygon,
380356
private var matrix: Matrix = Matrix()
381357
) : Shape {
382-
private val path = Path()
358+
private var path = Path()
383359
override fun createOutline(
384360
size: Size,
385361
layoutDirection: LayoutDirection,
386362
density: Density
387363
): Outline {
388364
path.rewind()
389-
polygon.toPath(path)
365+
path = polygon.toPath().asComposePath()
390366
matrix.reset()
391367
val bounds = polygon.getBounds()
392368
val maxDimension = max(bounds.width, bounds.height)
@@ -482,7 +458,7 @@ class CustomRotatingMorphShape(
482458
matrix.translate(1f, 1f)
483459
matrix.rotateZ(rotation)
484460

485-
val path = morph.toComposePath(progress = percentage)
461+
val path = morph.toPath(progress = percentage).asComposePath()
486462
path.transform(matrix)
487463

488464
return Outline.Generic(path)
@@ -596,8 +572,7 @@ private fun CartesianPoints() {
596572
Box(
597573
modifier = Modifier
598574
.drawWithCache {
599-
val roundedPolygonPath = polygon.cubics
600-
.toPath()
575+
val roundedPolygonPath = polygon.toPath().asComposePath()
601576
onDrawBehind {
602577
scale(size.width * 0.5f, size.width * 0.5f) {
603578
translate(size.width * 0.5f, size.height * 0.5f) {

0 commit comments

Comments
 (0)