Roar Gronmo
12/03/2019, 8:19 AM/*
Draws an Image in a sized container with image's aspectratio not altered,
as in xml's android:scaleType="fitCenter"
*/
@Composable
fun DrawImageFitCenter(
image: Image,
tint: Color? = null,
containerWidth: Dp,
containerHeight: Dp
){
val imageWidth = image.width.dp
val imageHeight = image.height.dp
val imageAspect = imageWidth.div(imageHeight)
val containerAspect = containerWidth.div(containerHeight)
var justWidth = 0.0.dp
var justHeight = 0.0.dp
Container(
width = containerWidth,
height = containerHeight
) {
if(containerAspect>imageAspect)
{
justWidth = containerWidth.times(imageAspect/containerAspect)
justHeight = containerHeight
} else {
justWidth = containerWidth
justHeight = containerHeight.times(containerAspect/imageAspect)
}
Container(
width = justWidth,
height = justHeight
) {
DrawImage(
image = image,
tint = tint
)
}
}
}
And of course, it is an item for simplifying...
RG