rob42
06/17/2024, 1:04 PMfun Modifier.scaleContentToFit(): Modifier = composed {
var frameSize by remember { mutableStateOf<Size?>(null) }
this
.graphicsLayer {
frameSize = size
}
.wrapContentSize(unbounded = true)
.graphicsLayer {
frameSize?.let { frameSize ->
// Scale that fits graphicsLayer.size fully inside frameSize
val scale = min(
frameSize.width / size.width,
frameSize.height / size.height)
this.scaleX = scale
this.scaleY = scale
}
}
}
Albert Chang
06/17/2024, 3:47 PMModifier.layout { measurable, constraints ->
val placeable = measurable.measure(Constraints())
val (width, height) = constraints.constrain(IntSize(placeable.width, placeable.height))
layout(width, height) {
placeable.placeWithLayer(
x = (width - placeable.width) / 2,
y = (height - placeable.height) / 2
) {
// Set scaleX and scaleY
}
}
}
rob42
06/17/2024, 5:47 PM