导航中目的地之间的动画转换
Animate transitions between destinations in navigation
如何制作导航中目的地之间的动画转换?
这就是我进入下一个片段的方式
Handler().postDelayed({
findNavController().navigate(R.id.nav_food_text_analysis,null)
},6000)
这是两个动画 xml 文件
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="100%" android:toXDelta="0%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration = "1000"/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0%" android:toXDelta="100%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration = "1000"/>
</set>
这是我的导航xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mobile_navigation"
app:startDestination="@id/welcome_fragment">
<fragment
android:id="@+id/nav_food_text_analysis"
android:name="com.example.nutritionfacts.ui.foodTextAnalysis.FoodAnalysisFragment"
android:label="Food analysis"
tools:layout="@layout/fragment_food_text_analysis" />
<fragment
android:id="@+id/nav_recipe_analysis"
android:name="com.example.nutritionfacts.ui.recipeAnalysis.RecipeAnalysis"
android:label="Recipe analysis"
tools:layout="@layout/fragment_recipe_analysis" />
<fragment
android:id="@+id/welcome_fragment"
android:name="com.example.nutritionfacts.ui.welcomeScreen.WelcomeScreen"
android:label="welcome screen"
tools:layout="@layout/fragment_welcome_screen" >
<action
android:id="@+id/action_welcome_fragment_to_nav_food_text_analysis2"
app:destination="@id/nav_food_text_analysis"
app:enterAnim="@anim/enter_from_right"
app:exitAnim="@anim/exit_to_right"
app:popExitAnim="@anim/fragment_open_exit" />
</fragment>
</navigation>
我需要它是这样的
gif
我用这段代码得到了我需要的结果
Handler().postDelayed({
(activity as AppCompatActivity).supportFragmentManager
.beginTransaction()
.setCustomAnimations(
R.anim.enter_from_right,
R.anim.exit_to_right
)
.replace(R.id.nav_host_fragment, FoodAnalysisFragment())
.commit()
},6000)
但是我现在需要使用导航来切换片段
请帮帮我
您正在使用 navigate(R.id.nav_food_text_analysis, null)
- 直接使用目标 ID,这意味着没有动画 运行。
如果您想通过您的操作进行导航,您需要使用 navigate(R.id.action_welcome_fragment_to_nav_food_text_analysis2, null)
并且 运行 与该操作关联的 enterAnim
和 exitAnim
。
如何制作导航中目的地之间的动画转换?
这就是我进入下一个片段的方式
Handler().postDelayed({
findNavController().navigate(R.id.nav_food_text_analysis,null)
},6000)
这是两个动画 xml 文件
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="100%" android:toXDelta="0%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration = "1000"/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0%" android:toXDelta="100%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration = "1000"/>
</set>
这是我的导航xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mobile_navigation"
app:startDestination="@id/welcome_fragment">
<fragment
android:id="@+id/nav_food_text_analysis"
android:name="com.example.nutritionfacts.ui.foodTextAnalysis.FoodAnalysisFragment"
android:label="Food analysis"
tools:layout="@layout/fragment_food_text_analysis" />
<fragment
android:id="@+id/nav_recipe_analysis"
android:name="com.example.nutritionfacts.ui.recipeAnalysis.RecipeAnalysis"
android:label="Recipe analysis"
tools:layout="@layout/fragment_recipe_analysis" />
<fragment
android:id="@+id/welcome_fragment"
android:name="com.example.nutritionfacts.ui.welcomeScreen.WelcomeScreen"
android:label="welcome screen"
tools:layout="@layout/fragment_welcome_screen" >
<action
android:id="@+id/action_welcome_fragment_to_nav_food_text_analysis2"
app:destination="@id/nav_food_text_analysis"
app:enterAnim="@anim/enter_from_right"
app:exitAnim="@anim/exit_to_right"
app:popExitAnim="@anim/fragment_open_exit" />
</fragment>
</navigation>
我需要它是这样的 gif
我用这段代码得到了我需要的结果
Handler().postDelayed({
(activity as AppCompatActivity).supportFragmentManager
.beginTransaction()
.setCustomAnimations(
R.anim.enter_from_right,
R.anim.exit_to_right
)
.replace(R.id.nav_host_fragment, FoodAnalysisFragment())
.commit()
},6000)
但是我现在需要使用导航来切换片段 请帮帮我
您正在使用 navigate(R.id.nav_food_text_analysis, null)
- 直接使用目标 ID,这意味着没有动画 运行。
如果您想通过您的操作进行导航,您需要使用 navigate(R.id.action_welcome_fragment_to_nav_food_text_analysis2, null)
并且 运行 与该操作关联的 enterAnim
和 exitAnim
。