注意::我正在使用AndroidTV应用程序,因此我正在使用焦点导航和远程控制。
我有一个item的recyclerView,我在每个项目的onFocusChange中做一些动画处理。因此,当您使用箭头按钮浏览列表时,它将使用动画突出显示每个项目:
override fun onFocusChange(v: View?, focused: Boolean) {
val card = rowItemView.card
val title = rowItemView.title
val smallAnimation = rowItemView.viewModel.useSmallScaleAnimation
card.pivotX = when {
smallAnimation && position != 0-> card.width.toFloat() / 2.0f
else -> 0f
}
card.pivotY = card.height.toFloat() / 2.0f
if (focused) {
rowItemView.viewModel.setIsFocused(focused)
card.doTheAnimation(
titleView = title,
scale = if (smallAnimation) 1.5f else 1.2f,
elevation = 8.0f,
destinationAlpha = 1.0f
)
card.cardElevation = rowItemView.context.resources.getDimensionPixelSize(R.dimen.small_card_elevation).toFloat()
}
else {
card.doTheAnimation(
titleView = title,
scale = 1.0f,
elevation = 0.0f,
destinationAlpha = 0.0f,
onAnimationEnd = { rowItemView.viewModel.setIsFocused(focused) }
)
card.cardElevation = 0f
}
}
private fun View.doTheAnimation(
titleView: View?,
scale: Float,
elevation: Float,
destinationAlpha: Float,
onAnimationEnd: () -> Unit = {}
) {
val bannerX = ObjectAnimator.ofFloat(this, View.SCALE_X, scale)
val bannerY = ObjectAnimator.ofFloat(this, View.SCALE_Y, scale)
val bannerZ = ObjectAnimator.ofFloat(this, View.TRANSLATION_Z, elevation)
val animSet = AnimatorSet()
if (titleView != null) {
val titleAlpha = ObjectAnimator.ofFloat<View>(
titleView,
View.ALPHA,
destinationAlpha
)
animSet.playTogether(bannerX, bannerY, bannerZ, titleAlpha)
} else {
animSet.playTogether(bannerX, bannerY, bannerZ)
}
animSet.duration = 150L
animSet.addListener(object : AnimatorListenerAdapter() {
override fun onAnimationStart(animation: Animator) {
titleView?.visibility = View.VISIBLE
}
override fun onAnimationEnd(animation: Animator) {
if (titleView?.alpha == 0.0f) {
titleView.visibility = View.GONE
} else {
titleView?.visibility = View.VISIBLE
}
onAnimationEnd.invoke()
}
})
// `Handler().post` is attempt to speed up animation a little -> might be removed in future if nop benefit can be experienced
Handler().post{
animSet.start()
}
}
但是您也可以按住遥控器上的箭头按钮,然后快速滚动列表。问题在于,在这种情况下,动画会尝试从快速滚动开始的每个项目开始播放,而且效果不好。它看起来像马车和破损。
是否有可能仅对真正聚焦的物品启动动画?因此,当您按下箭头时,它会针对每个项目启动,但是当您按住箭头时,它仅对您停止的项目启动?