There are two type of Activity Transitions-
1- Content Transition
2- Shared element transition
1- Content Transition : A content transition determines how an activity's non-shared views—called transitioning views—enter or exit the activity scene.
2- Shared element transition: A shared element transition determines how an activity's shared elements (also called hero views) are animated between two activities.
The following sections will have a good introductions.
1- Content Transition
2- Shared element transition
1- Content Transition : A content transition determines how an activity's non-shared views—called transitioning views—enter or exit the activity scene.
2- Shared element transition: A shared element transition determines how an activity's shared elements (also called hero views) are animated between two activities.
The following sections will have a good introductions.
- Enable the new transition APIs by requesting the
Window.FEATURE_ACTIVITY_TRANSITIONS
window feature in your called and calling Activities, either programatically or in your theme's XML.3 Material-themed applications have this flag enabled by default. - Set exit and enter content transitions for your calling and called activities respectively. Material-themed applications have their exit and enter content transitions set to
null
andFade
respectively by default. If the reenter or return transitions are not explicitly set, the activity's exit and enter content transitions respectively will be used in their place instead. - Set exit and enter shared element transitions for your calling and called activities respectively. Material-themed applications have their shared element exit and enter transitions set to
@android:transition/move
by default. If the reenter or returntransitions are not explicitly set, the activity's exit and enter shared element transitions respectively will be used in their place instead. - To start an Activity transition with content transitions and shared elements, call the
startActivity(Context, Bundle)
method and pass the followingBundle
as the second argument:ActivityOptions.makeSceneTransitionAnimation(activity, pairs).toBundle();
wherepairs
is an array ofPair<View, String>
objects listing the shared element views and names that you'd like to share between activities.4 Don't forget to give your shared elements unique transition names, either programatically or in XML. Otherwise, the transition will not work properly! - To programatically trigger a return transition, call
finishAfterTransition()
instead offinish()
. - By default, material-themed applications have their enter/return content transitions started a tiny bit before their exit/reenter content transitions complete, creating a small overlap that makes the overall effect more seamless and dramatic. If you wish to explicitly disable this behavior, you can do so by calling the
setWindowAllowEnterTransitionOverlap()
andsetWindowAllowReturnTransitionOverlap()
methods or by setting the corresponding attributes in your theme's XML.
Introducing the Fragment Transition API
If you are working with Fragment transitions, the API is similar with a few small differences:
- Content exit, enter, reenter, and return transitions should be set by calling the corresponding methods in the
Fragment
class or as attributes in your Fragment's XML tag. - Shared element enter and return transitions should be set by calling the corresponding methods in the
Fragment
class or as attributes in your Fragment's XML. - Whereas Activity transitions are triggered by explicit calls to
startActivity()
andfinishAfterTransition()
, Fragment transitions are triggered automatically when a fragment is added, removed, attached, detached, shown, or hidden by aFragmentTransaction
. - Shared elements should be specified as part of the
FragmentTransaction
by calling theaddSharedElement(View, String)
method before the transaction is committed.
1 - Content Transition
A content transition determines how the non-shared views—called transitioning views—enter or exit the scene during an Activity or Fragment transition. Motivated by Google's new Material Design language, content transitions allow us to coordinate the entrance and exit of each Activity/Fragment's views, making the act of switching between screens smooth and effortless. Beginning with Android Lollipop, content transitions can be set programatically by calling the following
Window
and Fragment
methods:setExitTransition()
-A
's exit transition animates transitioning views out of the scene whenA
startsB
.setEnterTransition()
-B
's enter transition animates transitioning views into the scene whenA
startsB
.setReturnTransition()
-B
's return transition animates transitioning views out of the scene whenB
returns toA
.setReenterTransition()
-A
's reenter transition animates transitioning views into the scene whenB
returns toA
.
Content Transitions Under-The-Hood
when Activity
A
starts Activity B
the following sequence of events occurs:1- Activity
A
callsstartActivity()
.- The framework traverses
A
's view hierarchy and determines the set of transitioning views that will exit the scene whenA
's exit transition is run. A
's exit transition captures the start state for the transitioning views inA
.- The framework sets all transitioning views in
A
toINVISIBLE
. - On the next display frame,
A
's exit transition captures the end state for the transitioning views inA
. A
's exit transition compares the start and end state of each transitioning view and creates anAnimator
based on the differences. TheAnimator
is run and the transitioning views exit the scene.
- The framework traverses
- Activity
B
is started.- The framework traverses
B
's view hierarchy and determines the set of transitioning views that will enter the scene whenB
's enter transition is run. The transitioning views are initially set toINVISIBLE
. B
's enter transition captures the start state for the transitioning views inB
.- The framework sets all transitioning views in
B
toVISIBLE
. - On the next display frame,
B
's enter transition captures the end state for the transitioning views inB
. B
's enter transition compares the start and end state of each transitioning view and creates anAnimator
based on the differences. TheAnimator
is run and the transitioning views enter the scene.
- The framework traverses
By toggling each transitioning view's visibility between
INVISIBLE
and VISIBLE
, the framework ensures that the content transition is given the state information it needs to create the desired animation. Clearly all content Transition
objects then must at the very least be able to capture and record each transitioning view's visibility in both its start and end states. Fortunately, the abstract Visibility
class already does this work for you: subclasses ofVisibility
need only implement the onAppear()
and onDisappear()
factory methods, in which they must create and return an Animator
that will either animate the views into or out of the scene. As of API 21, three concrete Visibility
implementations exist—Fade
, Slide
, and Explode
—all of which can be used to create Activity and Fragment content transitions. If necessary, custom Visibility
classes may be implemented as well; doing so will be covered in a future blog post.
No comments:
Post a Comment