FileDocCategorySizeDatePackage
TransitionInflater.javaAPI DocAndroid 5.1 API14198Thu Mar 12 22:22:10 GMT 2015android.transition

TransitionInflater

public class TransitionInflater extends Object
This class inflates scenes and transitions from resource files. 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 final Class[]
sConstructorSignature
private static final android.util.ArrayMap
sConstructors
private android.content.Context
mContext
Constructors Summary
private TransitionInflater(android.content.Context context)


       
        mContext = context;
    
Methods Summary
private java.lang.ObjectcreateCustom(android.util.AttributeSet attrs, java.lang.Class expectedType, java.lang.String tag)

        String className = attrs.getAttributeValue(null, "class");

        if (className == null) {
            throw new InflateException(tag + " tag must have a 'class' attribute");
        }

        try {
            synchronized (sConstructors) {
                Constructor constructor = sConstructors.get(className);
                if (constructor == null) {
                    Class c = mContext.getClassLoader().loadClass(className)
                            .asSubclass(expectedType);
                    if (c != null) {
                        constructor = c.getConstructor(sConstructorSignature);
                        sConstructors.put(className, constructor);
                    }
                }

                return constructor.newInstance(mContext, attrs);
            }
        } catch (InstantiationException e) {
            throw new InflateException("Could not instantiate " + expectedType + " class " +
                    className, e);
        } catch (ClassNotFoundException e) {
            throw new InflateException("Could not instantiate " + expectedType + " class " +
                    className, e);
        } catch (InvocationTargetException e) {
            throw new InflateException("Could not instantiate " + expectedType + " class " +
                    className, e);
        } catch (NoSuchMethodException e) {
            throw new InflateException("Could not instantiate " + expectedType + " class " +
                    className, e);
        } catch (IllegalAccessException e) {
            throw new InflateException("Could not instantiate " + expectedType + " class " +
                    className, e);
        }
    
private TransitioncreateTransitionFromXml(org.xmlpull.v1.XmlPullParser parser, android.util.AttributeSet attrs, Transition parent)


        Transition transition = null;

        // Make sure we are on a start tag.
        int type;
        int depth = parser.getDepth();

        TransitionSet transitionSet = (parent instanceof TransitionSet)
                ? (TransitionSet) parent : null;

        while (((type=parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth)
                && type != XmlPullParser.END_DOCUMENT) {

            if (type != XmlPullParser.START_TAG) {
                continue;
            }

            String  name = parser.getName();
            if ("fade".equals(name)) {
                transition = new Fade(mContext, attrs);
            } else if ("changeBounds".equals(name)) {
                transition = new ChangeBounds(mContext, attrs);
            } else if ("slide".equals(name)) {
                transition = new Slide(mContext, attrs);
            } else if ("explode".equals(name)) {
                transition = new Explode(mContext, attrs);
            } else if ("changeImageTransform".equals(name)) {
                transition = new ChangeImageTransform(mContext, attrs);
            } else if ("changeTransform".equals(name)) {
                transition = new ChangeTransform(mContext, attrs);
            } else if ("changeClipBounds".equals(name)) {
                transition = new ChangeClipBounds(mContext, attrs);
            } else if ("autoTransition".equals(name)) {
                transition = new AutoTransition(mContext, attrs);
            } else if ("recolor".equals(name)) {
                transition = new Recolor(mContext, attrs);
            } else if ("changeScroll".equals(name)) {
                transition = new ChangeScroll(mContext, attrs);
            } else if ("transitionSet".equals(name)) {
                transition = new TransitionSet(mContext, attrs);
            } else if ("transition".equals(name)) {
                transition = (Transition) createCustom(attrs, Transition.class, "transition");
            } else if ("targets".equals(name)) {
                getTargetIds(parser, attrs, parent);
            } else if ("arcMotion".equals(name)) {
                parent.setPathMotion(new ArcMotion(mContext, attrs));
            } else if ("pathMotion".equals(name)) {
                parent.setPathMotion((PathMotion)createCustom(attrs, PathMotion.class, "pathMotion"));
            } else if ("patternPathMotion".equals(name)) {
                parent.setPathMotion(new PatternPathMotion(mContext, attrs));
            } else {
                throw new RuntimeException("Unknown scene name: " + parser.getName());
            }
            if (transition != null) {
                if (!parser.isEmptyElementTag()) {
                    createTransitionFromXml(parser, attrs, transition);
                }
                if (transitionSet != null) {
                    transitionSet.addTransition(transition);
                    transition = null;
                } else if (parent != null) {
                    throw new InflateException("Could not add transition to another transition.");
                }
            }
        }

        return transition;
    
private TransitionManagercreateTransitionManagerFromXml(org.xmlpull.v1.XmlPullParser parser, android.util.AttributeSet attrs, android.view.ViewGroup sceneRoot)


        // Make sure we are on a start tag.
        int type;
        int depth = parser.getDepth();
        TransitionManager transitionManager = null;

        while (((type=parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth)
                && type != XmlPullParser.END_DOCUMENT) {

            if (type != XmlPullParser.START_TAG) {
                continue;
            }

            String  name = parser.getName();
            if (name.equals("transitionManager")) {
                transitionManager = new TransitionManager();
            } else if (name.equals("transition") && (transitionManager != null)) {
                loadTransition(attrs, sceneRoot, transitionManager);
            } else {
                throw new RuntimeException("Unknown scene name: " + parser.getName());
            }
        }
        return transitionManager;
    
public static android.transition.TransitionInflaterfrom(android.content.Context context)
Obtains the TransitionInflater from the given context.

        return new TransitionInflater(context);
    
private voidgetTargetIds(org.xmlpull.v1.XmlPullParser parser, android.util.AttributeSet attrs, Transition transition)


        // Make sure we are on a start tag.
        int type;
        int depth = parser.getDepth();

        while (((type=parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth)
                && type != XmlPullParser.END_DOCUMENT) {

            if (type != XmlPullParser.START_TAG) {
                continue;
            }

            String  name = parser.getName();
            if (name.equals("target")) {
                TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.TransitionTarget);
                int id = a.getResourceId(R.styleable.TransitionTarget_targetId, 0);
                String transitionName;
                if (id != 0) {
                    transition.addTarget(id);
                } else if ((id = a.getResourceId(R.styleable.TransitionTarget_excludeId, 0)) != 0) {
                    transition.excludeTarget(id, true);
                } else if ((transitionName = a.getString(R.styleable.TransitionTarget_targetName))
                        != null) {
                    transition.addTarget(transitionName);
                } else if ((transitionName = a.getString(R.styleable.TransitionTarget_excludeName))
                        != null) {
                    transition.excludeTarget(transitionName, true);
                } else {
                    String className = a.getString(R.styleable.TransitionTarget_excludeClass);
                    try {
                        if (className != null) {
                            Class clazz = Class.forName(className);
                            transition.excludeTarget(clazz, true);
                        } else if ((className =
                                a.getString(R.styleable.TransitionTarget_targetClass)) != null) {
                            Class clazz = Class.forName(className);
                            transition.addTarget(clazz);
                        }
                    } catch (ClassNotFoundException e) {
                        throw new RuntimeException("Could not create " + className, e);
                    }
                }
            } else {
                throw new RuntimeException("Unknown scene name: " + parser.getName());
            }
        }
    
public TransitioninflateTransition(int resource)
Loads a {@link Transition} object from a resource

param
resource The resource id of the transition to load
return
The loaded Transition object
throws
android.content.res.Resources.NotFoundException when the transition cannot be loaded

        XmlResourceParser parser =  mContext.getResources().getXml(resource);
        try {
            return createTransitionFromXml(parser, Xml.asAttributeSet(parser), null);
        } catch (XmlPullParserException e) {
            InflateException ex = new InflateException(e.getMessage());
            ex.initCause(e);
            throw ex;
        } catch (IOException e) {
            InflateException ex = new InflateException(
                    parser.getPositionDescription()
                            + ": " + e.getMessage());
            ex.initCause(e);
            throw ex;
        } finally {
            parser.close();
        }
    
public TransitionManagerinflateTransitionManager(int resource, android.view.ViewGroup sceneRoot)
Loads a {@link TransitionManager} object from a resource

param
resource The resource id of the transition manager to load
return
The loaded TransitionManager object
throws
android.content.res.Resources.NotFoundException when the transition manager cannot be loaded

        XmlResourceParser parser =  mContext.getResources().getXml(resource);
        try {
            return createTransitionManagerFromXml(parser, Xml.asAttributeSet(parser), sceneRoot);
        } catch (XmlPullParserException e) {
            InflateException ex = new InflateException(e.getMessage());
            ex.initCause(e);
            throw ex;
        } catch (IOException e) {
            InflateException ex = new InflateException(
                    parser.getPositionDescription()
                            + ": " + e.getMessage());
            ex.initCause(e);
            throw ex;
        } finally {
            parser.close();
        }
    
private voidloadTransition(android.util.AttributeSet attrs, android.view.ViewGroup sceneRoot, TransitionManager transitionManager)


        TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.TransitionManager);
        int transitionId = a.getResourceId(R.styleable.TransitionManager_transition, -1);
        int fromId = a.getResourceId(R.styleable.TransitionManager_fromScene, -1);
        Scene fromScene = (fromId < 0) ? null: Scene.getSceneForLayout(sceneRoot, fromId, mContext);
        int toId = a.getResourceId(R.styleable.TransitionManager_toScene, -1);
        Scene toScene = (toId < 0) ? null : Scene.getSceneForLayout(sceneRoot, toId, mContext);

        if (transitionId >= 0) {
            Transition transition = inflateTransition(transitionId);
            if (transition != null) {
                if (toScene == null) {
                    throw new RuntimeException("No toScene for transition ID " + transitionId);
                }
                if (fromScene == null) {
                    transitionManager.setTransition(toScene, transition);
                } else {
                    transitionManager.setTransition(fromScene, toScene, transition);
                }
            }
        }
        a.recycle();