How to scale icon width by height in Jetpack Compose

Viewed 625

I am trying to scale icon width by height. I was trying to add contentScale modifier, but it doesn't know it.

Icon(
           painter = painterResource(id = id),
           contentDescription = null,
           tint = Color.White,
           //contentScale = ContentScale.Fit,
           modifier = Modifier .fillMaxHeight(0.6f)
                               .align(CenterVertically).border(2.dp,Color.Black, RectangleShape)
    )

Picture how it looks - The icon on the left should be scaled to fit height of the black box: The icon on the left should be scaled to fit height of the black box

1 Answers

Ensuring your icon is a square should be sufficient.

Modifier
    .fillMaxHeight(0.6f)
    .aspectRatio(1f) // make the width same as the height
    ... // your other modifiers
Related