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

Beginner Of Jetpack Compose Animation

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.
Avatar for h5masa h5masa
August 12, 2021

Beginner Of Jetpack Compose Animation

potatotips 75 発表資料

Avatar for h5masa

h5masa

August 12, 2021
Tweet

More Decks by h5masa

Other Decks in Programming

Transcript

  1. • Android Developer GuideのCompose Pathwayで学ぶ機会があった • Compose By Exampleのチュートリアルで”Make the

    easy things easy and the hard things possible”というのが掲げられており、Animation は”hard things possible”にあたるとされていた。 Jetpack Compose Animationに興味を持ったキッカケ
  2. Jetpack Compose Animationとは? • JetpackComposeに含まれているコンポーネントの一つ • 宣言的なコーディングスタイルである • Compose Ver1.1.0時点での情報

    • StartStateとEndStateに基づいたUIの更新 • 継続的なStateの記録 • 特に設定がなければAnimationに必要な中間の情報を Composeが自動で補完する
  3. 単一Stateに基づいたPropertyの設定 var changed by remember { mutableStateOf(true) } val color

    by animateColorAsState( if (changed) Blue else Yellow, animationSpec = tween(durationMillis = 2000) ) Column { Button( onClick = { changed = !changed } ) { Text(text = "Click To Change Color") } Spacer(modifier = Modifier.height(30.dp)) customBox(color = color) }
  4. var boxState by remember { mutableStateOf(BoxState.SmallState) } val transition =

    updateTransition(targetState = boxState, label = "") val color by transition.animateColor(label = "color") { state -> when (state) { BoxState. SmallState -> Blue BoxState.LargeState -> Green } } val size by transition.animateDp(label = "size") { state -> when (state) { BoxState. SmallState -> 64.dp BoxState.LargeState -> 128.dp } } Column { Button( onClick = { boxState = when (boxState) { BoxState. SmallState -> BoxState.LargeState BoxState.LargeState -> BoxState.SmallState } }, 複数Stateに基づいたAnimation
  5. var boxState by remember { mutableStateOf(BoxState.SmallState) } val transition =

    updateTransition(targetState = boxState, label = "") val color by transition.animateColor(label = "color") { state -> when (state) { BoxState. SmallState -> Blue BoxState.LargeState -> Green } } val size by transition.animateDp(label = "size") { state -> when (state) { BoxState. SmallState -> 64.dp BoxState.LargeState -> 128.dp } } Column { Button( onClick = { boxState = when (boxState) { BoxState. SmallState -> BoxState.LargeState BoxState.LargeState -> BoxState.SmallState } }, 複数Stateに基づいたAnimation
  6. var vehicleState by remember { mutableStateOf(VechiclePosition.Start) } val offsetAnimation: Dp

    by animateDpAsState( if (vehicleState == VechiclePosition.Start) 5.dp else 300.dp, tween(durationMillis = 5000, easing = CubicBezierEasing(0.15f , 0.99f, 0.68f, 0.01f)) ) Image( painter = painterResource(R.drawable.vehicle), contentDescription = null, modifier = Modifier .height(90.dp) .absoluteOffset(x = offsetAnimation) ) FastOutSlowInEasing (デフォルト) LinearEasing FastOutLinearInEasing LinearOutSlowInEasing ベジェ曲線の図式化 Transition Builders:tween関数
  7. var vehicleState by remember { mutableStateOf(VechiclePosition.Start) } val offsetAnimation: Dp

    by animateDpAsState( if (vehicleState == VechiclePosition.Start) 5.dp else 250.dp, spring(dampingRatio = Spring.DampingRatioHighBouncy, stiffness = Spring.StiffnessLow) ) Image( painter = painterResource(R.drawable.vehicle), contentDescription = null, modifier = Modifier .height(90.dp) .absoluteOffset(x = offsetAnimation) ) Transition Builders:spring関数
  8. var vehicleState by remember { mutableStateOf(VechiclePosition.Start) } val offsetAnimation: Dp

    by animateDpAsState( if (vehicleState == VechiclePosition.Start) 5.dp else 250.dp, spring(dampingRatio = Spring.DampingRatioHighBouncy, stiffness = Spring.StiffnessLow) ) Image( painter = painterResource(R.drawable.vehicle), contentDescription = null, modifier = Modifier .height(90.dp) .absoluteOffset(x = offsetAnimation) ) Transition Builders:spring関数
  9. まとめ • 直観的 ◦ ComposeAnimationを使用することで、より直感的に記述して求めている Animationを実装できる • 拡張性 ◦ ComposeAnimationは以下の要素を駆使し、自分の理想のアニメーションに近づくように細かく設定ができる

    ▪ StartとEndのState ▪ 変化させたいアニメーションのタイプ ▪ TransitionBuildersでDurationや回数などの細かい設定 ▪ Easing関数(ベジェ曲線)を使用して、変化率のより細かいカスタマイズ • 柔軟性 ◦ ComposeAnimationどうしの組み合わせも可能 ◦ Animationの途中変化の値も確認可能