使用Android Jetpack导航预先加载片段以实现平稳过渡

时间:2018-08-22 13:10:51

标签: android android-transitions android-jetpack android-architecture-navigation

是否可以从Android Jetpack中向Navigation Architecture Component预先加载某些Fragments以便平滑过渡?

没有预加载,我将永远不会获得前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
}

0 个答案:

没有答案