当前位置: 代码迷 >> 综合 >> android学习笔记:activity(8)-navigate between fragments using animations
  详细解决方案

android学习笔记:activity(8)-navigate between fragments using animations

热度:57   发布时间:2023-11-20 22:20:57.0

1.The Fragment API provides two ways to use motion(运动) effects and transformations to visually(形象的) connect fragments during navigation.One of these is the Animation Framework,which uses both Animation and Animator(动画片制作者).The others the Transition Framework,which includes shared element transitions.(note: in this topic,we use the term animation to describe effects in the animation Framework,and we use the term transition to describe effects in the transition Framework.these two frameworks are mutually exclusive and should nor be used at the same time.

2.this animations can be defined in the res/anim directory.

<!-- res/anim/fade_out.xml -->
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"android:duration="@android:integer/config_shortAnimTime"android:interpolator="@android:anim/decelerate_interpolator"android:fromAlpha="1"android:toAlpha="0" />
<!-- res/anim/slide_in.xml -->
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"android:duration="@android:integer/config_shortAnimTime"android:interpolator="@android:anim/decelerate_interpolator"android:fromXDelta="100%"android:toXDelta="0%" />

 (Note:It is strongly recommended to use transitions for effects taht involve more than one type of animation as there are known issues with using nested AnimationSet instances)

<!-- res/anim/slide_out.xml -->
<translate xmlns:android="http://schemas.android.com/apk/res/android"android:duration="@android:integer/config_shortAnimTime"android:interpolator="@android:anim/decelerate_interpolator"android:fromXDelta="0%"android:toXDelta="100%" />
<!-- res/anim/fade_in.xml -->
<alpha xmlns:android="http://schemas.android.com/apk/res/android"android:duration="@android:integer/config_shortAnimTime"android:interpolator="@android:anim/decelerate_interpolator"android:fromAlpha="0"android:toAlpha="1" />
val fragment = FragmentB()
supportFragmentManager.commit {setCustomAnimations(enter = R.anim.slide_in,exit = R.anim.fade_out,popEnter = R.anim.fade_in,popExit = R.anim.slide_out)replace(R.id.fragment_container, fragment)addToBackStack(null)
}

note:FragmentTransaction.setCustomAnimations() applies the custom animations to all future fragment operations in the FragmentTransaction.Previous operations in the transaction are unaffected

3.You can also use transitions to define enter and exit effects.These transitons can be defined in XML resource files.For example,you might want the current fragment to fade out and the new fragment to slide in from the right edge of the screen.These transitions can be defined as follows:

<!-- res/transition/fade.xml -->
<fade xmlns:android="http://schemas.android.com/apk/res/android"android:duration="@android:integer/config_shortAnimTime"/>
<!-- res/transition/slide_right.xml -->
<slide xmlns:android="http://schemas.android.com/apk/res/android"android:duration="@android:integer/config_shortAnimTime"android:slideEdge="right" />

Once you.ve defined your transitions,apply them by calling setRnterTransition()on the entering fragment and setExitTransition() on the exiting fragment,passing in your inflated transition resources by their resource ID ,as shown in the following example:

class FragmentA : Fragment() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)val inflater = TransitionInflater.from(requireContext())exitTransition = inflater.inflateTransition(R.transition.fade)}
}class FragmentB : Fragment() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)val inflater = TransitionInflater.from(requireContext())enterTransition = inflater.inflateTransition(R.transition.slide_right)}
}

4.第二种过度动画效果没有理解,等后面再去理解

  相关解决方案