Skip to content

Instantly share code, notes, and snippets.

@SEAbdulbasit
Created February 4, 2024 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SEAbdulbasit/726b455c52b9b824d18bcc3c56683212 to your computer and use it in GitHub Desktop.
Save SEAbdulbasit/726b455c52b9b824d18bcc3c56683212 to your computer and use it in GitHub Desktop.
Bouncing ball animation
@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)
}
}
}
@SEAbdulbasit
Copy link
Author

Screen.Recording.2024-02-04.at.4.58.41.PM.mov

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment