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
yschimke
05/07/2024, 8:01 PM
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
alaershov
05/15/2024, 6:41 AM
Maybe I can, I'll give it a try, but still, it's not exactly what I want to achieve.
alaershov
05/15/2024, 6:47 AM
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
yschimke
05/15/2024, 4:56 PM
cc @Nader Jawad - are there options for this?
n
Nader Jawad
05/15/2024, 5:39 PM
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