Gestengesteuerte Animationen
Steuern Sie Bewegungen durch Ziehen und Wischen.
Gestengesteuerte Animationen ist eine kostenlose Jetpack Compose Academy-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Jetpack Compose Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Jetpack Compose Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
Motion You Control by Touch
State-driven animations run on their own, but some motion should follow your finger. Gesture-driven animations let drags and swipes steer the movement. ✨
Track the Offset
Hold the current position in state. The most direct tool is the Animatable, which stores a value and can both jump and animate to it.
val offsetX = remember { Animatable(0f) }Read Drag Events
Use pointerInput with detectDragGestures to receive each drag delta as the user moves their finger across the element.
Modifier.pointerInput(Unit) { detectDragGestures { _, drag -> } }Follow the Finger
During a drag, call snapTo to move instantly to the new position. Snapping keeps the element glued to the finger with no lag.
scope.launch { offsetX.snapTo(offsetX.value + drag.x) }Apply the Offset
Feed the value into an offset modifier so the Composable physically moves. The view tracks every pixel of the drag in real time.
Modifier.offset { IntOffset(offsetX.value.roundToInt(), 0) }Animate on Release
When the finger lifts, switch from snap to animateTo so the element glides back home or to the nearest anchor smoothly.
scope.launch { offsetX.animateTo(0f) }Carry the Momentum
Capture fling velocity from onDragEnd and pass it to animateDecay. The element keeps coasting after release, just like real flicks. 🌀
offsetX.animateDecay(velocity, exponentialDecay())Swipe Between Anchors
For swipe-to-dismiss or sliders, snap to fixed stops. anchoredDraggable ties gestures to defined anchor points and settles between them.
Coroutines Drive It
Animatable updates are suspend functions, so launch them in a coroutine scope. Grab one with rememberCoroutineScope inside your Composable.
val scope = rememberCoroutineScope()Interruptible by Design
Because gestures and animations share the same Animatable, a new touch interrupts any running animation, handing control straight back to the user.
The Feel of Native Apps
Combining snap during drag with decay and spring on release is the secret to fluid, natural gestures that feel responsive and alive.
Quick Check
While the finger is dragging, which Animatable call keeps the element glued to the touch?
Recap: Touch Meets Motion
You combined pointerInput with Animatable: snapTo while dragging, then animateTo or animateDecay on release for fluid, gesture-driven motion. 🎉
Häufig gestellte Fragen
Ist die Lektion „Gestengesteuerte Animationen“ kostenlos?
Ja — der vollständige Text von „Gestengesteuerte Animationen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Jetpack Compose Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Jetpack Compose Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Gestengesteuerte Animationen“?
Steuern Sie Bewegungen durch Ziehen und Wischen. Du übst Jetpack Compose Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Jetpack Compose Academy zu starten?
Keine Vorkenntnisse erforderlich. Jetpack Compose Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „Gestengesteuerte Animationen“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Jetpack Compose Academy-Lektion Code schreiben und ausführen?
Ja. Jede Jetpack Compose Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- animate*AsState für einfache Tweens
- AnimatedVisibility und Crossfade
- updateTransition für koordinierte Bewegungen
- Gestengesteuerte Animationen