how to remove default animation transition when using navigation component in Android?

前端 未结 3 1047
隐瞒了意图╮
隐瞒了意图╮ 2021-01-06 08:39

I am using Navigation component, and I have a bottom navigation view in the main activity. when I tap the tab in that bottom navigation view, it seems that there is a fade i

相关标签:
3条回答
  • 2021-01-06 09:11

    You can add anim file to replace the default animation.

    • res/anim/nav_default_enter_anim.xml
    • res/anim/nav_default_exit_anim.xml
    • res/anim/nav_default_pop_enter_anim.xml
    • res/anim/nav_default_pop_exit_anim.xml
    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <!--Empty to disable animation-->
    </set>
    

    [Navigation Component] I can`t change animation from NavigationUI.setupWithNavController()

    0 讨论(0)
  • 2021-01-06 09:25

    @HvSimon has provided a solution to set the global default which hasn't worked for me. Also I would like to choose/disable the animations per transition.

    You can supply additional arguments to navigate() with a NavOptions object.

    First create an animation in your res folder like res/anim/nav_enter_anim.xml (empty for no animation):

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

    Create one animation xml per animation or reuse the same. Then supply a NavOptions object to your navigate() call like so:

    val animationOptions = NavOptions.Builder().setEnterAnim(R.anim.nav_enter_anim)
                .setExitAnim(R.anim.nav_exit_anim)
                .setPopEnterAnim(R.anim.nav_pop_enter_anim)
                .setPopExitAnim(R.anim.nav_pop_exit_anim).build()
    
    findNavController().navigate(MyFragmentDirections.toMainActivity(), animationOptions)
    
    0 讨论(0)
  • 2021-01-06 09:25

    As per this issue:

    NavigationUI is a set of helpers which follow the material design guidelines and that includes animations between BottomNavigationView items.

    And you'll note in the Transitions section of the Material design guidelines, they specifically state:

    Transition between active and inactive bottom navigation destinations using a cross-fade animation.

    Therefore Navigation does not provide any API for customizing or removing the animations.

    Note that the Navigation 2.1.0-alpha03 release did have this change:

    The default animations provided by NavigationUI have been sped up from 400ms to 220ms to match the default animation speed of activities and fragments. b/130055522

    So I'd suggest 1) upgrading to Navigation 2.1.0-alpha03 or higher to get the updated animations and 2) following the material design guidelines.

    Of course, NavigationUI is totally optional and you can certainly do whatever you want using the underlying OnDestinationChangedListener that NavigationUI uses under the hood.

    0 讨论(0)
提交回复
热议问题