androidcanvaspathandroid-jetpack-compose

android.graphics.Path in Compose


I try place image behind cutout. And by documentation I can get path of cutout (LocalView.current.view.rootWindowInsets.displayCutout.cutout.cutoutPath)

But this path is android.graphics.Path and compose Canvas get androidx.compose.ui.graphics.Path for draw. I don't find any information about mapping android.graphics.Path to androidx.compose.ui.graphics.Path or drawing android.graphics.Path in Compose.

Does anyone know how I can use android.graphics.Path in Compose?


Solution

  • You can convert android.graphics.Path to androidx.compose.ui.graphics.Path with path.asComposePath(). Inside Canvas' or Modifier.drawWithContent/Behind/drawWithCache which both have lambdas for DrawScope for drawing, you can draw path.

    val path: Path = Path()
    val composePath: androidx.compose.ui.graphics.Path = path.asComposePath()
    
    
    Canvas(modifier = Modifier.fillMaxSize()) {
        // Like this
        drawPath(composePath, Color.Red)
        // or like this
        drawPath(path.asComposePath(), Color.Red)
    }