How to add an icon to canvas in Jetpack Compose?

Viewed 37

I am trying to add and icon from resource in Canvas DrawScope. The nearest solution I found is drawImage(), but it doesn't work for my case. Also I cannot use the normal Icon() composable inside the DrawScope. So is there any work around to display an icon inside canvas, similarly to the way we do it with composable:

import androidx.compose.material.Icon

Icon(Icons.Rounded.Menu, contentDescription = "Localized description")
1 Answers

Icons.Rounded.Menu is a VectorImage and you can wrap it into a VectorPainter. You can use something like:

val painter = rememberVectorPainter(Icons.Rounded.Menu)
Canvas(modifier = Modifier.fillMaxSize()) {
    with(painter) {
        draw(painter.intrinsicSize)
    }
}
Related