如何为片段设置翻译动画?

时间:2016-03-05 19:02:38

标签: java android xml

我在我的应用程序中为Fragments事务使用自定义动画但是我在gradle中得到了一个关闭此错误的应用程序:

03-05 22:25:58.185 14497-14497/ir.aftabeshafa.shafadoc E/AndroidRuntime: FATAL EXCEPTION: main
Process: ir.aftabeshafa.shafadoc, PID: 14497
java.lang.RuntimeException: Unknown animator name: translate
at android.animation.AnimatorInflater.createAnimatorFromXml(AnimatorInflater.java)
at android.animation.AnimatorInflater.createAnimatorFromXml(AnimatorInflater.java)
at android.animation.AnimatorInflater.createAnimatorFromXml(AnimatorInflater.java)
at android.animation.AnimatorInflater.loadAnimator(AnimatorInflater.java)
at android.animation.AnimatorInflater.loadAnimator(AnimatorInflater.java)
at android.animation.AnimatorInflater.loadAnimator(AnimatorInflater.java)
at android.app.FragmentManagerImpl.loadAnimator(FragmentManager.java)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java)
at android.app.FragmentManagerImpl.removeFragment(FragmentManager.java)
at android.app.BackStackRecord.run(BackStackRecord.java)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java)
at android.os.Handler.handleCallback(Handler.java)
at android.os.Handler.dispatchMessage(Handler.java)
at android.os.Looper.loop(Looper.java)
at android.app.ActivityThread.main(ActivityThread.java)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)

slideinanimation.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:duration="700"
        android:fromXDelta="0%"
        android:fromYDelta="-100%"
        android:toXDelta="0%"
        android:toYDelta="0%" />
</set>

slideoutanimation.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:duration="700"
        android:fromXDelta="0%"
        android:fromYDelta="0%"
        android:toXDelta="0%"
        android:toYDelta="-100%" />
</set>

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我发现我的问题回答了。对于片段,您应该在动画XML文件中使用objectAnimator,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <objectAnimator
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="500"
        android:propertyName="yFraction"
        android:valueFrom="-1000"
        android:valueTo="0" />

</set>