【问题标题】:Reverse Animation With Recursive Function具有递归函数的反向动画
【发布时间】:2020-09-30 10:29:57
【问题描述】:

在 android 上反转动画方向的最佳做法是什么? 另外,我在分析器上跟踪 ram 使用情况,结果是正常的。 我确实喜欢下面的代码:

 var flagHeight: Int = 100
    private fun startAnimation() {
        val animation = tv_hello_world.animate().apply {
            translationYBy(flagHeight.toFloat())
            setListener(object : Animator.AnimatorListener {
                override fun onAnimationStart(p0: Animator?) {
                    // do nothing
                }

                override fun onAnimationEnd(p0: Animator?) {
                    flagHeight = flagHeight.not()
                    startAnimation()
                }

                override fun onAnimationCancel(p0: Animator?) {
                    // do nothing
                }

                override fun onAnimationRepeat(p0: Animator?) {
                    // do nothing
                }
            })
            duration = 1000
        }
        animation.start()
    }

    fun Int.not() = run { if (this > 0) -this else (this * -1) }



   

【问题讨论】:

    标签: android animation recursion reverse


    【解决方案1】:

    正确的方法是使用ValueAnimator 而不是PropertyAnimator,因为您可以更好地控制它。试试这个方法:

    val animator = ValueAnimator.ofFloat(0f, 100f).apply {
            duration = 1000
            repeatCount = ValueAnimator.INFINITE
            repeatMode = ValueAnimator.REVERSE
            addUpdateListener {
                tv_hello_world.translationY = it.animatedValue as Float
            }
        }
        animator.start()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-05
      • 2010-11-28
      • 2020-09-26
      • 1970-01-01
      • 2015-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多