ViewAnimator目前的新替代品是什么?

and*_*per 11 android android-animation viewswitcher android-view viewanimator

背景

我一直在使用ViewAnimator/ViewSwitcher.

我最常见的用例是从加载阶段切换到内容阶段,或者在向导阶段之间切换,甚至有时会出现错误阶段.

问题

当我建议在"android-ktx"存储库(这里)添加一个很好的扩展函数时,我被告知:

ViewAnimator不是我们积极推荐用于动画视图的API.它基于旧的动画系统,我们不想在这个库中推广它的使用.

我试过的

我查看了ViewAnimator和ViewSwitcher的文章,包括文档.它没有说它被替换/弃用,或者建议使用别的东西.

问题

  1. 什么取代了ViewAnimator?他在谈论过渡吗?

  2. 与ViewAnimator相比有哪些优缺点?

  3. 给定具有一些视图的ViewAnimator,如何将其转换为更新的解决方案,包括状态之间的切换?

and*_*per 0

我想一种可能的替代方案是使用 ConstraintLayout 的转换,如此处所示

为了实现,它似乎必须使用 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)
    }
}
Run Code Online (Sandbox Code Playgroud)