Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Getting started with animations in Jetpack Compose

Getting started with animations in Jetpack Compose

PRESENTED AT:
International Women's Day 2022 at GDG Bonny Island

https://gdg.community.dev/events/details/google-gdg-bonny-island-presents-international-womens-day-2022/

DATE:
March 12, 2022

TIME:
7:00 > 20 min

DESCRIPTION:

MORE TALKS & ARTICLES FROM ME: https://cupsofcode.com/talks/

Avatar for Aida Issayeva

Aida Issayeva

March 12, 2022
Tweet

More Decks by Aida Issayeva

Other Decks in Programming

Transcript

  1. “Jetpack Compose is Android’s modern toolkit for building native UI.”

    https://developer.android.com/jetpack/compose/why-adopt https://developer.android.com/codelabs/jetpack-compose-animation#0
  2. High-Level API AnimatedVisibility AnimatedContent animateContentSize Crossfade val show = remember

    { mutableStateOf(false) } AnimatedVisibility(visible = show.value){ ProgressNotPerfection() } Button( onClick = { show.value = !show.value } )
  3. High-Level API AnimatedVisibility AnimatedContent animateContentSize Crossfade val years = remember

    { mutableStateOf(2019) } AnimatedContent(targetState = years.value) { targetYear -> ProgressNotPerfection(targetYear) } Button( onClick = { years.value++ }
  4. High-Level API AnimatedVisibility AnimatedContent animateContentSize Crossfade val years = remember

    { mutableStateOf(2019) } Crossfade(targetState = years.value) { targetYear -> ProgressNotPerfection(targetYear) } Button( onClick = { years.value++ }
  5. High-Level API AnimatedVisibility AnimatedContent animateContentSize Crossfade val isProgress = remember

    { mutableStateOf(true) } val text = if(isProgress.value) { "#ProgressNotPerfection" } else { "IWD'22" } Box( modifier = Modifier .animateContentSize() .clickable { isProgress.value = !isProgress. value } ) { ProgressNotPerfection(text)
  6. val isProgress = remember { mutableStateOf(true) } val color by

    animateColorAsState( targetValue = if (isProgress.value) { Color.Blue } else { Color.Red } ) Box( modifier = Modifier .background(color = color) .clickable { isProgress.value = !isProgress. value } ) Low-Level API animate*AsState rememberInfiniteTransition updateTransition
  7. Low-Level API animate*AsState rememberInfiniteTransition updateTransition val infiniteTransition = rememberInfiniteTransition() val

    alpha = infiniteTransition.animateFloat( initialValue = 0f, targetValue = 1f, animationSpec = infiniteRepeatable( animation = tween(durationMillis = 1000), repeatMode = RepeatMode.Reverse ) ) Box( modifier = Modifier .alpha(alpha = alpha.value) )
  8. Low-Level API animate*AsState rememberInfiniteTransition updateTransition val transition = updateTransition( targetState

    = isProgress.value) val offsetY = transition.animateDp(label = "offsetY") { targetState -> when (targetState) { true -> 10.dp else -> 60.dp } } . . . Box( modifier = Modifier .offset(y = offsetY.value) .alpha(alpha = alpha.value) .border(width = borderWidth.value) .clickable { isProgress.value = !isProgress. value }