如何使用Jetpack Compose制作动画?

保尔捷·图森库洛夫

我想在此代码实验室中使用Compose而不是ConstraintLayout:https ://codelabs.developers.google.com/codelabs/advanced-android-kotlin-training-property-animation/#1

如何将任何动画应用到Compose?

保尔捷·图森库洛夫

这是如何应用动画的指南:

该教程中的代码和平:

      private val rotation = FloatPropKey()

      private fun createDefinition(duration: Int) = transitionDefinition {
          state(0) { this[rotation] = 0f }
          state(1) { this[rotation] = 360f }

          transition {
              rotation using repeatable {
                  animation = tween {
                      easing = LinearEasing
                      this.duration = duration
                  }
                  iterations = Infinite
              }
          }
      }

      @Composable
      fun RotateIndefinitely(durationPerRotation: Int, children: @Composable() () -> Unit) {
          Transition(definition = createDefinition(durationPerRotation), initState = 0, toState = 1) {
              Rotate(it[rotation], children)
          }
      }

      @Composable
      fun Rotate(degree: Float, children: @Composable() () -> Unit) {
          Draw(children = children) { canvas, parent ->
              val halfWidth = parent.width.value / 2
              val halfHeight = parent.height.value / 2

              canvas.save()
              canvas.translate(halfWidth, halfHeight)
              canvas.rotate(degree)
              canvas.translate(-halfWidth, -halfHeight)
              drawChildren()
              canvas.restore()
          }
      }


            @Composable
        private fun RotatingPokeBall() {
            RotateIndefinitely(durationPerRotation = 4000) {
                Opacity(opacity = 0.75f) {
                    DrawImage(
                        image = +imageResource(R.drawable.pokeball),
                        tint = +colorResource(R.color.poke_red)
                    )
                }
            }
        }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章