如何为导航操作定义默认动画? [英] How do I define default animations for Navigation Actions?

查看:23
本文介绍了如何为导航操作定义默认动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Android Studio 3.2 Canary 14 和 导航架构组件.有了这个,您可以像使用 Intent 一样定义过渡动画.

I'm using Android Studio 3.2 Canary 14 and The Navigation Architecture Component. With this you can define transition animations pretty much as you would when using Intents.

但是动画被设置为导航图中动作的属性,如下所示:

But the animations are set as properties of the actions in the navigation graph, like so:

<fragment
    android:id="@+id/startScreenFragment"
    android:name="com.example.startScreen.StartScreenFragment"
    android:label="fragment_start_screen"
    tools:layout="@layout/fragment_start_screen" >
  <action
    android:id="@+id/action_startScreenFragment_to_findAddressFragment"
    app:destination="@id/findAddressFragment"
    app:enterAnim="@animator/slide_in_right"
    app:exitAnim="@animator/slide_out_left"
    app:popEnterAnim="@animator/slide_in_left"
    app:popExitAnim="@animator/slide_out_right"/>
</fragment>

为图中的所有动作定义这会很乏味!

This gets tedious to define for all actions in the graph!

有没有办法在动作上定义一组默认动画?

我没有运气为此使用样式.

I've had no luck using styles for this.

推荐答案

R.anim 具有定义的默认动画(作为最终):

R.anim has the default animations defined (as final):

  • nav_default_enter_anim

nav_default_exit_anim

nav_default_pop_enter_anim

nav_default_pop_exit_anim

为了改变这种行为,你必须使用自定义 NavOptions,

in order to change this behavior, you would have to use custom NavOptions,

因为这是将这些动画分配给 NavAction.

because this is where those animation are being assigned to a NavAction.

可以使用 NavOptions.Builder 分配这些:>

one can assign these with the NavOptions.Builder:

protected NavOptions getNavOptions() {

    NavOptions navOptions = new NavOptions.Builder()
      .setEnterAnim(R.anim.default_enter_anim)
      .setExitAnim(R.anim.default_exit_anim)
      .setPopEnterAnim(R.anim.default_pop_enter_anim)
      .setPopExitAnim(R.anim.default_pop_exit_anim)
      .build();

    return navOptions;
}

很可能需要创建一个 DefaultNavFragment,它扩展类 androidx.navigation.fragment(那里的文档似乎还没有完成).

most likely one would need to create a DefaultNavFragment, which extends class androidx.navigation.fragment (the documentation there does not seem completed yet).

所以你可以像这样将这些 NavOptions 传递给 NavHostFragment:

So you can pass these NavOptions to the NavHostFragment like this:

NavHostFragment.findNavController(this).navigate(R.id.your_action_id, null, getNavOptions());

或者...查看该包的 attrs.xml 时;这些动画具有风格:

alternatively... when looking at the attrs.xml of that package; these animations are style-able:

<resources>
    <declare-styleable name="NavAction">
        <attr name="enterAnim" format="reference"/>
        <attr name="exitAnim" format="reference"/>
        <attr name="popEnterAnim" format="reference"/>
        <attr name="popExitAnim" format="reference"/>
        ...
    </declare-styleable>
</resources>

这意味着,可以定义相应的样式 - 并将这些定义为主题的一部分...

this means, one can define the according styles - and define these, as part of the theme...

可以在 styles.xml 中定义它们:

one can define them in styles.xml:

<style name="Theme.Default" parent="Theme.AppCompat.Light.NoActionBar">

    <!-- these should be the correct ones -->
    <item name="NavAction_enterAnim">@anim/default_enter_anim</item>
    <item name="NavAction_exitAnim">@anim/default_exit_anim</item>
    <item name="NavAction_popEnterAnim">@anim/default_pop_enter_anim</item>
    <item name="NavAction_popExitAnim">@anim/default_pop_exit_anim</item>

</style>


还可以在res/anim中定义默认动画:

  • 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

这篇关于如何为导航操作定义默认动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆