1

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")
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Vasil Test
  • 31
  • 2

2 Answers2

0

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)
    }
}
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
0

Drawscope extension for abstract class Painter has size, alpha and colorFilter params. You can change these params. If you wish to change draw position of Icon, it' drawn top left (0,0) by default you can use translate function or other functions such as rotate or scale for further operations

val painter = rememberVectorPainter(Icons.Rounded.Menu)
Canvas(modifier = Modifier.fillMaxSize()) {
    translate(left = 0f, top = 0f) {
        with(painter) {
         //   draw(size = painter.intrinsicSize)
            draw(
                size = Size(40.dp.toPx(), 40.dp.toPx()),
                alpha = 1f,
                colorFilter = ColorFilter.tint(Color.Red)
            )
        }
    }
}
Thracian
  • 43,021
  • 16
  • 133
  • 222