Hi Folks! Am struggling into drawing a VectorDrawa...
# compose
m
Hi Folks! Am struggling into drawing a VectorDrawable with the Canvas composable, full info in the 🧵
Am trying to draw a Knob drawable like follows:
Copy code
@Composable
    fun Knob() {
        val knobBitmap = ImageBitmap.imageResource(id = R.drawable.rotary_knob)
        Canvas(Modifier.size(200.dp)) { drawImage(knobBitmap) }
    }
and Im getting
android.graphics.drawable.VectorDrawable cannot be cast to android.graphics.drawable.BitmapDrawable
I get that the following internal call is where the issue resides :
Copy code
fun ImageBitmap.Companion.imageResource(res: Resources, @DrawableRes id: Int): ImageBitmap {
    return (res.getDrawable(id, null) as BitmapDrawable).bitmap.asImageBitmap()
}
as is a VectorDrawable rather thana BitmapDrawable
but then what would be the APi to achieve what Im trying?
something like
Copy code
resources.getDrawable(R.drawable.rotary_knob, null)
            .toBitmap(200 ,200)
works, but I was hopping there is a better approach?
i
Vector assets are loaded via `painterResource`: https://developer.android.com/jetpack/compose/resources#vector-assets
n
You can do the following:
Copy code
val vectorPainter = rememberVectorPainter(           
ImageVector.vectorResource(R.drawable.ic_android_orange)
)
Canvas {
    with(vectorPainter) {
        draw(Size(96.dp.toPx(), 96.dp.toPx()))
    }
}
👀 1
m
Vector assets are loaded via `painterResource`: https://developer.android.com/jetpack/compose/resources#vector-assets
Isn´t that for Image/Icon Comopsable?
drawImage
from the Canvas DrawScope does not requires a painter, but a
ImageBitmap
m
Oh I see, thanks both!