如何解决为什么使用 AnimatorInflater.loadAnimator 不执行动画
我目前正在处理工具栏动画,通过在运行时设置动画来根据屏幕的滚动状态更改工具栏的高度和颜色。这些动画目前工作正常。这是我正在运行的代码:
val elevation = if (scrolled) resources.getDimension(R.dimen.toolbar_scrolled_elevation) else resources.getDimension(R.dimen.toolbar_non_scrolled_elevation)
val elevationAnimation = ObjectAnimator.ofFloat(binding.toolbar,"elevation",elevation)
val color = if (scrolled) ContextCompat.getColor(baseContext,R.color.white) else ContextCompat.getColor(baseContext,R.color.blue_white)
val colorAnimation = ObjectAnimator.ofInt(binding.toolbar,"color",color)
AnimatorSet().apply {
duration = resources.getInteger(android.R.integer.config_shortAnimTime).toLong()
}.run {
playTogether(elevationAnimation,colorAnimation)
start()
}
我一直在研究重构并将动画移动到资源文件中。我用 <set>
创建了两个不同的 <objectAnimator>
并将它们放置在 animator
资源文件夹中。进行这些更改时,动画将停止工作。
toolbar_non_scrolled_state_animator.xml
:
<?xml version="1.0" encoding="utf-8"?>
<set>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_shortAnimTime"
android:propertyName="elevation"
android:valueTo="@dimen/toolbar_non_scrolled_elevation"
android:valueType="floatType"/>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_shortAnimTime"
android:propertyName="color"
android:valueTo="@color/white"
android:valueType="intType"/>
</set>
toolbar_scrolled_state_animator.xml
:
<?xml version="1.0" encoding="utf-8"?>
<set>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_shortAnimTime"
android:propertyName="elevation"
android:valueTo="@dimen/toolbar_scrolled_elevation"
android:valueType="floatType"/>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_shortAnimTime"
android:propertyName="color"
android:valueTo="@color/blue_white"
android:valueType="intType"/>
</set>
并将 Activity
中的代码更改为:
val animator = if (scrolled) R.animator.toolbar_scrolled_state_animator else R.animator.toolbar_non_scrolled_state_animator
(AnimatorInflater.loadAnimator(baseContext,animator) as AnimatorSet).start()
运行此代码时,动画不会运行,我在控制台中也看不到任何错误。
这种新方法不起作用的可能原因是什么?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。