Skip to content

Instantly share code, notes, and snippets.

@cp-radhika-s
Created June 15, 2022 10:19
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
@Composable
fun SpringAnimation() {
val scope = rememberCoroutineScope()
val offset = remember { Animatable(Offset(0f, 0f), Offset.VectorConverter) }
val offset2 = remember { Animatable(Offset(0f, 0f), Offset.VectorConverter) }
val offset3 = remember { Animatable(Offset(0f, 0f), Offset.VectorConverter) }
Column(
modifier = Modifier
.fillMaxSize()
.padding(20.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Circle(modifier = Modifier
.offset { offset.value.toIntOffset() }
.background(ThemeColor, CircleShape)
.pointerInput(Unit) {
detectDragGestures { change, dragAmount ->
change.consumeAllChanges()
scope.launch {
offset.animateTo(
offset.value + change.position, spring(
dampingRatio = Spring.DampingRatioLowBouncy
)
)
}
scope.launch {
offset2.animateTo(
offset.value + change.position, spring(
dampingRatio = Spring.DampingRatioLowBouncy,
stiffness = 100f
)
)
}
scope.launch {
offset3.animateTo(
offset.value + change.position, spring(
dampingRatio = Spring.DampingRatioLowBouncy,
stiffness = StiffnessVeryLow
)
)
}
}
})
Spacer(modifier = Modifier.height(30.dp))
Circle(modifier = Modifier
.offset { offset2.value.toIntOffset() }
.background(Color.White.copy(0.8f), CircleShape))
Spacer(modifier = Modifier.height(30.dp))
Circle(modifier = Modifier
.offset { offset3.value.toIntOffset() }
.background(Color.White.copy(0.3f), CircleShape))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment