8

Fragments是否可以使用 Android Jetpack的导航架构组件预加载某些内容以获得平滑过渡?

如果没有预加载,我将永远无法在第一次navigate()通话时获得平滑过渡(即使在高端设备上也是如此)。我目前在我app:enterAnim的.app:exitAnimnav_graph.xml

我正在尝试像在 Telegram 应用程序中那样完成平滑的过渡(我认为他们正在预加载所有内容,并且只是以Fragments类似的方式制作动画Views)。

是否有一些隐藏的 XML 属性app:preload="true"

我不仅要求对我的问题提供全面的答案 - 也欢迎进行一些小的优化!

我走了多远:

anim_nav_enter.xml

<set
    android:duration="200"
    android:shareInterpolator="true">
    <!-- Fade in from the right on enter -->
    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />
    <translate
        android:fromXDelta="10%"
        android:toXDelta="0%" />
    <scale
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:toXScale="1.0"
        android:toYScale="1.0" />
    </set>

anim_nav_exit.xml

<set
    android:duration="300"
    android:shareInterpolator="true">
    <!-- Fade out on exit -->
    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />
    <scale
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:toXScale="1.0"
        android:toYScale="1.0" />
</set>

anim_nav_pop_enter.xml

<set
    android:duration="200"
    android:shareInterpolator="true">
    <!-- Fade in on pop enter -->
    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />
    <scale
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:toXScale="1.0"
        android:toYScale="1.0" />
</set>

anim_nav_pop_exit.xml

<set
    android:duration="200"
    android:shareInterpolator="true">
    <!-- Fade out to the right on pop exit -->
    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />
    <translate
        android:fromXDelta="0%"
        android:toXDelta="10%" />
    <scale
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:toXScale="1.0"
        android:toYScale="1.0" />
</set>

BaseFragment.kt

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    allowEnterTransitionOverlap = true
    allowReturnTransitionOverlap = true
}

override fun onCreateAnimation(transit: Int, enter: Boolean, nextAnim: Int): Animation? {
    var animation = super.onCreateAnimation(transit, enter, nextAnim)
    if (animation == null && nextAnim != 0) {
        animation = AnimationUtils.loadAnimation(activity, nextAnim)
    }
    if (animation != null) {
        view?.setLayerType(View.LAYER_TYPE_HARDWARE, null)
        animation.setAnimationListener(object : AnimationListener {
            override fun onAnimationRepeat(animation: Animation?) {
            }

            override fun onAnimationStart(animation: Animation?) {
            }

            override fun onAnimationEnd(animation: Animation?) {
                view?.setLayerType(View.LAYER_TYPE_NONE, null)
            }
        })
    }
    return animation
}
4

0 回答 0