7

I tried animations in Jetpack Compose. I'm facing problem in rotate animation.

It seems everything fine. But don't know why it's not working.

My code:

     @Composable
    private fun RotateAnimationContent() {
        val isRotated = rememberSaveable { mutableStateOf(false) }
        val rotationAngle by animateFloatAsState(
            targetValue = if (isRotated.value) 360F else 0F,
            animationSpec = tween(durationMillis = 1500,easing = FastOutLinearInEasing)

        )
        Column {
            Box(modifier = Modifier.background(Color.Red).size(100.dp).rotate(rotationAngle))
            Button(
                onClick = { isRotated.value = !isRotated.value },
                modifier = Modifier.padding(10.dp)
            ) {
                Text(text = "Rotate Box")
            }
        }
    }
Stefano Sansone
  • 2,377
  • 7
  • 20
  • 39
Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159

1 Answers1

6

Put the .rotate(rotationAngle) as the first modifier of the Box. Before background and size.

Column {
    Box(modifier = Modifier.rotate(rotationAngle).background(Color.Red).size(100.dp))
    Button(
        onClick = { isRotated.value = !isRotated.value },
        modifier = Modifier.padding(10.dp)
    ) {
        Text(text = "Rotate Box")
    }
}

Check this very detailed answer to better understand why the order of modifiers matters.

Stefano Sansone
  • 2,377
  • 7
  • 20
  • 39