How to do my image to rotate infinitely?
This is my code but animation does not work
val angle: Float by animateFloatAsState(
targetValue = 360F,
animationSpec = infiniteRepeatable(
tween(2000))
)
Image(
painter = painterResource(R.drawable.sonar_scanner),
"image",
Modifier
.fillMaxSize()
.rotate(angle),
contentScale = ContentScale.Fit
)
You can use the InfiniteTransition
using rememberInfiniteTransition
.
Something like
val infiniteTransition = rememberInfiniteTransition()
val angle by infiniteTransition.animateFloat(
initialValue = 0F,
targetValue = 360F,
animationSpec = infiniteRepeatable(
animation = tween(2000, easing = LinearEasing)
)
)
Just a note.
Instead of using
Modifier.rotate(angle)
You can use
Modifier
.graphicsLayer {
rotationZ = angle
}
As you can check in the doc:
Prefer this version when you have layer properties backed by a
androidx.compose.runtime.State
or an animated value as reading a state insideblock
will only cause the layer properties update without triggering recomposition and relayout.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With