Stefan Oltmann
09/07/2022, 9:53 AMImageComposeScene
with this code to take screenshots of my Composables which works fine for most things.
But now I got a Composable which shows an Image
Composable after a produceState()
loaded the BitmapPainter
.
As long as the bitmapPainter has the initialValue of null is just displays an empty Box
as placeholder.
My question now is how I can let ImageComposeScene
wait until the produceState()
executed and re-composed until taking the screenshot. Now the screenshot is taken immediately after the first composition which leaves me with an empty Box
.
val bitmapPainter = produceState<BitmapPainter?>(initialValue = null, thumbnailFileName) {
value = withContext(Dispatchers.Default) {
val image = imageLoader.loadThumbnailImage(thumbnailFileName)
?: return@withContext null
return@withContext BitmapPainter(image = image)
}
}
@OptIn(ExperimentalComposeUiApi::class)
fun takeScreenshot(content: @Composable () -> Unit): ByteArray {
ImageComposeScene(
width = 1024,
height = 768,
density = Density(1f),
content = content
).use {
return@takeScreenshot it.render().encodeToData(EncodedImageFormat.PNG)!!.bytes
}
}
Stefan Oltmann
09/07/2022, 11:55 AMuse()
because the test will run endless then.
Hopefully someone tells me that I do it all wrong and how I must actually do this. 🙈
@OptIn(ExperimentalComposeUiApi::class)
fun takeScreenshot(content: @Composable () -> Unit): ByteArray {
val scene = ImageComposeScene(
width = TestAppStates.DEFAULT_SCREEN_WIDTH,
height = TestAppStates.DEFAULT_SCREEN_HEIGHT,
density = density,
content = content
)
/* First call to render() starts the animations and executes produceState() blocks. */
scene.render()
/*
* The second call to render() produces the final screenshot.
* See <https://github.com/JetBrains/compose-jb/issues/1395>
*
* We are supposed to close the ImageComposeScene now, but
* if we do so the test will run endless.
*/
return scene.render().encodeToPng()
}
Kirill Grouchnikov
09/07/2022, 1:01 PMrender
gets me the right visuals.Stefan Oltmann
09/07/2022, 2:38 PMStefan Oltmann
09/09/2022, 8:44 AMKirill Grouchnikov
09/09/2022, 3:04 PMStefan Oltmann
09/09/2022, 3:06 PM