We can draw Path
s using Canvas
here, but I want to know whether I could just draw an irregular closed loop, then apply a background to the inside of the loop. For example, there can be just a box, filled to the screen, with a white background, then I draw a shape (a loop essentially). Now how can I apply a red background only to the area captured by the loop? Composables seem to be rects essentially so it seems pretty impossible.
Can I clip out a Path in a Composable
It looks like you are looking for drawing a path in different styles.
With the same Path
, you can both fill and stroke it:
Canvas(modifier = Modifier
.fillMaxWidth()
.aspectRatio(1f)
.padding(30.dp)
) {
val path = Path().apply {
addOval(Rect(Offset.Zero, size))
}
drawPath(path = path, color = Color.Yellow, style = Fill)
drawPath(path = path, color = Color.Blue, style = Stroke(width = 15f))
}
If you need to create a Path
from SVG data and scale it, you can use android Path
, because compose one does not seem to have support for transformations yet:
Canvas(modifier = Modifier
.fillMaxWidth()
.aspectRatio(1f)
.padding(30.dp)
) {
val path = PathParser.createPathFromPathData("M20.8221,3.25C18.5096,1.2375 15.5096,0 12.1971,0C6.9971,0 2.5221,3.025 0.3971,7.4125C-0.0029,8.25 0.4471,9.25 1.3346,9.55C2.0721,9.8 2.8721,9.45 3.2096,8.75C4.8346,5.425 8.2471,3.125 12.1971,3.125C14.6346,3.125 16.8596,4.025 18.5971,5.475L16.2096,7.8625C15.4221,8.65 15.9721,10 17.0846,10H24.0721C24.7596,10 25.3221,9.4375 25.3221,8.75V1.7625C25.3221,0.65 23.9721,0.0875 23.1846,0.875L20.8221,3.25Z")
val rectF = RectF()
path.computeBounds(rectF, true)
val matrix = Matrix()
val scale = minOf( size.width / rectF.width(), size.height / rectF.height() )
matrix.setScale(scale, scale)
path.transform(matrix)
val composePath = path.asComposePath()
drawPath(path = composePath, color = Color.Yellow, style = Fill)
drawPath(path = composePath, color = Color.Blue, style = Stroke(width = 15f))
}
© 2022 - 2024 — McMap. All rights reserved.