Skip to content

Commit bbdb61b

Browse files
committed
Revert shape changes for now.
1 parent cf8e3c9 commit bbdb61b

File tree

1 file changed

+48
-47
lines changed
  • compose/snippets/src/main/java/com/example/compose/snippets/graphics

1 file changed

+48
-47
lines changed

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

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,20 @@ import androidx.compose.foundation.layout.size
3838
import androidx.compose.material3.MaterialTheme
3939
import androidx.compose.material3.Text
4040
import androidx.compose.runtime.Composable
41-
import androidx.compose.runtime.State
4241
import androidx.compose.runtime.getValue
43-
import androidx.compose.runtime.mutableStateOf
4442
import androidx.compose.runtime.remember
4543
import androidx.compose.ui.Alignment
4644
import androidx.compose.ui.Modifier
4745
import androidx.compose.ui.draw.clip
4846
import androidx.compose.ui.draw.drawWithCache
47+
import androidx.compose.ui.geometry.Rect
4948
import androidx.compose.ui.geometry.Size
5049
import androidx.compose.ui.graphics.Color
5150
import androidx.compose.ui.graphics.Matrix
5251
import androidx.compose.ui.graphics.Outline
5352
import androidx.compose.ui.graphics.Path
5453
import androidx.compose.ui.graphics.Shape
54+
import androidx.compose.ui.graphics.asComposePath
5555
import androidx.compose.ui.graphics.drawscope.scale
5656
import androidx.compose.ui.graphics.drawscope.translate
5757
import androidx.compose.ui.graphics.graphicsLayer
@@ -68,9 +68,11 @@ import androidx.graphics.shapes.Cubic
6868
import androidx.graphics.shapes.Morph
6969
import androidx.graphics.shapes.RoundedPolygon
7070
import androidx.graphics.shapes.star
71+
import androidx.graphics.shapes.toPath
7172
import com.example.compose.snippets.R
7273
import kotlin.math.PI
7374
import kotlin.math.cos
75+
import kotlin.math.max
7476
import kotlin.math.sin
7577

7678
@Preview
@@ -86,8 +88,7 @@ fun BasicShapeCanvas() {
8688
centerX = size.width / 2,
8789
centerY = size.height / 2
8890
)
89-
val roundedPolygonPath = roundedPolygon.cubics
90-
.toPath()
91+
val roundedPolygonPath = roundedPolygon.toPath().asComposePath()
9192
onDrawBehind {
9293
drawPath(roundedPolygonPath, color = Color.Blue)
9394
}
@@ -114,8 +115,7 @@ private fun RoundedShapeExample() {
114115
smoothing = 1f
115116
)
116117
)
117-
val roundedPolygonPath = roundedPolygon.cubics
118-
.toPath()
118+
val roundedPolygonPath = roundedPolygon.toPath().asComposePath()
119119
onDrawBehind {
120120
drawPath(roundedPolygonPath, color = Color.Black)
121121
}
@@ -142,8 +142,7 @@ private fun RoundedShapeSmoothnessExample() {
142142
smoothing = 0.1f
143143
)
144144
)
145-
val roundedPolygonPath = roundedPolygon.cubics
146-
.toPath()
145+
val roundedPolygonPath = roundedPolygon.toPath().asComposePath()
147146
onDrawBehind {
148147
drawPath(roundedPolygonPath, color = Color.Black)
149148
}
@@ -180,7 +179,7 @@ private fun MorphExample() {
180179

181180
val morph = Morph(start = triangle, end = square)
182181
val morphPath = morph
183-
.toComposePath(progress = 0.5f)
182+
.toPath(progress = 0.5f).asComposePath()
184183

185184
onDrawBehind {
186185
drawPath(morphPath, color = Color.Black)
@@ -227,7 +226,8 @@ private fun MorphExampleAnimation() {
227226

228227
val morph = Morph(start = triangle, end = square)
229228
val morphPath = morph
230-
.toComposePath(progress = morphProgress.value)
229+
.toPath(progress = morphProgress.value)
230+
.asComposePath()
231231

232232
onDrawBehind {
233233
drawPath(morphPath, color = Color.Black)
@@ -238,6 +238,7 @@ private fun MorphExampleAnimation() {
238238
// [END android_compose_graphics_polygon_morph_animation]
239239
}
240240

241+
// [START android_compose_morph_to_path]
241242
/**
242243
* Transforms the morph at a given progress into a [Path].
243244
* It can optionally be scaled, using the origin (0,0) as pivot point.
@@ -259,10 +260,11 @@ fun Morph.toComposePath(progress: Float, scale: Float = 1f, path: Path = Path())
259260
path.close()
260261
return path
261262
}
262-
263+
// [END android_compose_morph_to_path]
263264
/**
264265
* Function used to create a Path from a list of Cubics.
265266
*/
267+
// [START android_compose_list_cubics_to_path]
266268
fun List<Cubic>.toPath(path: Path = Path(), scale: Float = 1f): Path {
267269
path.rewind()
268270
firstOrNull()?.let { first ->
@@ -278,11 +280,12 @@ fun List<Cubic>.toPath(path: Path = Path(), scale: Float = 1f): Path {
278280
path.close()
279281
return path
280282
}
283+
// [END android_compose_list_cubics_to_path]
281284

282285
// [START android_compose_morph_clip_shape]
283286
class MorphPolygonShape(
284287
private val morph: Morph,
285-
private val percentage: State<Float>
288+
private val percentage: Float
286289
) : Shape {
287290

288291
private val matrix = Matrix()
@@ -296,7 +299,7 @@ class MorphPolygonShape(
296299
matrix.scale(size.width / 2f, size.height / 2f)
297300
matrix.translate(1f, 1f)
298301

299-
val path = morph.toComposePath(progress = percentage.value)
302+
val path = morph.toPath(progress = percentage).asComposePath()
300303
path.transform(matrix)
301304
return Outline.Generic(path)
302305
}
@@ -335,7 +338,7 @@ private fun MorphOnClick() {
335338
modifier = Modifier
336339
.size(200.dp)
337340
.padding(8.dp)
338-
.clip(MorphPolygonShape(morph, animatedProgress))
341+
.clip(MorphPolygonShape(morph, animatedProgress.value))
339342
.background(Color(0xFF80DEEA))
340343
.size(200.dp)
341344
.clickable(interactionSource = interactionSource, indication = null) {
@@ -347,22 +350,26 @@ private fun MorphOnClick() {
347350
}
348351

349352
// [START android_compose_shapes_polygon_compose_shape]
353+
fun RoundedPolygon.getBounds() = calculateBounds().let { Rect(it[0], it[1], it[2], it[3]) }
350354
class RoundedPolygonShape(
351-
private val polygon: State<RoundedPolygon>
355+
private val polygon: RoundedPolygon,
356+
private var matrix: Matrix = Matrix()
352357
) : Shape {
353-
private val matrix = Matrix()
358+
private var path = Path()
354359
override fun createOutline(
355360
size: Size,
356361
layoutDirection: LayoutDirection,
357362
density: Density
358363
): Outline {
359-
val path = polygon.value.cubics.toPath()
360-
// below assumes that you haven't changed the default radius of 1f, nor the centerX and centerY of 0f
361-
// 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.
362-
matrix.scale(size.width / 2f, size.height / 2f)
363-
matrix.translate(1f, 1f)
364-
path.transform(matrix)
364+
path.rewind()
365+
path = polygon.toPath().asComposePath()
366+
matrix.reset()
367+
val bounds = polygon.getBounds()
368+
val maxDimension = max(bounds.width, bounds.height)
369+
matrix.scale(size.width / maxDimension, size.height / maxDimension)
370+
matrix.translate(-bounds.left, -bounds.top)
365371

372+
path.transform(matrix)
366373
return Outline.Generic(path)
367374
}
368375
}
@@ -373,14 +380,12 @@ class RoundedPolygonShape(
373380
fun ApplyPolygonAsClipBasic() {
374381
// [START android_compose_shapes_apply_as_clip]
375382
val hexagon = remember {
376-
mutableStateOf(
377-
RoundedPolygon(
378-
6,
379-
rounding = CornerRounding(0.2f)
380-
)
383+
RoundedPolygon(
384+
6,
385+
rounding = CornerRounding(0.2f)
381386
)
382387
}
383-
val clip = remember {
388+
val clip = remember(hexagon) {
384389
RoundedPolygonShape(polygon = hexagon)
385390
}
386391
Box(
@@ -403,11 +408,9 @@ fun ApplyPolygonAsClipBasic() {
403408
fun ApplyPolygonAsClipImage() {
404409
// [START android_compose_shapes_apply_as_clip_advanced]
405410
val hexagon = remember {
406-
mutableStateOf(
407-
RoundedPolygon(
408-
6,
409-
rounding = CornerRounding(0.2f)
410-
)
411+
RoundedPolygon(
412+
6,
413+
rounding = CornerRounding(0.2f)
411414
)
412415
}
413416
val clip = remember(hexagon) {
@@ -439,8 +442,8 @@ fun ApplyPolygonAsClipImage() {
439442
// [START android_compose_shapes_custom_rotating_morph_shape]
440443
class CustomRotatingMorphShape(
441444
private val morph: Morph,
442-
private val percentage: State<Float>,
443-
private val rotation: State<Float>
445+
private val percentage: Float,
446+
private val rotation: Float
444447
) : Shape {
445448

446449
private val matrix = Matrix()
@@ -453,9 +456,9 @@ class CustomRotatingMorphShape(
453456
// 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.
454457
matrix.scale(size.width / 2f, size.height / 2f)
455458
matrix.translate(1f, 1f)
456-
matrix.rotateZ(rotation.value)
459+
matrix.rotateZ(rotation)
457460

458-
val path = morph.toComposePath(progress = percentage.value)
461+
val path = morph.toPath(progress = percentage).asComposePath()
459462
path.transform(matrix)
460463

461464
return Outline.Generic(path)
@@ -499,13 +502,6 @@ private fun RotatingScallopedProfilePic() {
499502
),
500503
label = "animatedMorphProgress"
501504
)
502-
val morphingShape = remember {
503-
CustomRotatingMorphShape(
504-
morph,
505-
animatedProgress,
506-
animatedRotation
507-
)
508-
}
509505
Box(
510506
modifier = Modifier.fillMaxSize(),
511507
contentAlignment = Alignment.Center
@@ -515,7 +511,13 @@ private fun RotatingScallopedProfilePic() {
515511
contentDescription = "Dog",
516512
contentScale = ContentScale.Crop,
517513
modifier = Modifier
518-
.clip(morphingShape)
514+
.clip(
515+
CustomRotatingMorphShape(
516+
morph,
517+
animatedProgress.value,
518+
animatedRotation.value
519+
)
520+
)
519521
.size(200.dp)
520522
)
521523
}
@@ -570,8 +572,7 @@ private fun CartesianPoints() {
570572
Box(
571573
modifier = Modifier
572574
.drawWithCache {
573-
val roundedPolygonPath = polygon.cubics
574-
.toPath()
575+
val roundedPolygonPath = polygon.toPath().asComposePath()
575576
onDrawBehind {
576577
scale(size.width * 0.5f, size.width * 0.5f) {
577578
translate(size.width * 0.5f, size.height * 0.5f) {

0 commit comments

Comments
 (0)