Capturable
is a composable that allows you to (capture / take snapshot of / take screenshot of) any composable.
val captureState = rememberCaptureState() // Returns MutableState<CaptureState?>
Enclose the composable to be captured inside the Capturable
composable :
@Composable
fun Capturable(
state: MutableState<CaptureState?>,
content: @Composable () -> Unit
)
Example :
Capturable(state = captureState) {
CountryCard(
country = Country("IN", "Bharat", "Asia")
)
}
Everything inside the content composable will be captured.
To capture the passed composable as Bitmap
, invoke the captureState.capture()
function :
fun MutableState<CaptureState?>.capture(
callback: (Bitmap) -> Unit // Use the bitmap inside this lambda
)
Example :
Button(
onClick = {
captureState.capture { bitmap ->
saveAndShareImage(bitmap)
}
}
) {
Text(text = "Capture & Share")
}