Ali Kabiri
08/18/2021, 1:03 PMdbaelz
08/18/2021, 2:07 PMChachako
08/18/2021, 3:24 PMAli Kabiri
08/18/2021, 3:53 PMAli Kabiri
08/18/2021, 3:57 PMChachako
08/18/2021, 4:26 PMcomposeView.draw
does not get the latest appearance, like: https://kotlinlang.slack.com/archives/CJLTWPH7S/p1629271246339900?thread_ts=1629193140.247600&cid=CJLTWPH7SAli Kabiri
08/18/2021, 5:01 PMChachako
08/18/2021, 5:13 PMBitmap(..).applyCanvas { canvas ->
LocalView.current.draw(canvas)
}
and then you need to crop the bitmap according to the region of the widget. This is not a good solution, but it may be the only one at present 🙏Ali Kabiri
08/18/2021, 5:16 PMChachako
08/18/2021, 5:20 PMAbdalla Hassanin
08/18/2021, 6:44 PMAli Kabiri
08/18/2021, 7:48 PMval view = LocalView.current
view.measure(View.MeasureSpec.EXACTLY, View.MeasureSpec.EXACTLY)
val bmp = Bitmap.createBitmap(
view.measuredWidth,
view.measuredHeight,
Bitmap.Config.ARGB_8888).applyCanvas {
LocalView.current.draw(this)
}
bmp.let {
File(LocalContext.current.filesDir, "screenshot.png")
.writeBitmap(bmp, Bitmap.CompressFormat.PNG, 85)
}
...
private fun File.writeBitmap(bitmap: Bitmap, format: Bitmap.CompressFormat, quality: Int) {
outputStream().use { out ->
bitmap.compress(format, quality, out)
out.flush()
}
}
which follows by the following error:
Process: com.etrusted.android.mars.debug, PID: 11369
java.lang.IllegalArgumentException: width and height must be > 0
Tash
08/18/2021, 8:20 PMAli Kabiri
08/18/2021, 8:38 PMAli Kabiri
08/18/2021, 9:09 PMval view = LocalView.current
val context = LocalContext.current
val handler = Handler(Looper.getMainLooper())
handler.postDelayed(Runnable {
val bmp = Bitmap.createBitmap(view.width, view.height,
Bitmap.Config.ARGB_8888).applyCanvas {
view.draw(this)
}
bmp.let {
File(context.filesDir, "screenshot.png")
.writeBitmap(bmp, Bitmap.CompressFormat.PNG, 85)
}
}, 1000)
a 1-second delay is applied by the runner to wait for view to finishe measuring.
this way, the view.width
and `view.height`return correctly
Stack Overflow:
https://stackoverflow.com/a/68839490/3481187Tash
08/18/2021, 9:18 PMBoxWithConstraints
can be used where you can get the container’s width/height to pass down to createBitmap
Ali Kabiri
08/19/2021, 9:00 AMChachako
08/19/2021, 9:28 AMModifier.onGloballyPositioned {it.boundsInRoot().. }
, and then use them to crop the bitmap. In addition, yesterday I seem to have thought of a better way to take a screenshot of a single composable. I am going to create a demo to try it out. I will publish it here if it goes well 🤯.Ali Kabiri
08/19/2021, 3:28 PMAli Kabiri
08/26/2021, 12:26 PMChachako
08/28/2021, 11:18 AMAli Kabiri
08/30/2021, 1:23 PM