Cyril Kym
06/20/2022, 12:24 PMMichael Paus
06/20/2022, 12:43 PMephemient
06/20/2022, 1:29 PMandroid.graphics.Path
, you can use the old android.graphics.Region
API:
val paths = mapOf(
"a" to Path().apply {
addRect(Rect(100.0f, 100.0f, 400.0f, 500.0f))
},
"b" to Path().apply {
addArc(
oval = Rect(400.0f, 500.0f, 600.0f, 700.0f),
startAngleDegrees = 0.0f,
sweepAngleDegrees = 360.0f,
)
},
"c" to Path().apply {
addRoundRect(RoundRect(500.0f, 200.0f, 700.0f, 400.0f, CornerRadius(25.0f)))
},
)
Canvas(
modifier = Modifier
.fillMaxSize()
.pointerInput(paths) {
val boundingRegion = Region(0, 0, size.width, size.height)
val regions = paths.mapValues { (_, path) ->
Region().apply { setPath(path.asAndroidPath(), boundingRegion) }
}
detectTapGestures { (x, y) ->
val key = regions.entries.find { (_, region) ->
region.contains(x.roundToInt(), y.roundToInt())
}?.key
Log.i("Canvas", "tap($key)")
}
}
) {
val random = Random(1)
for (path in paths.values) {
drawPath(
path = path,
color = Color.hsv(hue = random.nextFloat() * 255.0f, saturation = 1.0f, value = 1.0f),
style = Fill,
)
}
}