【发布时间】:2016-06-16 05:41:12
【问题描述】:
我不知道为什么这总是很难开始工作。我正在使用 AppCompat 库和android.app.Fragment。我尝试添加动画以向左/向右滑动新片段(就像 iOS 一样),但是当添加片段时,它们会立即添加/删除,没有任何动画。
我做错了什么?
getFragmentManager()
.beginTransaction()
.setCustomAnimations(R.animator.slide_in_from_right, R.animator.slide_out_to_the_left)
.add(R.id.navrootlayout, fragment)
.addToBackStack(null)
.commit();
res/animator/slide_in_from_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:interpolator="@interpolator/decelerate_cubic"
android:valueFrom="1"
android:valueTo="0"
android:valueType="floatType"
android:propertyName="xFraction"
android:duration="3000"/>
</set>
res/animator/slide_out_to_the_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:interpolator="@interpolator/decelerate_cubic"
android:valueFrom="0"
android:valueTo="-1"
android:valueType="floatType"
android:propertyName="xFraction"
android:duration="3000"/>
</set>
我什至将动画的持续时间设置为 3000(即 3 秒),这样我就可以肯定地看到它是否正在被使用,但事实并非如此。添加片段时根本没有任何动画。我捕获了它发生的屏幕视频,新片段立即出现(并最终消失)。
【问题讨论】:
-
“xFraction”是什么属性?
-
Yeeeeaaaah,我刚刚花了最后一个小时才弄清楚这一点。显然,我从中得到的示例代码假设我将向我的 Layout 类添加自定义属性,但他们没有提到这一点。
-
我认为它的原因是动画师没有相对的翻译机制,所以你不能滑入。创建一个“xFraction”动画(我假设“X”的百分比)让你做幻灯片。
标签: android android-fragments android-appcompat