https://kotlinlang.org logo
v

Vladas

09/30/2021, 9:24 AM
Is there any way for fillMaxSize to work with rotation. It works fine when view is a lot smaller than than device width and height but as it gets larger it starts to deform. I tried to do it with BoxWithConstraints and flip between width and height but it is still deforming when rotated.
My code
Copy code
@Composable
private fun RotatableImage() {
    var rotated by remember { mutableStateOf(false) }
    val degrees = remember(rotated) {
        if (rotated) 90f
        else 0f
    }

    BoxWithConstraints(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.Gray), contentAlignment = Alignment.Center
    ) {
        val width: Dp
        val height: Dp

        if (rotated) {
            width = maxHeight
            height = maxWidth
        } else {
            width = maxWidth
            height = maxHeight
        }
//I use Image instead of Box
        Box(modifier = Modifier
            .rotate(degrees)
            .background(Color.Red)
            .width(width)
            .height(height)
//            .width(width/3) //this works but it is too small
//            .height(height/3)

            .clickable {
                rotated = !rotated
            }
        )
    }
}
After rotation.
z

Zach Klippenstein (he/him) [MOD]

09/30/2021, 4:40 PM
Are you sure the constraints being passed to BoxWithConstraints are actually the full size of the screen?
14 Views