Hi all. I'm trying to render a gallery screen as ...
# compose
m
Hi all. I'm trying to render a gallery screen as a grid. What i'd like to do is to give each tile a composable, and capture that composable to a bitmap, scale it to a certain size, and render as an image. I've seen this article on doing this, but it renders the composable on screen and saves the picture to a file. What i really want is to just capture the rendering so i can display a preview of it. I've also looked at the Capturable library but it seems to also be geared towards content that's already rendered on screen.
s
https://developer.android.com/develop/ui/compose/graphics/draw/modifiers#composable-to-bitmap It’s a bit unclear what issue you’re encountering. To capture a bitmap from a composable, you can use a graphics layer, which can then be rendered into a bitmap.
m
@Sergey Y. This documentation assumes you want to capture content that's already on screen. So it's dual drawing to the screen and to a graphics layer so that it can write it out later. I'm just trying to render a non interactive preview of a specific composable. I don't want the actual composable rendered on screen.
s
Ah, I see. You can search the chat, I remember someone already asked that and even proposed a solution based on Android off-screen rendering using Presentation and a virtual display.
s
You'd want to do the same as in the documentation above, but remove this part:
Copy code
// draw the graphics layer on the visible canvas
drawLayer(graphicsLayer)
Instead of
Copy code
.drawWithContent {
    // call record to capture the content in the graphics layer
    graphicsLayer.record {
        // draw the contents of the composable into the graphics layer
        this@drawWithContent.drawContent()
    }
    // draw the graphics layer on the visible canvas
    drawLayer(graphicsLayer)
}
do
Copy code
.drawWithContent {
    // call record to capture the content in the graphics layer
    graphicsLayer.record {
        // draw the contents of the composable into the graphics layer
        this@drawWithContent.drawContent()
    }
}