当前我正在用以下代码进行动画对象的宽度:
val animation = ValueAnimator.ofInt(layoutParams.width, value)
.setDuration(duration)
animation.interpolator = interpolator ?: LinearInterpolator()
animation.start()
animation.addUpdateListener {
val animatedValue = it.animatedValue as Int
layoutParams.width = animatedValue
requestLayout()
}
animation.addListener(object: Animator.AnimatorListener {
override fun onAnimationRepeat(p0: Animator?) {}
override fun onAnimationEnd(p0: Animator?) {
endHandler?.invoke()
}
override fun onAnimationCancel(p0: Animator?) {}
override fun onAnimationStart(p0: Animator?) {}
})
但是,即使在简单的布局上,动画也很懒惰。有什么方法可以改善LayoutParams改变动画的性能?
我已经在"办公时间"的Cracow的GDD 2017上问了这个问题,他们告诉我看尼克·屠夫的应用程序 -> plaid.io。绝对值得一看的是,您是否在Android的动画中很有趣。
这是链接:
https://github.com/nickbutcher/plaid
我们做到了,我们看到他经常使用
animation.willChangeBounds = true
,这解决了我们用laggy动画的问题< 3
使用animation.setTarget(viewToAnimate)
方法来动画目标视图。
我没有测试过,让我知道它是否有效。
希望它有帮助。