Created
February 4, 2024 11:59
-
-
Save SEAbdulbasit/726b455c52b9b824d18bcc3c56683212 to your computer and use it in GitHub Desktop.
Bouncing ball animation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun BouncingBall() { | |
val scope = rememberCoroutineScope() | |
val animatable = remember { Animatable(0f) } | |
var targetHeight by remember { mutableStateOf(1f) } | |
LaunchedEffect(key1 = true) { | |
scope.launch { | |
while (targetHeight > 0.01f) { // the animation will stop when the height is less than 1% of maxHeight | |
targetHeight *= 0.8f | |
animatable.animateTo( | |
targetValue = targetHeight, | |
animationSpec = tween( | |
durationMillis = 1000, | |
easing = LinearOutSlowInEasing | |
) | |
) | |
animatable.animateTo( | |
targetValue = 0f, | |
animationSpec = tween( | |
durationMillis = 1000, | |
easing = LinearOutSlowInEasing | |
) | |
) // it goes to the top smoothly | |
} | |
} | |
} | |
val maxHeight = 300 | |
val height = animatable.value * maxHeight | |
Box( | |
Modifier | |
.fillMaxSize() | |
.background(Color.LightGray) | |
) { | |
Canvas(modifier = Modifier.offset(y = height.dp)) { | |
drawCircle(color = Color.Blue, radius = 50f) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Screen.Recording.2024-02-04.at.4.58.41.PM.mov