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

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

问题描述

我正在使用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

为了更改此行为,您必须使用自定义

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

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

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

可以使用 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,它扩展了类

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>

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

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