Question
Creating compose smooth stopping rotation animation
I am trying to create a smooth stopping animation. When the user clicks on the icon it will rotate. Once its fetched from the Endpoint it will stop.
However, I am controlling this by resetting the angle to 0f. So when it stops it will jump to that 0f. This makes starting smooth as it will also start from 0f.
I am thinking the angle should be remembered so whatever angle its stops at that should be the angle it starts from again. Or is there a better way to handle starting and stopping?
@Composable
fun RatesStatus(
ratesStatus: RateStatus,
onRateRefreshClicked: () -> Unit,
isFetchingNewRates: Boolean = false
) {
val infiniteTransition = rememberInfiniteTransition()
val angle by infiniteTransition.animateFloat(
initialValue = 0F,
targetValue = -360F,
animationSpec = infiniteRepeatable(
animation = tween(2000, easing = LinearEasing)
)
)
Row(
modifier = Modifier.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween
) {
Row {
Image(
modifier = Modifier.size(50.dp),
painter = painterResource(Res.drawable.exchange_illustration),
contentDescription = "Exchange rate illustration"
)
Spacer(modifier = Modifier.width(12.dp))
Column {
Text(
text = displayCurrentDateTime(),
color = Color.White
)
Text(
text = stringResource(ratesStatus.title),
color = ratesStatus.color,
fontSize = MaterialTheme.typography.bodySmall.fontSize
)
}
}
IconButton(
onClick = onRateRefreshClicked
) {
Icon(
modifier = Modifier
.size(24.dp)
.graphicsLayer {
this.rotationZ = if(isFetchingNewRates) angle else 0f
},
painter = painterResource(Res.drawable.refresh_ic),
contentDescription = stringResource(Res.string.start_refresh),
tint = staleColor
)
}
}
}