Christopher Mederos
04/12/2024, 3:25 AMonDrawWithContent
modifier to draw my composable to a Picture. I then convert that Picture to a bitmap, save as a PNG, and share the URI accordingly.
However, I haven't figured out how to draw the picture off-screen / invisible / somehow hidden in the parent composable. While the content is only drawn to the picture, I still have to preserve the total space of the needed canvas on the screen. Any ideas on how to work around this, or if there is a better overall approach? Thanks!Christopher Mederos
04/12/2024, 3:29 AMval picture = remember { Picture() }
Button(onClick = {share(picture)}) {
Text("Share")
}
Box(
modifier = Modifier.size(400.dp).drawWithCache {
val width = this.size.width.toInt()
val height = this.size.height.toInt()
onDrawWithContent {
val pictureCanvas =
androidx.compose.ui.graphics.Canvas(
picture.beginRecording(
width,
height
)
)
draw(this, this.layoutDirection, pictureCanvas, this.size) {
this@onDrawWithContent.drawContent()
}
picture.endRecording()
}
}
) {
SharedContent()
}
Nader Jawad
04/18/2024, 6:24 AM@Composable
@Composable
fun MyComposable(
modifier: Modifier,
content: @Composable () -> Unit
) {
val context = LocalContext.current
Box(modifier.drawWithCache {
val layer = obtainGraphicsLayer().apply {
record {
drawContent()
}
}
GlobalScope.launch(<http://Dispatchers.IO|Dispatchers.IO>) {
val imageBitmap = layer.toImageBitmap()
val outputStream = context.openFileOutput("MyGraphicsLayerImageBitmap.png",
Context.MODE_PRIVATE
)
// Create a bitmap from the graphicsLayer and save it to disk
imageBitmap.asAndroidBitmap().compress(Bitmap.CompressFormat.PNG, 100, outputStream)
}
onDrawWithContent {
// Draw the layer into the original destination
drawLayer(layer)
}
}) {
content()
}
}
Christopher Mederos
05/01/2024, 6:30 AMOnDrawContent
block in your example almost works for this goal. The content is indeed not drawn. However, the parent composable Box must still be a large enough size to contain the content. In this case, I end up with a blank space.
I can use another box to draw the blank space behind some other content that I do want to show. However, I'm curious if there is a more graceful alternative!Nader Jawad
05/11/2024, 9:44 PMChristopher Mederos
05/21/2024, 12:51 AMBox(
modifier = Modifier
.size(0.dp)
.drawWithCache {
graphicsLayer = obtainGraphicsLayer().apply {
record(size = IntSize(1080, 1080)) {
drawContent()
}
}
onDrawWithContent {}
}
) {
Surface {
Text("Share Me")
}
}
Christopher Mederos
05/21/2024, 1:52 AMBox(
modifier = Modifier
.size(0.dp)
.drawWithCache {
graphicsLayer = obtainGraphicsLayer().apply {
record(size = IntSize(400.dp.toPx().toInt(), 400.dp.toPx().toInt())) {
drawContent()
}
}
onDrawWithContent { }
}
) {
Box(
modifier = Modifier
.wrapContentHeight(unbounded = true, align = Alignment.Top)
.wrapContentWidth(unbounded = true, align = Alignment.Start)
.requiredSize(400.dp)
) {
Surface(modifier = Modifier.fillMaxSize()) {
Text("Share Me")
}
}
}
Nader Jawad
05/23/2024, 10:23 PMChristopher Mederos
05/28/2024, 5:47 AM