Home > database >  How to draw ImageVector on Canvas jetpack compose
How to draw ImageVector on Canvas jetpack compose

Time:11-27

I am converting SVG to vector asset by importing it to android studio. How can I draw that vector to canvas with jetpack compose. The only option I see is drawImage, which only takes ImageBitmap. But this is a vector and not a bitmap, so is there a way to just draw ImageVector.

val logoVector: ImageVector = ImageVector.vectorResource(id = R.drawable.diasyst_logo)

CodePudding user response:

You can wrap your ImageVector into a VectorPainter, which is able to render to the standard compose Canvas.

val vector = ImageVector.vectorResource(id = R.drawable.ic_launcher_foreground)
val painter = rememberVectorPainter(image = vector)
Canvas(modifier = Modifier.fillMaxSize()) {
    with(painter) {
        draw(painter.intrinsicSize)
    }
}

CodePudding user response:

If it is not strictly required to draw in Canvas, the Icon and Image Composables accept a vector graphics resource.

Icon(
 imageVector = ...
 contentDescription = ""
)

Image(
 imageVector = ...
 contentDescription = ""
)

Other than that, the only way is to basically convert the vector to a format you can use inside drawImage method of Canvas. I know you are concerned about performance and optimization, but rest assured, there's no problem using the conversion method

  • Related