android - 具有递归函数的反向动画
问题描述
在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) }
解决方案
正确的方法是使用ValueAnimator
instead on,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()
推荐阅读
- java - SpringSession DefaultCookieSerializer.setJvmRoute 有效,但 HttpServletRequest 没有所需的 jvmRoute
- android - 具有可滚动视图的 BottomSheetDialogFragment
- android - Android - 与移动应用程序的服务器通信
- javascript - 何时/如何格式化十进制数据类型 ASP.NET 返回 JSON 以供在数据表 js 中使用
- jmeter - Jmeter - Distributed test - How to distribute users to multiple slaves
- c# - how do save cookies with restclient
- mysql - 使用 Java 的 JSON MySQL 数据库
- swift - 使用 swift 和 spritekit,你如何阻止场景在变得活跃/移动到前台后取消暂停?
- ms-access - How to generate phone number extensions in MS access VBA
- airflow - Storing execution parameters in the Airflow database