Skip to content

Commit 41199d3

Browse files
authored
Fix Shape snippets to correctly work with different parent sizing. (#233)
* Fix Shape snippets to correctly work with different parent sizing. * Fix Shape snippets to correctly work with different parent sizing.
1 parent ad9ffb0 commit 41199d3

File tree

1 file changed

+38
-8
lines changed
  • compose/snippets/src/main/java/com/example/compose/snippets/graphics

1 file changed

+38
-8
lines changed

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

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import androidx.compose.ui.Alignment
4444
import androidx.compose.ui.Modifier
4545
import androidx.compose.ui.draw.clip
4646
import androidx.compose.ui.draw.drawWithCache
47+
import androidx.compose.ui.geometry.Rect
4748
import androidx.compose.ui.geometry.Size
4849
import androidx.compose.ui.graphics.Color
4950
import androidx.compose.ui.graphics.Matrix
@@ -66,9 +67,11 @@ import androidx.graphics.shapes.Cubic
6667
import androidx.graphics.shapes.Morph
6768
import androidx.graphics.shapes.RoundedPolygon
6869
import androidx.graphics.shapes.star
70+
import androidx.graphics.shapes.toPath
6971
import com.example.compose.snippets.R
7072
import kotlin.math.PI
7173
import kotlin.math.cos
74+
import kotlin.math.max
7275
import kotlin.math.sin
7376

7477
@Preview
@@ -348,22 +351,49 @@ private fun MorphOnClick() {
348351
}
349352

350353
// [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+
fun RoundedPolygon.getBounds() = calculateBounds().let { Rect(it[0], it[1], it[2], it[3]) }
351378
class RoundedPolygonShape(
352-
private val polygon: RoundedPolygon
379+
private val polygon: RoundedPolygon,
380+
private var matrix: Matrix = Matrix()
353381
) : Shape {
354-
private val matrix = Matrix()
382+
private val path = Path()
355383
override fun createOutline(
356384
size: Size,
357385
layoutDirection: LayoutDirection,
358386
density: Density
359387
): Outline {
360-
val path = polygon.cubics.toPath()
361-
// below assumes that you haven't changed the default radius of 1f, nor the centerX and centerY of 0f
362-
// By default this stretches the path to the size of the container, if you don't want stretching, use the same size.width for both x and y.
363-
matrix.scale(size.width / 2f, size.height / 2f)
364-
matrix.translate(1f, 1f)
365-
path.transform(matrix)
388+
path.rewind()
389+
polygon.toPath(path)
390+
matrix.reset()
391+
val bounds = polygon.getBounds()
392+
val maxDimension = max(bounds.width, bounds.height)
393+
matrix.scale(size.width / maxDimension, size.height / maxDimension)
394+
matrix.translate(-bounds.left, -bounds.top)
366395

396+
path.transform(matrix)
367397
return Outline.Generic(path)
368398
}
369399
}

0 commit comments

Comments
 (0)