Val*_*tin 3 android android-animation kotlin-android-extensions android-ktx
我的 Android 应用中有一些动画,我想更改代码以利用 Android KTX。可悲的是,我不太了解有关它的文档。有人可以告诉我如何在 Android KTX 的帮助下改进此代码吗?
view
.animate()
.translationY(view.height.toFloat())
.setDuration(3000)
.setInterpolator(AccelerateInterpolator())
.setListener(object : Animator.AnimatorListener {
override fun onAnimationStart(animation: Animator?) {}
override fun onAnimationRepeat(animation: Animator?) {}
override fun onAnimationCancel(animation: Animator?) {}
override fun onAnimationEnd(animation: Animator?) {
// Do whatever I want
}
})
Run Code Online (Sandbox Code Playgroud)
当然,我已经在 Gradle 文件中添加了依赖项:
implementation 'androidx.core:core-ktx:1.0.2'
Run Code Online (Sandbox Code Playgroud)
非常感谢您提前:)
Android KTX 具有doOnEnd { /** Do whatever you want **/ }和其他扩展功能android.animation.Animator。当你调用view.animate(),它返回ViewPropertyAnimator它不从继承android.animation.Animator类。因此您无法view.animate().doOnEnd { /** Do whatever you want **/ }由 Android KTX 提供。
但是,您可以使用withEndAction在动画结束时执行任何操作。withEndAction是 default 的一部分ViewPropertyAnimator,因此,您不需要使用 Android KTX 来使用它:
view
.animate()
.translationY(image.height.toFloat())
.setDuration(3000)
.setInterpolator(AccelerateInterpolator())
.withEndAction {
// Do whatever you want
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1448 次 |
| 最近记录: |