eenriquelopez
04/08/2025, 12:21 PMval fakeCanvas = Canvas()
val composeView =
ComposeView(parent.context)
.apply {
layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
)
setParentCompositionContext(compositionContext)
setContent(content)
}
.also(parent::addView)
composeView.draw(fakeCanvas)
composeView.measure(measureSpec, measureSpec)
At this point, composeView.measureWidth
or measuredHeight
is zero. This only happens during the Preview, not when I am using the Composable itself. What happens differently during the preview process?romainguy
04/08/2025, 5:06 PMeenriquelopez
04/08/2025, 5:40 PMromainguy
04/08/2025, 5:43 PMeenriquelopez
04/08/2025, 5:47 PMcomposeView.draw(fakeCanvas)
composeView.measure(measureSpec, measureSpec)
// Here I see the size is already for width and height
composeView.layout(0, 0, composeView.measuredWidth, composeView.measuredHeight)
val bitmap =
Bitmap
.createBitmap(
composeView.measuredWidth,
composeView.measuredHeight,
Bitmap.Config.ARGB_8888,
)
bitmap.applyCanvas { composeView.draw(this) }
parent.removeView(composeView)
return BitmapDescriptorFactory.fromBitmap(bitmap)
romainguy
04/08/2025, 5:58 PMromainguy
04/08/2025, 5:59 PMromainguy
04/08/2025, 6:00 PMeenriquelopez
04/08/2025, 6:02 PMromainguy
04/08/2025, 6:13 PMromainguy
04/08/2025, 6:13 PMromainguy
04/08/2025, 6:14 PMeenriquelopez
04/08/2025, 6:38 PMromainguy
04/08/2025, 6:57 PM