There is a cool new API in Compose that allows us ...
# compose
a
There is a cool new API in Compose that allows us to write Composable to a Bitmap As I can see from the sample, it still requires us to draw Composable to a screen, and then capture it to a bitmap. Question: can I write Composable to a Bitmap without rendering it to a screen? For instance, I want to generate a Bitmap icon for my Map, and not use any View-related stuff. Something like
bitmap.setContent { myComposableStuff }
y
So it has to be in your composition. But I don't think you need to render it to screen. And it doesn't need to match the properties of the display, density size etc.
I think you can skip this call to not render to the composition
a
Maybe I can, I'll give it a try, but still, it's not exactly what I want to achieve.
it has to be in your composition
I want to create some kind of Composable scope, but without any connection to UI. For instance, I can create a Bitmap anywhere, even in data layer or business logic layer. And I can draw on Canvas on this Bitmap anywhere. And I want to use Compose to draw on said Bitmap or Canvas. My use-case is building a Google Map marker image with some non-trivial layout. It doesn't need to be interactive, so currently I draw on Bitmap using Canvas, which is hard when it comes to layout. I want to: • create a Bitmap • create some kind of Composition that is not attached to my UI, but that can draw on this Bitmap's Canvas • use all powers of Compose to build my map icon
y
cc @Nader Jawad - are there options for this?
n
The GraphicsLayer API is completely decoupled from Composition. We just provide sample code to show how to leverage it to capture Compose UI content. You can take any DrawScope lambda and pass that to
GraphicsLayer#record
and call
GraphicsLayer#toImageBitmap
. If you don't want to draw the content to the screen you can avoid calling
drawLayer
within the
onDrawWithContent
call.
🎉 1