Tuesday, July 28, 2015

Android Activity Transition API

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.
  • Enable the new transition APIs by requesting theWindow.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 nulland Fade 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 thestartActivity(Context, Bundle) method and pass the following Bundle as the second argument:
    ActivityOptions.makeSceneTransitionAnimation(activity, pairs).toBundle();
    
    where pairs is an array of Pair<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 of finish().
  • 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 thesetWindowAllowEnterTransitionOverlap() 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 exitenterreenter, 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 the addSharedElement(View, String) method before the transaction is committed.

1 - Content Transition 

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 when A starts B.
  • setEnterTransition() - B's enter transition animates transitioning views into the scene when A starts B.
  • setReturnTransition() - B's return transition animates transitioning views out of the scene when B returns to A.
  • setReenterTransition() - A's reenter transition animates transitioning views into the scene when B returns to A.

Content Transitions Under-The-Hood

when Activity A starts Activity Bthe following sequence of events occurs:1
  1. Activity A calls startActivity().
    1. The framework traverses A's view hierarchy and determines the set of transitioning views that will exit the scene when A's exit transition is run.
    2. A's exit transition captures the start state for the transitioning views in A.
    3. The framework sets all transitioning views in A to INVISIBLE.
    4. On the next display frame, A's exit transition captures the end state for the transitioning views in A.
    5. A's exit transition compares the start and end state of each transitioning view and creates an Animator based on the differences. The Animator is run and the transitioning views exit the scene.
  2. Activity B is started.
    1. The framework traverses B's view hierarchy and determines the set of transitioning views that will enter the scene when B's enter transition is run. The transitioning views are initially set to INVISIBLE.
    2. B's enter transition captures the start state for the transitioning views in B.
    3. The framework sets all transitioning views in B to VISIBLE.
    4. On the next display frame, B's enter transition captures the end state for the transitioning views in B.
    5. B's enter transition compares the start and end state of each transitioning view and creates an Animator based on the differences. The Animator is run and the transitioning views enter the scene.
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—FadeSlide, 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