You can see how to rotate through jetpack compose
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Compose_BasicsTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
AnimateAsFloatContent()
}
}
}
}
}
@Composable
fun AnimateAsFloatContent(){
var isRotated by rememberSaveable {
mutableStateOf(false)
}
val rotationAngle by animateFloatAsState(
targetValue = if (isRotated) 360F else 0f,
animationSpec = tween(durationMillis = 2500)
)
Column(horizontalAlignment = Alignment.CenterHorizontally) {
Image(painter = painterResource(id = R.drawable.img), contentDescription = "fan",
modifier = Modifier
.rotate(rotationAngle)
.size(150.dp))
Button(onClick = {isRotated = !isRotated},
modifier = Modifier
.padding(top = 50.dp)
.width(200.dp)
) {
Text(text = "Rotate Fan")
}
}
}
@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
Compose_BasicsTheme {
AnimateAsFloatContent()
}
}