gpt4 book ai didi

android - 当前 ViewAnimator 的新替代品是什么?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:13:45 27 4
gpt4 key购买 nike

背景

我一直在使用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 .它并没有说它已被替换/弃用,或者建议使用其他东西代替。

问题

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

  2. 与ViewAnimator相比有什么优缺点?

  3. 给定一个带有一些 View 的 ViewAnimator,如何将它转换为更新的解决方案,包括状态之间的切换?

最佳答案

我假设 Romain Guy means , 是ViewAnimator使用 Animation API,而较新的 API 被视为 Animator .参见“属性动画与 View 动画的不同之处” 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。如文档中所述:“如果 View 动画完成了您需要做的一切,或者如果您现有的代码已经按照您想要的方式工作,则无需使用属性动画系统”,这是为什么 ViewAnimator 没有被弃用。

关于android - 当前 ViewAnimator 的新替代品是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48661590/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com