Class

SeekableTransitionState

A TransitionState that can manipulate the progress of the Transition by seeking with seekTo or animating with animateTo.

SeekingAnimationSample

@Sampled
@Composable
fun SeekingAnimationSample() {
    Column {
        val seekingState = remember { SeekableTransitionState(BoxSize.Small) }
        val scope = rememberCoroutineScope()
        Column {
            Row {
                Button(
                    onClick = { scope.launch { seekingState.animateTo(BoxSize.Small) } },
                    Modifier.wrapContentWidth().weight(1f),
                ) {
                    Text("Animate Small")
                }
                Button(
                    onClick = { scope.launch { seekingState.seekTo(0f, BoxSize.Small) } },
                    Modifier.wrapContentWidth().weight(1f),
                ) {
                    Text("Seek Small")
                }
                Button(
                    onClick = { scope.launch { seekingState.seekTo(0f, BoxSize.Medium) } },
                    Modifier.wrapContentWidth().weight(1f),
                ) {
                    Text("Seek Medium")
                }
                Button(
                    onClick = { scope.launch { seekingState.seekTo(0f, BoxSize.Large) } },
                    Modifier.wrapContentWidth().weight(1f),
                ) {
                    Text("Seek Large")
                }
                Button(
                    onClick = { scope.launch { seekingState.animateTo(BoxSize.Large) } },
                    Modifier.wrapContentWidth().weight(1f),
                ) {
                    Text("Animate Large")
                }
            }
        }
        Slider(
            value = seekingState.fraction,
            modifier = Modifier.systemGestureExclusion().padding(10.dp),
            onValueChange = { value -> scope.launch { seekingState.seekTo(fraction = value) } },
        )
        val transition = rememberTransition(seekingState)

        val scale: Float by
            transition.animateFloat(
                transitionSpec = { tween(easing = LinearEasing) },
                label = "Scale",
            ) { state ->
                when (state) {
                    BoxSize.Small -> 1f
                    BoxSize.Medium -> 2f
                    BoxSize.Large -> 3f
                }
            }

        transition.AnimatedContent(
            transitionSpec = {
                fadeIn(tween(easing = LinearEasing)) togetherWith
                    fadeOut(tween(easing = LinearEasing))
            }
        ) { state ->
            if (state == BoxSize.Large) {
                Box(Modifier.size(50.dp).background(Color.Magenta))
            } else {
                Box(Modifier.size(50.dp))
            }
        }
        Box(
            Modifier.fillMaxSize()
                .wrapContentSize(Alignment.Center)
                .size(100.dp)
                .graphicsLayer {
                    scaleX = scale
                    scaleY = scale
                }
                .background(Color.Blue)
        )
    }
}
Jetpack Compose beyond MaterialFree components, screens and templates for apps that don’t look like Google’s. Copy them into your app, or let your coding agent add them.Browse for free

Content updated: