Compose Animation Interview Questions
UI › Compose UI
What animation APIs does Compose offer, and which would you use for simple cases?
What a strong answer covers: High-level: AnimatedVisibility for showing and hiding, AnimatedContent and Crossfade for switching content, animateContentSize for size changes. Value-based: animate*AsState (animateFloatAsState, animateColorAsState) to animate one value towards a target. Lower level: updateTransition for several values together, rememberInfiniteTransition, and Animatable for imperative coroutine-driven animations.
How does animate*AsState work, and when is it not enough?
What a strong answer covers: You give it a target value; whenever the target changes, it animates from the current value to the new one with the given animationSpec, and returns State that recomposes the reader each frame. It's not enough when you need to coordinate several values (use updateTransition), start an animation from an event or chain animations (Animatable with coroutines), follow a gesture, or run indefinitely.
What's the difference between tween, spring and keyframes animation specs?
What a strong answer covers: tween animates over a fixed duration with an easing curve. spring is physics-based, with damping ratio and stiffness, has no fixed duration, and handles interruptions smoothly by keeping velocity, which is why it's the default. keyframes specifies values at particular times within a duration. snap jumps straight to the end.
How would you make an animation that follows a drag gesture and then settles or flings to a position?
What a strong answer covers: Use an Animatable for the offset. In pointerInput with detectDragGestures (or Modifier.draggable), call animatable.snapTo(current + delta) as the finger moves while a VelocityTracker records velocity. On release, launch animateTo(target, initialVelocity = velocity) with a spring, or animateDecay for a fling; picking the target from position plus velocity (AnchoredDraggable handles this). Read the offset in Modifier.offset { } to avoid recomposition.
An animation runs smoothly in debug on your device but stutters on low-end devices. What would you look at?
What a strong answer covers: First test a release build with R8 and Baseline Profiles, because debug Compose is much slower. Then check whether animated values are read in composition (causing recomposition every frame) instead of in the layout or draw phase through lambda modifiers or graphicsLayer; animating layout-affecting properties like size, which forces relayout; overdraw; and allocations in the frame. Use Perfetto system traces and frame timing to see which phase is over budget.