Share
Embed
Transcript
162 slides
-
Program 1. Layout Animation Basics 2. Composable Animation APIs 3.
Animations Customization -
1. Layout Animation Basics 2. Composable Animation APIs 3. Animations
Customization 4. Random shapes! Program -
States var viewColor by remember { mutableStateOf(Color.Blue) } var viewScale
by remember { mutableStateOf(0.5f) } -
States var viewColor by remember { mutableStateOf(Color.Blue) } var viewScale
by remember { mutableStateOf(0.5f) } Box( modifier = Modifier .scale(viewScale) .background(viewColor) ) -
Layout Animations - What do we need? 1. States 2.
Animation APIs -
Composable APIs Composable functions that handle all the animation logic
AnimationComposable() { AmazingUI( ... ) } -
Composable APIs Composable functions that handle all the animation logic
AnimationComposable() { <- Does all the work! AmazingUI( ... ) } -
Composable APIs Composable functions that handle all the animation logic
AnimationComposable(targetState = state) { <- Does all the work! AmazingUI( ... ) } -
What do we need for a great show? 1. Curtain
2. Conductor 3. Performers -
Animated Visibility var isVisible by remember { mutableStateOf(true) } AnimatedVisibility(
visible = isVisible, ) { ... } -
Animated Visibility - MORE! - FUN! @Composable fun AnimatedVisibility( visible:
Boolean, modifier: Modifier = Modifier, enter: EnterTransition = fadeIn() + expandVertically(), exit: ExitTransition = fadeOut() + shrinkVertically(), label: String = "AnimatedVisibility", content: @Composable AnimatedVisibilityScope.() -> Unit ) { val transition = updateTransition(visible, label) AnimatedEnterExitImpl(transition, { it }, ... ) } -
Animated Visibility - MORE! @Composable fun AnimatedVisibility( visible: Boolean, modifier:
Modifier = Modifier, enter: EnterTransition = fadeIn() + expandVertically(), exit: ExitTransition = fadeOut() + shrinkVertically(), label: String = "AnimatedVisibility", content: @Composable AnimatedVisibilityScope.() -> Unit ) { val transition = updateTransition(visible, label) AnimatedEnterExitImpl(transition, { it }, ... ) } -
Transitions Define how a state change is animated Contain: •
The current state • The target state • The animation(s) to play -
Transitions - Enter & Exit Define how a composable will
appear or disappear in the screen -
Transitions - Enter & Exit var isVisible by remember {
mutableStateOf(true) } AnimatedVisibility( visible = isVisible, ) { ... } -
Transitions - Enter & Exit var isVisible by remember {
mutableStateOf(true) } AnimatedVisibility( visible = isVisible, enter = slideInHorizontally(), exit = slideOutHorizontally(), ) { ... } -
Transitions - Enter & Exit var isVisible by remember {
mutableStateOf(true) } AnimatedVisibility( visible = isVisible, enter = scaleIn(), exit = scaleOut(), ) { ... } -
Transitions - Enter & Exit var isVisible by remember {
mutableStateOf(true) } AnimatedVisibility( visible = isVisible, enter = scaleIn() + slideInHorizontally(), exit = scaleOut() + slideOutHorizontally(), ) { ... } -
Transitions - MORE! @Composable fun AnimatedVisibility( visible: Boolean, modifier: Modifier
= Modifier, enter: EnterTransition = fadeIn() + expandVertically(), exit: ExitTransition = fadeOut() + shrinkVertically(), label: String = "AnimatedVisibility", content: @Composable AnimatedVisibilityScope.() -> Unit ) { val transition = updateTransition(visible, label) AnimatedEnterExitImpl(transition, { it }, ... ) } -
Transitions - MORE! @Composable fun AnimatedVisibility( visible: Boolean, modifier: Modifier
= Modifier, enter: EnterTransition = fadeIn() + expandVertically(), exit: ExitTransition = fadeOut() + shrinkVertically(), label: String = "AnimatedVisibility", content: @Composable AnimatedVisibilityScope.() -> Unit ) { val transition = updateTransition(visible, label) AnimatedEnterExitImpl(transition, { it }, ... ) } -
Transitions - MORE! AnimatedVisibility(...) { val color by transition.animateColor() {
tState -> if (tState == EnterExitState.Visible) { TileColor.Green } else { TileColor.Red } } } -
Transitions - MORE! AnimatedVisibility(...) { val color by transition.animateColor() {
tState -> if (tState == EnterExitState.Visible) { TileColor.Green } else { TileColor.Red } } ColorScreen(color = color) { Text(text = "I'm visible! :D") } } -
Transitions - MORE! AnimatedVisibility(...) { val color by transition.animateColor() {
... } val progress by transition.animateFloat(){ tState -> if (tState == EnterExitState.Visible) 1f else 0f } ColorScreen(color = color) { Text(text = if (progress == 1f) "I'm visible! :D" else "D:" ) } } -
Transitions - MORE! AnimatedVisibility(...) { val color by transition.animateColor() {
... } val progress by transition.animateFloat(){...} val roundCorners by transition.animateDp { state -> if (state == EnterExitState.Visible) 0.dp else 500.dp } ColorScreen(Modifier.clip(RoundedCornerShape(...))) { Text(text = if (progress == 1f) "I'm visible! :D" else "D:" ) } } -
Crossfade @Composable fun <T> Crossfade( targetState: T, modifier: Modifier =
Modifier, animationSpec: FiniteAnimationSpec<Float> = tween(), content: @Composable (T) -> Unit ) { val transition = updateTransition(targetState) transition.Crossfade(modifier, animationSpec, content = content) } -
Crossfade @Composable fun <T> Crossfade( targetState: T, modifier: Modifier =
Modifier, animationSpec: FiniteAnimationSpec<Float> = tween(), content: @Composable (T) -> Unit ) { val transition = updateTransition(targetState) transition.Crossfade(modifier, animationSpec, content = content) } -
Crossfade @Composable fun <T> Crossfade( targetState: T, modifier: Modifier =
Modifier, animationSpec: FiniteAnimationSpec<Float> = tween(), content: @Composable (T) -> Unit ) { val transition = updateTransition(targetState) transition.Crossfade(modifier, animationSpec, content = content) } -
Crossfade enum class Screen { ScreenOne, ScreenTwo, } var screen
by remember { mutableStateOf(ScreenOne) } -
Crossfade var screen by remember { mutableStateOf(ScreenOne) } Crossfade( targetState
= screen, ) { state -> when (state) { ScreenOne -> ScreenOne() ScreenTwo -> ScreenTwo() } } -
Crossfade var screen by remember { mutableStateOf(ScreenOne) } Crossfade( targetState
= screen, ) { state -> when (state) { ScreenOne -> ScreenOne() ScreenTwo -> ScreenTwo() } } -
Crossfade @Composable fun <T> Crossfade( targetState: T, modifier: Modifier =
Modifier, animationSpec: FiniteAnimationSpec<Float> = tween(), content: @Composable (T) -> Unit ) { val transition = updateTransition(targetState) transition.Crossfade(modifier, animationSpec, content = content) } -
Crossfade @Composable fun <T> Crossfade( targetState: T, modifier: Modifier =
Modifier, animationSpec: FiniteAnimationSpec<Float> = tween(), content: @Composable (T) -> Unit ) { val transition = updateTransition(targetState) transition.Crossfade(modifier, animationSpec, content = content) } -
Crossfade @Composable fun <T> Crossfade( targetState: T, modifier: Modifier =
Modifier, animationSpec: FiniteAnimationSpec<Float> = tween(), content: @Composable (T) -> Unit ) { val transition = updateTransition(targetState) transition.Crossfade(modifier, animationSpec, content = content) } -
AnimationSpec Defines how an animation runs. 1. Transitions ◦ Define
the general path to follow (target state) -
AnimationSpec Defines how an animation runs. 1. Transitions ◦ Define
the general path to follow (target state) ▪ Ej: slide to top left -
AnimationSpec Defines how an animation runs. 1. Transitions ◦ Define
the general path to follow (target state) ▪ Ej: slide to top left 2. AnimationSpec ◦ Define how to follow that path -
AnimationSpec Defines how an animation runs. 1. Transitions ◦ Define
the general path to follow (target state) ▪ Ej: slide to top left 2. AnimationSpec ◦ Define how to follow that path ▪ Ej: Go straight to the point -
AnimationSpec Defines how an animation runs. 1. Transitions ◦ Define
the general path to follow (target state) ▪ Ej: slide to top left 2. AnimationSpec ◦ Define how to follow that path ▪ Ej: Go straight to the point ▪ Ej: First go super fast and then super slow -
AnimationSpec Defines how an animation runs. 1. Transitions ◦ Define
the general path to follow (target state) ▪ Ej: slide to top left 2. AnimationSpec ◦ Define how to follow that path ▪ Ej: Go straight to the point ▪ Ej: First go super fast and then super slow ▪ Ej: Take a detour -
Crossfade var screen by remember { mutableStateOf(ScreenOne) } Crossfade( targetState
= screen, ) { state -> when (state) { ScreenOne -> ScreenOne() ScreenTwo -> ScreenTwo() } } -
Crossfade var screen by remember { mutableStateOf(ScreenOne) } Crossfade( targetState
= screen, animationSpec = tween(durationMillis = 2000), ) { state -> when (state) { ScreenOne -> ScreenOne() ScreenTwo -> ScreenTwo() } } -
Crossfade var screen by remember { mutableStateOf(ScreenOne) } Crossfade( targetState
= screen, animationSpec = snap(), ) { state -> when (state) { ScreenOne -> ScreenOne() ScreenTwo -> ScreenTwo() } } -
Crossfade var screen by remember { mutableStateOf(ScreenOne) } Crossfade( targetState
= screen, animationSpec = spring(), ) { state -> when (state) { ScreenOne -> ScreenOne() ScreenTwo -> ScreenTwo() } } -
Animated Content @Composable fun <S> AnimatedContent( targetState: S, modifier: Modifier
= Modifier, transitionSpec: AnimatedContentScope<S>.() -> ContentTransform = { fadeIn() + scaleIn() with fadeOut() }, contentAlignment: Alignment = Alignment.TopStart, content: @Composable() AnimatedVisibilityScope.(targetState: S) -> Unit ) { val transition = updateTransition(targetState = targetState) transition.AnimatedContent( ... ) } -
Animated Content @Composable fun <S> AnimatedContent( targetState: S, modifier: Modifier
= Modifier, transitionSpec: AnimatedContentScope<S>.() -> ContentTransform = { fadeIn() + scaleIn() with fadeOut() }, contentAlignment: Alignment = Alignment.TopStart, content: @Composable() AnimatedVisibilityScope.(targetState: S) -> Unit ) { val transition = updateTransition(targetState = targetState) transition.AnimatedContent( ... ) } -
Animated Content var isVisible by remember { mutableStateOf(true) } AnimatedContent(
targetState = isVisible, ) { ... } -
Animated Content var isVisible by remember { mutableStateOf(true) } AnimatedContent(
targetState = isVisible, ) { state -> if (state) { ColorScreen( ... ){ ... } } } -
Animated Content var isVisible by remember { mutableStateOf(true) } AnimatedContent(
targetState = isVisible, ) { state -> if (state) { ColorScreen( ... ){ ... } } } -
Transitions - Enter & Exit @Composable fun <S> AnimatedContent( targetState:
S, modifier: Modifier = Modifier, transitionSpec: AnimatedContentScope<S>.() -> ContentTransform = { fadeIn() + scaleIn() with fadeOut() }, contentAlignment: Alignment = Alignment.TopStart, content: @Composable() AnimatedVisibilityScope.(targetState: S) -> Unit ) { val transition = updateTransition(targetState = targetState) transition.AnimatedContent( ... ) } -
Transitions - Enter & Exit @Composable fun <S> AnimatedContent( targetState:
S, modifier: Modifier = Modifier, transitionSpec: AnimatedContentScope<S>.() -> ContentTransform = { fadeIn() + scaleIn() with fadeOut() }, contentAlignment: Alignment = Alignment.TopStart, content: @Composable() AnimatedVisibilityScope.(targetState: S) -> Unit ) { val transition = updateTransition(targetState = targetState) transition.AnimatedContent( ... ) } -
Transitions - Enter & Exit @Composable fun <S> AnimatedContent( targetState:
S, modifier: Modifier = Modifier, transitionSpec: AnimatedContentScope<S>.() -> ContentTransform = { fadeIn() + scaleIn() with fadeOut() }, contentAlignment: Alignment = Alignment.TopStart, content: @Composable() AnimatedVisibilityScope.(targetState: S) -> Unit ) { val transition = updateTransition(targetState = targetState) transition.AnimatedContent( ... ) } -
Transitions - Enter & Exit @Composable fun <S> AnimatedContent( targetState:
S, modifier: Modifier = Modifier, transitionSpec: AnimatedContentScope<S>.() -> ContentTransform = { enterTransition() with fadeOut() }, contentAlignment: Alignment = Alignment.TopStart, content: @Composable() AnimatedVisibilityScope.(targetState: S) -> Unit ) { val transition = updateTransition(targetState = targetState) transition.AnimatedContent( ... ) } -
Transitions - Enter & Exit @Composable fun <S> AnimatedContent( targetState:
S, modifier: Modifier = Modifier, transitionSpec: AnimatedContentScope<S>.() -> ContentTransform = { enterTransition() with exitTransition() }, contentAlignment: Alignment = Alignment.TopStart, content: @Composable() AnimatedVisibilityScope.(targetState: S) -> Unit ) { val transition = updateTransition(targetState = targetState) transition.AnimatedContent( ... ) } -
Transitions - Enter & Exit var isVisible by remember {
mutableStateOf(true) } AnimatedContent( targetState = isVisible, ) { state -> if (state) { ColorScreen( ... ){ ... } } } -
Transitions - Enter & Exit var isVisible by remember {
mutableStateOf(true) } AnimatedContent( targetState = isVisible, transitionSpec = { fadeIn() + expandVertically() with fadeOut() + shrinkVertically() }, ) { state -> if (state) { ColorScreen( ... ){ ... } } } -
Performer 1: The Curtain! • State: Visibility Boolean • Fade
& expand transitions • It good curtain -
Performer 2: The Clicker var isVisible by remember { mutableStateOf(true)
} AnimatedContent( targetState = isVisible, ) { ... } -
Performer 2: The Clicker var isVisible by remember { mutableStateOf(true)
} AnimatedContent( targetState = isVisible, ) { ... } -
Performer 2: The Clicker var number by remember { mutableStateOf(0)
} AnimatedContent( targetState = number, ) { ... } -
Performer 2: The Clicker var number by remember { mutableStateOf(0)
} AnimatedContent( targetState = number, ) { state -> Text(text = "$state") } -
Performer 2: The Clicker var number by remember { mutableStateOf(0)
} AnimatedContent( targetState = number, ) { state -> Text(text = "$state") } -
Performer 2: The Clicker var number by remember { mutableStateOf(0)
} AnimatedContent( targetState = number, ) { state -> Text(text = "$state").background(...) } -
Performer 2: The Clicker var number by remember { mutableStateOf(0)
} AnimatedContent( targetState = number, transitionSpec = { slideIn { IntOffset(it.width / 2, it.height / 2) } with slideOut { IntOffset(-it.width, -it.height) } }, ) { state -> Text(text = "$state").background(...) } -
Performer 2: The Clicker var number by remember { mutableStateOf(0)
} AnimatedContent( targetState = number, transitionSpec = { slideIn { IntOffset(it.width / 2, it.height / 2) } with slideOut { IntOffset(-it.width, -it.height) } using SizeTransform(clip = false) }, ) { state -> Text(text = "$state").background(...) } -
Performer 2: The Clicker var number by remember { mutableStateOf(0)
} AnimatedContent( targetState = number, transitionSpec = { slideIn { IntOffset(it.width / 2, it.height / 2) } with slideOut { IntOffset(-it.width, -it.height) } using SizeTransform(clip = false) }, ) { state -> Text(text = "$state").background(...) } -
Performer 2: The Clicker! • State: counter Int • Slide
transition • SizeTransform to remove clipping -
SizeTransform Defines how a size change happens. • Enter &
Exit transitions take care of visibility • SizeTransform takes care of size changes -
SizeTransform fun SizeTransform( clip: Boolean = true, sizeAnimationSpec: (initialSize: IntSize,
targetSize: IntSize) -> FiniteAnimationSpec<IntSize> = { _, _ -> spring(visibilityThreshold = IntSize.VisibilityThreshold) } ): SizeTransform = SizeTransformImpl(clip, sizeAnimationSpec) -
SizeTransform fun SizeTransform( clip: Boolean = true, sizeAnimationSpec: (initialSize: IntSize,
targetSize: IntSize) -> FiniteAnimationSpec<IntSize> = { _, _ -> spring(visibilityThreshold = IntSize.VisibilityThreshold) } ): SizeTransform = SizeTransformImpl(clip, sizeAnimationSpec) -
SizeTransform fun SizeTransform( clip: Boolean = true, sizeAnimationSpec: (initialSize: IntSize,
targetSize: IntSize) -> FiniteAnimationSpec<IntSize> = { _, _ -> spring(visibilityThreshold = IntSize.VisibilityThreshold) } ): SizeTransform = SizeTransformImpl(clip, sizeAnimationSpec) -
SizeTransform fun SizeTransform( clip: Boolean = true, sizeAnimationSpec: (initialSize: IntSize,
targetSize: IntSize) -> FiniteAnimationSpec<IntSize> = { _, _ -> spring(visibilityThreshold = IntSize.VisibilityThreshold) } ): SizeTransform = SizeTransformImpl(clip, sizeAnimationSpec) -
SizeTransform fun SizeTransform( clip: Boolean = true, sizeAnimationSpec: (initialSize: IntSize,
targetSize: IntSize) -> FiniteAnimationSpec<IntSize> = { _, _ -> spring(visibilityThreshold = IntSize.VisibilityThreshold) } ): SizeTransform = SizeTransformImpl(clip, sizeAnimationSpec) -
Performer 3: The Size Shifter enum class ScreenMode { FullScreen,
Quarter, } var screenMode by remember { mutableStateOf(FullScreen) } -
Performer 3: The Size Shifter var screenMode by remember {
mutableStateOf(FullScreen) } AnimatedContent( targetState = screenMode, ) { state -> when (state) { FullScreen -> FullScreen() Quarter -> Quarter() } } -
Performer 3: The Size Shifter var screenMode by remember {
mutableStateOf(FullScreen) } AnimatedContent( targetState = screenMode, ) { state -> when (state) { FullScreen -> FullScreen() Quarter -> Quarter() } } -
Performer 3: The Size Shifter var screenMode by remember {
mutableStateOf(FullScreen) } AnimatedContent( targetState = screenMode, transitionSpec = {...}, ) { state -> when (state) { FullScreen -> FullScreen() Quarter -> Quarter() } } -
Performer 3: The Size Shifter var screenMode by remember {
mutableStateOf(FullScreen) } AnimatedContent( targetState = screenMode, transitionSpec = {...}, ) { state -> when (state) { FullScreen -> FullScreen() Quarter -> Quarter() } } -
Performer 3: The Size Shifter transitionSpec = { fadeIn(tween(300, 300))
with fadeOut(tween(600)) -
Performer 3: The Size Shifter transitionSpec = { fadeIn(tween(300, 300))
with fadeOut(tween(600)) using SizeTransform { initialSize, targetSize -> -
Performer 3: The Size Shifter transitionSpec = { fadeIn(tween(300, 300))
with fadeOut(tween(600)) using SizeTransform { initialSize, targetSize -> if (targetState == FullScreen) { keyframes { IntSize(targetSize.width, initialSize.height) at 300 durationMillis = 600 } } else { keyframes { IntSize(initialSize.width, targetSize.height) at 300 durationMillis = 600 } } } }, -
Performer 3: The Size Shifter transitionSpec = { fadeIn(tween(300, 300))
with fadeOut(tween(600)) using SizeTransform { initialSize, targetSize -> if (targetState == FullScreen) { keyframes { IntSize(targetSize.width, initialSize.height) at 300 durationMillis = 600 } } else { keyframes { IntSize(initialSize.width, targetSize.height) at 300 durationMillis = 600 } } } }, -
Performer 3: The Size Shifter transitionSpec = { fadeIn(tween(300, 300))
with fadeOut(tween(600)) using SizeTransform { initialSize, targetSize -> if (targetState == FullScreen) { // growing keyframes { IntSize(targetSize.width, initialSize.height) at 300 durationMillis = 600 } } else { // shrinking keyframes { IntSize(initialSize.width, targetSize.height) at 300 durationMillis = 600 } } } }, -
Performer 3: The Size Shifter transitionSpec = { fadeIn(tween(300, 300))
with fadeOut(tween(600)) using SizeTransform { initialSize, targetSize -> if (targetState == FullScreen) { keyframes { IntSize(targetSize.width, initialSize.height) at 300 durationMillis = 600 } } else { keyframes { IntSize(initialSize.width, targetSize.height) at 300 durationMillis = 600 } } } }, -
Performer 3: The Size Shifter transitionSpec = { fadeIn(tween(300, 300))
with fadeOut(tween(600)) using SizeTransform { initialSize, targetSize -> if (targetState == FullScreen) { keyframes { IntSize(targetSize.width, initialSize.height) at 300 durationMillis = 600 } } else { keyframes { IntSize(initialSize.width, targetSize.height) at 300 durationMillis = 600 } } } }, -
Performer 3: The Size Shifter transitionSpec = { fadeIn(tween(300, 300))
with fadeOut(tween(600)) using SizeTransform { initialSize, targetSize -> if (targetState == FullScreen) { keyframes { IntSize(targetSize.width, initialSize.height) at 300 durationMillis = 600 } } else { keyframes { IntSize(initialSize.width, targetSize.height) at 300 durationMillis = 600 } } } }, -
Performer 3: The Size Shifter transitionSpec = { fadeIn(tween(300, 300))
with fadeOut(tween(600)) using SizeTransform { initialSize, targetSize -> if (targetState == FullScreen) { keyframes { IntSize(targetSize.width, initialSize.height) at 300 durationMillis = 600 } } else { keyframes { IntSize(initialSize.width, targetSize.height) at 300 durationMillis = 600 } } } }, -
Performer 3: The Size Shifter transitionSpec = { fadeIn(tween(300, 300))
with fadeOut(tween(600)) using SizeTransform { initialSize, targetSize -> if (targetState == FullScreen) { keyframes { IntSize(targetSize.width, initialSize.height) at 300 durationMillis = 600 } } else { keyframes { IntSize(initialSize.width, targetSize.height) at 300 durationMillis = 600 } } } }, -
Performer 3: The Size Shifter var screenMode by remember {
mutableStateOf(FullScreen) } AnimatedContent( targetState = screenMode, transitionSpec = {...}, ) { state -> when (state) { FullScreen -> FullScreen() Quarter -> Quarter() } } -
Performer 3: The Size Shifter enum class ScreenMode { FullScreen,
Quarter, HalfVertical, HalfHorizontal, } var screenMode by remember { mutableStateOf(FullScreen) } AnimatedContent( targetState = screenMode, transitionSpec = {...}, ) { state -> when (state) { FullScreen -> FullScreen() Quarter -> Quarter() } } -
Performer 3: The Size Shifter! • State: enum class •
fade transition • Custom SizeTransform -
Wanna learn more? • Play with the repo: https://github.com/nicole-terc/animationsShow •
Animations in Compose: https://developer.android.com/jetpack/compose/animation • Animations API in Jetpack Compose Video: https://youtu.be/Z_T1bVjhMLk