当前 ViewAnimator 的新替代品是什么?
What is the current, new alternative to ViewAnimator?
背景
我已经使用 ViewAnimator/ViewSwitcher 很长时间了。
我遇到的最常见的用例是从加载阶段切换到内容阶段,或者在向导的阶段之间切换,有时甚至会出现错误阶段。
问题
当我建议向 "android-ktx" 存储库 (here) 添加一个不错的扩展功能时,我被告知:
ViewAnimator is not an API we actively recommend to animate views.
It's based on the old animation system and we don't want to promote
its use in this library.
我试过的
我看过 ViewAnimator 和 ViewSwitcher 的文章,包括 docs。它并没有说它是replaced/deprecated,或者建议使用其他东西代替。
问题
ViewAnimator 被什么取代了?他在谈论过渡吗?
与ViewAnimator相比有什么优缺点?
给定一个带有一些视图的 ViewAnimator,如何将它转换为较新的解决方案,包括状态之间的切换?
我假设什么Romain Guy means, is that ViewAnimator
uses Animation
API, whereas a newer API is considered Animator
. See "How Property Animation Differs from View Animation" in the docs,其中提到了每个API的优点和缺点以及使用场景:
The view animation system provides the capability to only animate View objects, so if you wanted to animate non-View objects, you have to implement your own code to do so. The view animation system is also constrained in the fact that it only exposes a few aspects of a View object to animate, such as the scaling and rotation of a View but not the background color, for instance.
Another disadvantage of the view animation system is that it only modified where the View was drawn, and not the actual View itself. For instance, if you animated a button to move across the screen, the button draws correctly, but the actual location where you can click the button does not change, so you have to implement your own logic to handle this.
With the property animation system, these constraints are completely removed, and you can animate any property of any object (Views and non-Views) and the object itself is actually modified. The property animation system is also more robust in the way it carries out animation. At a high level, you assign animators to the properties that you want to animate, such as color, position, or size and can define aspects of the animation such as interpolation and synchronization of multiple animators.
The view animation system, however, takes less time to setup and requires less code to write. If view animation accomplishes everything that you need to do, or if your existing code already works the way you want, there is no need to use the property animation system. It also might make sense to use both animation systems for different situations if the use case arises.
虽然没有直接的方法来“将ViewAnimator
转换为使用更新的方法”,因为它在内部使用Animation
API。如文档中所述:“如果视图动画完成了您需要做的一切,或者如果您现有的代码已经按照您想要的方式工作,则无需使用 属性 动画系统” ,这就是 ViewAnimator
未被弃用的原因。
尝试使用AdapterViewAnimator。 AdapterViewAnimator
需要子视图的适配器,因此它可能比 ViewAnimator
更难使用,但它需要 ViewAnimator
class 的方法,例如 showNext()
, setInAnimation(ObjectAnimator)
和 setOutAnimation(ObjectAnimator)
。是的,你必须手动将所有Animation重写到ObjectAnimator中。
我想一种可能的替代方法是使用 ConstraintLayout 的转换,如图 here 所示。
要实现,似乎必须使用 2 个相似的布局,每个视图具有相同的 id,然后您可以在阶段之间切换,例如:
class MainActivity : AppCompatActivity() {
private var show = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.circuit)
backgroundImage.setOnClickListener {
if(show)
hideComponents() // if the animation is shown, we hide back the views
else
showComponents() // if the animation is NOT shown, we animate the views
}
}
private fun showComponents(){
show = true
val constraintSet = ConstraintSet()
constraintSet.clone(this, R.layout.circuit_detail)
val transition = ChangeBounds()
transition.interpolator = AnticipateOvershootInterpolator(1.0f)
transition.duration = 1200
TransitionManager.beginDelayedTransition(constraint, transition)
constraintSet.applyTo(constraint)
}
private fun hideComponents(){
show = false
val constraintSet = ConstraintSet()
constraintSet.clone(this, R.layout.circuit)
val transition = ChangeBounds()
transition.interpolator = AnticipateOvershootInterpolator(1.0f)
transition.duration = 1200
TransitionManager.beginDelayedTransition(constraint, transition)
constraintSet.applyTo(constraint)
}
}
背景
我已经使用 ViewAnimator/ViewSwitcher 很长时间了。
我遇到的最常见的用例是从加载阶段切换到内容阶段,或者在向导的阶段之间切换,有时甚至会出现错误阶段。
问题
当我建议向 "android-ktx" 存储库 (here) 添加一个不错的扩展功能时,我被告知:
ViewAnimator is not an API we actively recommend to animate views. It's based on the old animation system and we don't want to promote its use in this library.
我试过的
我看过 ViewAnimator 和 ViewSwitcher 的文章,包括 docs。它并没有说它是replaced/deprecated,或者建议使用其他东西代替。
问题
ViewAnimator 被什么取代了?他在谈论过渡吗?
与ViewAnimator相比有什么优缺点?
给定一个带有一些视图的 ViewAnimator,如何将它转换为较新的解决方案,包括状态之间的切换?
我假设什么Romain Guy means, is that ViewAnimator
uses Animation
API, whereas a newer API is considered Animator
. See "How Property Animation Differs from View Animation" in the docs,其中提到了每个API的优点和缺点以及使用场景:
The view animation system provides the capability to only animate View objects, so if you wanted to animate non-View objects, you have to implement your own code to do so. The view animation system is also constrained in the fact that it only exposes a few aspects of a View object to animate, such as the scaling and rotation of a View but not the background color, for instance.
Another disadvantage of the view animation system is that it only modified where the View was drawn, and not the actual View itself. For instance, if you animated a button to move across the screen, the button draws correctly, but the actual location where you can click the button does not change, so you have to implement your own logic to handle this.
With the property animation system, these constraints are completely removed, and you can animate any property of any object (Views and non-Views) and the object itself is actually modified. The property animation system is also more robust in the way it carries out animation. At a high level, you assign animators to the properties that you want to animate, such as color, position, or size and can define aspects of the animation such as interpolation and synchronization of multiple animators.
The view animation system, however, takes less time to setup and requires less code to write. If view animation accomplishes everything that you need to do, or if your existing code already works the way you want, there is no need to use the property animation system. It also might make sense to use both animation systems for different situations if the use case arises.
虽然没有直接的方法来“将ViewAnimator
转换为使用更新的方法”,因为它在内部使用Animation
API。如文档中所述:“如果视图动画完成了您需要做的一切,或者如果您现有的代码已经按照您想要的方式工作,则无需使用 属性 动画系统” ,这就是 ViewAnimator
未被弃用的原因。
尝试使用AdapterViewAnimator。 AdapterViewAnimator
需要子视图的适配器,因此它可能比 ViewAnimator
更难使用,但它需要 ViewAnimator
class 的方法,例如 showNext()
, setInAnimation(ObjectAnimator)
和 setOutAnimation(ObjectAnimator)
。是的,你必须手动将所有Animation重写到ObjectAnimator中。
我想一种可能的替代方法是使用 ConstraintLayout 的转换,如图 here 所示。
要实现,似乎必须使用 2 个相似的布局,每个视图具有相同的 id,然后您可以在阶段之间切换,例如:
class MainActivity : AppCompatActivity() {
private var show = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.circuit)
backgroundImage.setOnClickListener {
if(show)
hideComponents() // if the animation is shown, we hide back the views
else
showComponents() // if the animation is NOT shown, we animate the views
}
}
private fun showComponents(){
show = true
val constraintSet = ConstraintSet()
constraintSet.clone(this, R.layout.circuit_detail)
val transition = ChangeBounds()
transition.interpolator = AnticipateOvershootInterpolator(1.0f)
transition.duration = 1200
TransitionManager.beginDelayedTransition(constraint, transition)
constraintSet.applyTo(constraint)
}
private fun hideComponents(){
show = false
val constraintSet = ConstraintSet()
constraintSet.clone(this, R.layout.circuit)
val transition = ChangeBounds()
transition.interpolator = AnticipateOvershootInterpolator(1.0f)
transition.duration = 1200
TransitionManager.beginDelayedTransition(constraint, transition)
constraintSet.applyTo(constraint)
}
}