Skip to content

Animatable

A value-based animation method for fine-grained control.

val offsetX = remember { Animatable(0f) }
offsetX.animateTo(
targetValue = maxWidth.toFloat(),
animationSpec = tween(durationMillis = 4000, easing = LinearEasing)
)

snapTo() - Set value instantly without animation

Section titled “snapTo() - Set value instantly without animation”
offsetX.snapTo(0f)
val offsetX = remember { Animatable(0f) }
Box(
Modifier
.offset { IntOffset(offsetX.value.roundToInt(), 0) }
.pointerInput(Unit) {
detectDragGestures(
onDragEnd = {
// Snap back to origin
coroutineScope.launch {
offsetX.animateTo(
targetValue = 0f,
animationSpec = spring()
)
}
},
onDrag = { change, dragAmount ->
change.consume()
coroutineScope.launch {
offsetX.snapTo(offsetX.value + dragAmount.x)
}
}
)
}
)