0

Blow the code, I want to set value to currentDegree on animate end, but when I do it, it recomposes. How to make it not recompose?

    @Composable
    fun RotateImageCompose(width: Dp, height: Dp, resId: Int, targetDegree: Float) {
        val currentDegree = remember { mutableStateOf(0f) }

        var diff = currentDegree.value - targetDegree
        .
        .
        .

        val dregree by animateFloatAsState(targetValue = diff, animationSpec = tween(
            easing = LinearEasing,
            durationMillis = 180
        ), finishedListener = {
            //this code cause the issue
            currentDegree.value = it
        })

        Image(
            painter = painterResource(id = resId),
            contentDescription = null,
            modifier = Modifier
                .rotate(dregree)
                .size(width, height)
        )
    }
user1419851
  • 131
  • 1
  • 1
  • 6

2 Answers2

0

Just use :

val currentDegree = remember {0f}

That way your value is immutable.

Please look here for a detailed explanation: https://stackoverflow.com/a/66178263/5513788

Code Poet
  • 6,222
  • 2
  • 29
  • 50
0

I save the value outside the compose

user1419851
  • 131
  • 1
  • 1
  • 6