【问题标题】:Android Fragment transition exit animation played above enter animation上面播放的Android Fragment过渡退出动画进入动画
【发布时间】:2019-06-30 08:32:12
【问题描述】:

我正在实现片段过渡动画。

我的exit 动画是

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:ordering="together">
    <objectAnimator
        android:propertyName="scaleX"
        android:valueType="floatType"
        android:valueFrom="1.0"
        android:valueTo="0.95"
        android:duration="300"/>

    <objectAnimator
        android:propertyName="scaleY"
        android:valueType="floatType"
        android:valueFrom="1.0"
        android:valueTo="0.95"
        android:duration="300"/>

    <objectAnimator
        android:propertyName="x"
        android:valueType="floatType"
        android:valueFrom="0"
        android:valueTo="10dp"
        android:duration="300"/>
</set>

enter动画是:

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="x"
    android:valueType="floatType"
    android:valueFrom="1280"
    android:valueTo="0"
    android:duration="400"/>

事务是这样创建的:

fragmentManager.beginTransaction()
            .setCustomAnimations(enter, exit, popEnter, popExit)
            .replace(CONTENT_CONTAINER_ID, newFragment)
            .addToBackStack(null)
            .commit();

在正常的动画速度下,由于动画持续时间短,不需要的效果几乎是不可见的,但是,当你放慢它们的速度时,你可以清楚地看到,z-order 是错误的。

进入片段动画低于退出片段动画。有解决办法吗?

【问题讨论】:

标签: android android-fragments android-animation


【解决方案1】:

我认为您可以尝试以下方法作为解决方法。

fragmentTr.setCustomAnimations(enter, exit);
fragmentTr.hide(currentFragment);
fragmentTr.add(containerId, newFragment, "aTag");
fragmentTr.show(newFragment);
fragmentTr.commit();

不要使用replace()。我尝试了上面的方法,它奏效了。 希望这会有所帮助。

【讨论】:

  • 是的,这种工作,但我的导航有点棘手,有自定义的后台堆栈,这会破坏它......
  • 请注意:顺序也很重要。演出后我有 setCustomAnimations ......不是一个好的 idae
【解决方案2】:

这可能已经过时了,但我刚刚遇到了同样的问题。我通过在我的 XML 中定义两个内容区域来解决它,例如:

<FrameLayout>
   <FrameLayout id="@+id/oldFragment" />
   <FrameLayout id="@+id/newFragment" />
</FrameLayout>

我会将第一个片段加载到oldFragment,我的事务如下所示:

getActivity().getSupportFragmentManager()
        .beginTransaction()
        .remove(old_frag)
        .add(R.id.newFragment, new_frag)
        .addToBackStack(null)
        .commit();

希望对您有所帮助。

【讨论】:

    【解决方案3】:

    我知道它太旧了,但我在使用 android jetpack 时遇到了同样的问题。

    android jetpack 和导航图的这个答案

    1. 在 navigation.xml 中设置动画

      <fragment
      android:id="@+id/specifyAmountFragment"
      android:name="com.example.buybuddy.buybuddy.SpecifyAmountFragment"
      android:label="fragment_specify_amount"
      tools:layout="@layout/fragment_specify_amount">
      <action
          android:id="@+id/confirmationAction"
          app:destination="@id/confirmationFragment"
          app:enterAnim="@anim/slide_in_right"
          app:exitAnim="@anim/slide_out_left"
          app:popEnterAnim="@anim/slide_in_left"
          app:popExitAnim="@anim/slide_out_right" />
      

    2. 添加动画资源

    slide_in_left.xml

    <?xml version="1.0" encoding="utf-8"?>
    <setxmlns:android="http://schemas.android.com/apk/res/android">    
        <translate android:fromXDelta="-100%p" android:toXDelta="0" ndroid:duration="200"/>
    </set>
    

    slide_out_left.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate android:fromXDelta="0" android:toXDelta="-100%p" android:duration="200"/>
    </set>
    

    slide_in_right.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="200"/>
    </set>
    

    slide_out_right.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate android:fromXDelta="0" android:toXDelta="100%p" android:duration="200"/>
    </set>
    

    Other useful answer More details at android developer docs

    其他示例动画:

    【讨论】:

      【解决方案4】:

      它对我有用,将这一行添加到动画中

      android:interpolator="@android:anim/linear_interpolator"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-09-21
        • 1970-01-01
        • 1970-01-01
        • 2020-07-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-11
        相关资源
        最近更新 更多