How to use image loaders with Canvas and drawImage...
# compose
y
How to use image loaders with Canvas and drawImage? It seems Canvas API requires ImageBitmap instead of Painter, so the image loader APIs like coil rememberImagePainter don't work.
So far came up with the following. But I guess need to put this behind some facade like rememberImageBitmap. Am I missing something?
Copy code
var imageBitmap by remember { mutableStateOf<ImageBitmap?>(null) }

    val imageLoader = LocalImageLoader.current
    val context = LocalContext.current

    LaunchedEffect(key1 = null) {
        val result = imageLoader.execute(ImageRequest.Builder(context)
            .data("<https://upload.wikimedia.org/wikipedia/commons/3/31/Wiki_logo_Nupedia.jpg>")
            .allowConversionToBitmap(true)
            .build())

        imageBitmap = result.drawable?.toBitmap()?.asImageBitmap()
    }
a
Painter
class has a
draw()
function.
y
Perfect, thanks. DrawScope.draw(). That's a nice pattern for extending the Canvas API.
Is this the only way to actually call it?
Copy code
Canvas(modifier = Modifier.fillMaxSize()) {
            with (bitmap) {
                draw(size = ...)
            }
a
I think so.