How to convert a compose `ImageVector` to an Andro...
# compose-android
m
How to convert a compose
ImageVector
to an Android
Bitmap
? Note - need to use it for
CustomTabsIntent.Builder#setCloseButtonIcon
so not within a composable. https://developer.android.com/reference/androidx/browser/customtabs/CustomTabsIntent.Builder#setCloseButtonIcon(android.graphics.Bitmap)
e
untested but it should be doable with something along these lines
Copy code
fun Painter.toImageBitmap(
    density: Density,
    layoutDirection: LayoutDirection,
    size: Size = intrinsicSize,
    config: ImageBitmapConfig = ImageBitmapConfig.Argb8888,
): ImageBitmap {
    val image = ImageBitmap(width = size.width.roundToInt(), height = size.height.roundToInt(), config = config)
    Canvas(image)
    CanvasDrawScope().draw(density = density, layoutDirection = layoutDirection, canvas = canvas, size = size) {
        draw(size = this.size)
    }
    return image
}

val painter = rememberVectorPainter(image = ...)
painter.toImageBitmap(density = Density(density = 1f), layoutDirection = LayoutDirection.Ltr).asAndroidBitmap()
m
Thanks very much, but don’t I need a composable to go from
ImageVector
to
Painter
?
🚫 1
e
remember*
is on
@Composable
only
you can dig in and grab the
VectorPainter
but it's
internal
😖
m
Yeah, that’s a lot of code to copy. Methinks it’s easier to stick with the vector drawable resource and use it directly!
875 Views