Zoltan Demant
12/06/2023, 6:03 AMBitmap
using Picture::beginRecording
, but if the content is animated the Bitmap
wont reflect the final stage of the animaion. If anyone has any ideas, please let me know. Code in 🧵Zoltan Demant
12/06/2023, 6:06 AMprivate fun Modifier.capture(
scope: CoroutineScope,
callback: (ImageBitmap) -> Unit,
): Modifier {
return drawWithCache {
val picture = Picture()
var job: Job? = null
val width = size.width.toInt()
val height = size.height.toInt()
onDrawWithContent {
val canvas = Canvas(picture.beginRecording(width, height))
draw(
density = this,
size = size,
layoutDirection = layoutDirection,
canvas = canvas,
block = {
this@onDrawWithContent.drawContent()
},
)
picture.endRecording()
job?.cancel()
job = scope.launch {
val bitmap = withContext(Default) {
picture.toBitmap().asImageBitmap()
}
callback(bitmap)
}
}
}
}
Usage:
Box(
modifier = modifier.capture(
scope = rememberCoroutineScope(),
callback = { bitmap ->
// Stored in mutableState, then rendered using Image(x)
},
),
content = content,
)
Zoltan Demant
12/07/2023, 9:57 AMZach Klippenstein (he/him) [MOD]
12/07/2023, 3:38 PMZach Klippenstein (he/him) [MOD]
12/07/2023, 3:39 PMNader Jawad
12/07/2023, 9:26 PMZach Klippenstein (he/him) [MOD]
12/07/2023, 10:01 PMZoltan Demant
12/08/2023, 7:37 AMZoltan Demant
12/08/2023, 11:23 AM@Composable () -> Unit
which triggers the capture logic to recompose as well.Zoltan Demant
12/08/2023, 11:30 AM@NonRestartableComposable
on my capture composable makes the logic "work", but it also recomposes endlessly, so theres that.