FileDocCategorySizeDatePackage
TransitionManager.javaAPI DocAndroid 5.1 API17854Thu Mar 12 22:22:10 GMT 2015android.transition

TransitionManager

public class TransitionManager extends Object
This class manages the set of transitions that fire when there is a change of {@link Scene}. To use the manager, add scenes along with transition objects with calls to {@link #setTransition(Scene, Transition)} or {@link #setTransition(Scene, Scene, Transition)}. Setting specific transitions for scene changes is not required; by default, a Scene change will use {@link AutoTransition} to do something reasonable for most situations. Specifying other transitions for particular scene changes is only necessary if the application wants different transition behavior in these situations.

TransitionManagers can be declared in XML resource files inside the res/transition directory. TransitionManager resources consist of the transitionManagertag name, containing one or more transition tags, each of which describe the relationship of that transition to the from/to scene information in that tag. For example, here is a resource file that declares several scene transitions:

{@sample development/samples/ApiDemos/res/transition/transitions_mgr.xml TransitionManager}

For each of the fromScene and toScene attributes, there is a reference to a standard XML layout file. This is equivalent to creating a scene from a layout in code by calling {@link Scene#getSceneForLayout(ViewGroup, int, Context)}. For the transition attribute, there is a reference to a resource file in the res/transition directory which describes that transition.

Information on XML resource descriptions for transitions can be found for {@link android.R.styleable#Transition}, {@link android.R.styleable#TransitionSet}, {@link android.R.styleable#TransitionTarget}, {@link android.R.styleable#Fade}, and {@link android.R.styleable#TransitionManager}.

Fields Summary
private static String
LOG_TAG
private static Transition
sDefaultTransition
private static final String[]
EMPTY_STRINGS
android.util.ArrayMap
mSceneTransitions
android.util.ArrayMap
mScenePairTransitions
private static ThreadLocal
sRunningTransitions
private static ArrayList
sPendingTransitions
Constructors Summary
Methods Summary
public static voidbeginDelayedTransition(android.view.ViewGroup sceneRoot)
Convenience method to animate, using the default transition, to a new scene defined by all changes within the given scene root between calling this method and the next rendering frame. Equivalent to calling {@link #beginDelayedTransition(ViewGroup, Transition)} with a value of null for the transition parameter.

param
sceneRoot The root of the View hierarchy to run the transition on.

        beginDelayedTransition(sceneRoot, null);
    
public static voidbeginDelayedTransition(android.view.ViewGroup sceneRoot, Transition transition)
Convenience method to animate to a new scene defined by all changes within the given scene root between calling this method and the next rendering frame. Calling this method causes TransitionManager to capture current values in the scene root and then post a request to run a transition on the next frame. At that time, the new values in the scene root will be captured and changes will be animated. There is no need to create a Scene; it is implied by changes which take place between calling this method and the next frame when the transition begins.

Calling this method several times before the next frame (for example, if unrelated code also wants to make dynamic changes and run a transition on the same scene root), only the first call will trigger capturing values and exiting the current scene. Subsequent calls to the method with the same scene root during the same frame will be ignored.

Passing in null for the transition parameter will cause the TransitionManager to use its default transition.

param
sceneRoot The root of the View hierarchy to run the transition on.
param
transition The transition to use for this change. A value of null causes the TransitionManager to use the default transition.

        if (!sPendingTransitions.contains(sceneRoot) && sceneRoot.isLaidOut()) {
            if (Transition.DBG) {
                Log.d(LOG_TAG, "beginDelayedTransition: root, transition = " +
                        sceneRoot + ", " + transition);
            }
            sPendingTransitions.add(sceneRoot);
            if (transition == null) {
                transition = sDefaultTransition;
            }
            final Transition transitionClone = transition.clone();
            sceneChangeSetup(sceneRoot, transitionClone);
            Scene.setCurrentScene(sceneRoot, null);
            sceneChangeRunTransition(sceneRoot, transitionClone);
        }
    
private static voidchangeScene(Scene scene, Transition transition)
This is where all of the work of a transition/scene-change is orchestrated. This method captures the start values for the given transition, exits the current Scene, enters the new scene, captures the end values for the transition, and finally plays the resulting values-populated transition.

param
scene The scene being entered
param
transition The transition to play for this scene change


        final ViewGroup sceneRoot = scene.getSceneRoot();

        Transition transitionClone = null;
        if (transition != null) {
            transitionClone = transition.clone();
            transitionClone.setSceneRoot(sceneRoot);
        }

        Scene oldScene = Scene.getCurrentScene(sceneRoot);
        if (oldScene != null && transitionClone != null &&
                oldScene.isCreatedFromLayoutResource()) {
            transitionClone.setCanRemoveViews(true);
        }

        sceneChangeSetup(sceneRoot, transitionClone);

        scene.enter();

        sceneChangeRunTransition(sceneRoot, transitionClone);
    
public static TransitiongetDefaultTransition()
Gets the current default transition. The initial value is an {@link AutoTransition} instance.

return
The current default transition.
see
#setDefaultTransition(Transition)
hide
pending later changes

        return sDefaultTransition;
    
private static android.util.ArrayMapgetRunningTransitions()

        WeakReference<ArrayMap<ViewGroup, ArrayList<Transition>>> runningTransitions =
                sRunningTransitions.get();
        if (runningTransitions == null || runningTransitions.get() == null) {
            ArrayMap<ViewGroup, ArrayList<Transition>> transitions =
                    new ArrayMap<ViewGroup, ArrayList<Transition>>();
            runningTransitions = new WeakReference<ArrayMap<ViewGroup, ArrayList<Transition>>>(
                    transitions);
            sRunningTransitions.set(runningTransitions);
        }
        return runningTransitions.get();
    
private TransitiongetTransition(Scene scene)
Returns the Transition for the given scene being entered. The result depends not only on the given scene, but also the scene which the {@link Scene#getSceneRoot() sceneRoot} of the Scene is currently in.

param
scene The scene being entered
return
The Transition to be used for the given scene change. If no Transition was specified for this scene change, the default transition will be used instead.

        Transition transition = null;
        ViewGroup sceneRoot = scene.getSceneRoot();
        if (sceneRoot != null) {
            // TODO: cached in Scene instead? long-term, cache in View itself
            Scene currScene = Scene.getCurrentScene(sceneRoot);
            if (currScene != null) {
                ArrayMap<Scene, Transition> sceneTransitionMap = mScenePairTransitions.get(scene);
                if (sceneTransitionMap != null) {
                    transition = sceneTransitionMap.get(currScene);
                    if (transition != null) {
                        return transition;
                    }
                }
            }
        }
        transition = mSceneTransitions.get(scene);
        return (transition != null) ? transition : sDefaultTransition;
    
public static voidgo(Scene scene)
Convenience method to simply change to the given scene using the default transition for TransitionManager.

param
scene The Scene to change to

        changeScene(scene, sDefaultTransition);
    
public static voidgo(Scene scene, Transition transition)
Convenience method to simply change to the given scene using the given transition.

Passing in null for the transition parameter will result in the scene changing without any transition running, and is equivalent to calling {@link Scene#exit()} on the scene root's current scene, followed by {@link Scene#enter()} on the scene specified by the scene parameter.

param
scene The Scene to change to
param
transition The transition to use for this scene change. A value of null causes the scene change to happen with no transition.

        changeScene(scene, transition);
    
private static voidsceneChangeRunTransition(android.view.ViewGroup sceneRoot, Transition transition)

        if (transition != null && sceneRoot != null) {
            MultiListener listener = new MultiListener(transition, sceneRoot);
            sceneRoot.addOnAttachStateChangeListener(listener);
            sceneRoot.getViewTreeObserver().addOnPreDrawListener(listener);
        }
    
private static voidsceneChangeSetup(android.view.ViewGroup sceneRoot, Transition transition)


        // Capture current values
        ArrayList<Transition> runningTransitions = getRunningTransitions().get(sceneRoot);

        if (runningTransitions != null && runningTransitions.size() > 0) {
            for (Transition runningTransition : runningTransitions) {
                runningTransition.pause(sceneRoot);
            }
        }

        if (transition != null) {
            transition.captureValues(sceneRoot, true);
        }

        // Notify previous scene that it is being exited
        Scene previousScene = Scene.getCurrentScene(sceneRoot);
        if (previousScene != null) {
            previousScene.exit();
        }
    
public voidsetDefaultTransition(Transition transition)
Sets the transition to be used for any scene change for which no other transition is explicitly set. The initial value is an {@link AutoTransition} instance.

param
transition The default transition to be used for scene changes.
hide
pending later changes



                                                  
        
        sDefaultTransition = transition;
    
public voidsetTransition(Scene scene, Transition transition)
Sets a specific transition to occur when the given scene is entered.

param
scene The scene which, when applied, will cause the given transition to run.
param
transition The transition that will play when the given scene is entered. A value of null will result in the default behavior of using the default transition instead.

        mSceneTransitions.put(scene, transition);
    
public voidsetTransition(Scene fromScene, Scene toScene, Transition transition)
Sets a specific transition to occur when the given pair of scenes is exited/entered.

param
fromScene The scene being exited when the given transition will be run
param
toScene The scene being entered when the given transition will be run
param
transition The transition that will play when the given scene is entered. A value of null will result in the default behavior of using the default transition instead.

        ArrayMap<Scene, Transition> sceneTransitionMap = mScenePairTransitions.get(toScene);
        if (sceneTransitionMap == null) {
            sceneTransitionMap = new ArrayMap<Scene, Transition>();
            mScenePairTransitions.put(toScene, sceneTransitionMap);
        }
        sceneTransitionMap.put(fromScene, transition);
    
public voidtransitionTo(Scene scene)
Change to the given scene, using the appropriate transition for this particular scene change (as specified to the TransitionManager, or the default if no such transition exists).

param
scene The Scene to change to

        // Auto transition if there is no transition declared for the Scene, but there is
        // a root or parent view
        changeScene(scene, getTransition(scene));