片段,android:zAdjustment(z order)和动画

Boy*_*Boy 25 android-animation android-fragments

我正在使用支持库.现在,我希望从底部移动一个片段,移动到前一个片段.

为此我使用它来保持前一个片段(正在滑过的片段)可见,直到新片段到位:

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
   android:fromAlpha="1.0" android:toAlpha="1.0" 
   android:duration="2500"
   android:zAdjustment="bottom" />
Run Code Online (Sandbox Code Playgroud)

这是用于从底部滑入的新片段的动画:

<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="100%p" android:toYDelta="0"
        android:duration="@android:integer/config_mediumAnimTime" 
        android:zAdjustment="top"/>
Run Code Online (Sandbox Code Playgroud)

我已经将z调整放到了底部和顶部,但仍然是"底部"动画仍然在新片段的顶部!我已将持续时间设置为2500以进行测试,并且它始终保持最佳状态.

zAdjustment不适用于片段动画吗?

Mar*_*ius 9

根据这个谷歌组线程Z调整仅适用于窗口动画.

"Z调整仅适用于窗口动画.我认为这是有记录的,但显然不是." - Dianne Hackborn(Android框架工程师)

  • 那么有什么可行的解决方法呢? (11认同)

bar*_*bus 5

您可以覆盖该onCreateAnimation方法,对于任何动画,您都可以检查当前正在运行的动画,如果需要将其放在顶部,请从此处设置Z轴。

override fun onCreateAnimation(transit: Int, enter: Boolean, nextAnim: Int): Animation? {
    if (nextAnim == R.anim.enter_from_right || nextAnim == R.anim.exit_to_right) {
        ViewCompat.setTranslationZ(view, 1f)
    } else {
        ViewCompat.setTranslationZ(view, 0f)
    }

    return super.onCreateAnimation(transit, enter, nextAnim)
}
Run Code Online (Sandbox Code Playgroud)

建议将其作为所有片段的基础Fragment类实现。