FileDocCategorySizeDatePackage
AnimationUtils.javaAPI DocAndroid 1.5 API11752Wed May 06 22:41:56 BST 2009android.view.animation

AnimationUtils

public class AnimationUtils extends Object
Defines common utilities for working with animations.

Fields Summary
Constructors Summary
Methods Summary
private static AnimationcreateAnimationFromXml(android.content.Context c, org.xmlpull.v1.XmlPullParser parser)

        return createAnimationFromXml(c, parser, null, Xml.asAttributeSet(parser));
    
private static AnimationcreateAnimationFromXml(android.content.Context c, org.xmlpull.v1.XmlPullParser parser, AnimationSet parent, android.util.AttributeSet attrs)

        
        Animation anim = null;
 
        // Make sure we are on a start tag.
        int type = parser.getEventType();
        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("set")) {
                anim = new AnimationSet(c, attrs);
                createAnimationFromXml(c, parser, (AnimationSet)anim, attrs);
            } else if (name.equals("alpha")) {
                anim = new AlphaAnimation(c, attrs);
            } else if (name.equals("scale")) {
                anim = new ScaleAnimation(c, attrs);
            }  else if (name.equals("rotate")) {
                anim = new RotateAnimation(c, attrs);
            }  else if (name.equals("translate")) {
                anim = new TranslateAnimation(c, attrs);
            } else {
                throw new RuntimeException("Unknown animation name: " + parser.getName());
            }

            if (parent != null) {
                parent.addAnimation(anim);
            }
        }
    
        return anim;

    
private static InterpolatorcreateInterpolatorFromXml(android.content.Context c, org.xmlpull.v1.XmlPullParser parser)

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

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

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

            AttributeSet attrs = Xml.asAttributeSet(parser);
            
            String  name = parser.getName();
    
            
            if (name.equals("linearInterpolator")) {
                interpolator = new LinearInterpolator(c, attrs);
            } else if (name.equals("accelerateInterpolator")) {
                interpolator = new AccelerateInterpolator(c, attrs);
            } else if (name.equals("decelerateInterpolator")) {
                interpolator = new DecelerateInterpolator(c, attrs);
            }  else if (name.equals("accelerateDecelerateInterpolator")) {
                interpolator = new AccelerateDecelerateInterpolator(c, attrs);
            }  else if (name.equals("cycleInterpolator")) {
                interpolator = new CycleInterpolator(c, attrs);
            } else {
                throw new RuntimeException("Unknown interpolator name: " + parser.getName());
            }

        }
    
        return interpolator;

    
private static LayoutAnimationControllercreateLayoutAnimationFromXml(android.content.Context c, org.xmlpull.v1.XmlPullParser parser)

        return createLayoutAnimationFromXml(c, parser,
                Xml.asAttributeSet(parser));
    
private static LayoutAnimationControllercreateLayoutAnimationFromXml(android.content.Context c, org.xmlpull.v1.XmlPullParser parser, android.util.AttributeSet attrs)


        LayoutAnimationController controller = null;

        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 ("layoutAnimation".equals(name)) {
                controller = new LayoutAnimationController(c, attrs);
            } else if ("gridLayoutAnimation".equals(name)) {
                controller = new GridLayoutAnimationController(c, attrs);
            } else {
                throw new RuntimeException("Unknown layout animation name: " +
                        name);
            }
        }

        return controller;
    
public static longcurrentAnimationTimeMillis()
Returns the current animation time in milliseconds. This time should be used when invoking {@link Animation#setStartTime(long)}. Refer to {@link android.os.SystemClock} for more information about the different available clocks. The clock used by this method is not the "wall" clock (it is not {@link System#currentTimeMillis}).

return
the current animation time in milliseconds
see
android.os.SystemClock

        return SystemClock.uptimeMillis();
    
public static AnimationloadAnimation(android.content.Context context, int id)
Loads an {@link Animation} object from a resource

param
context Application context used to access resources
param
id The resource id of the animation to load
return
The animation object reference by the specified id
throws
NotFoundException when the animation cannot be loaded


        XmlResourceParser parser = null;
        try {
            parser = context.getResources().getAnimation(id);
            return createAnimationFromXml(context, parser);
        } catch (XmlPullParserException ex) {
            NotFoundException rnf = new NotFoundException(
                    "Can't load animation resource ID #0x"
                            + Integer.toHexString(id));
            rnf.initCause(ex);
            throw rnf;
        } catch (IOException ex) {
            NotFoundException rnf = new NotFoundException(
                    "Can't load animation resource ID #0x"
                            + Integer.toHexString(id));
            rnf.initCause(ex);
            throw rnf;
        } finally {
            if (parser != null) parser.close();
        }
    
public static InterpolatorloadInterpolator(android.content.Context context, int id)
Loads an {@link Interpolator} object from a resource

param
context Application context used to access resources
param
id The resource id of the animation to load
return
The animation object reference by the specified id
throws
NotFoundException


        XmlResourceParser parser = null;
        try {
            parser = context.getResources().getAnimation(id);
            return createInterpolatorFromXml(context, parser);
        } catch (XmlPullParserException ex) {
            NotFoundException rnf = new NotFoundException(
                    "Can't load animation resource ID #0x"
                            + Integer.toHexString(id));
            rnf.initCause(ex);
            throw rnf;
        } catch (IOException ex) {
            NotFoundException rnf = new NotFoundException(
                    "Can't load animation resource ID #0x"
                            + Integer.toHexString(id));
            rnf.initCause(ex);
            throw rnf;
        } finally {
            if (parser != null) parser.close();
        }

    
public static LayoutAnimationControllerloadLayoutAnimation(android.content.Context context, int id)

        
        XmlResourceParser parser = null;
        try {
            parser = context.getResources().getAnimation(id);
            return createLayoutAnimationFromXml(context, parser);
        } catch (XmlPullParserException ex) {
            NotFoundException rnf = new NotFoundException(
                    "Can't load animation resource ID #0x" +
                            Integer.toHexString(id));
            rnf.initCause(ex);
            throw rnf;
        } catch (IOException ex) {
            NotFoundException rnf = new NotFoundException(
                    "Can't load animation resource ID #0x" +
                            Integer .toHexString(id));
            rnf.initCause(ex);
            throw rnf;
        } finally {
            if (parser != null) parser.close();
        }
    
public static AnimationmakeInAnimation(android.content.Context c, boolean fromLeft)
Make an animation for objects becoming visible. Uses a slide and fade effect.

param
c Context for loading resources
param
fromLeft is the object to be animated coming from the left
return
The new animation

        
        Animation a;
        if (fromLeft) {
            a = AnimationUtils.loadAnimation(c, com.android.internal.R.anim.slide_in_left);
        } else {
            a = AnimationUtils.loadAnimation(c, com.android.internal.R.anim.slide_in_right);
        }

        a.setInterpolator(new DecelerateInterpolator());
        a.setStartTime(currentAnimationTimeMillis());
        return a;
    
public static AnimationmakeInChildBottomAnimation(android.content.Context c)
Make an animation for objects becoming visible. Uses a slide up and fade effect.

param
c Context for loading resources
return
The new animation

   

        Animation a;
        a = AnimationUtils.loadAnimation(c, com.android.internal.R.anim.slide_in_child_bottom);
        a.setInterpolator(new AccelerateInterpolator());
        a.setStartTime(currentAnimationTimeMillis());
        return a;
    
public static AnimationmakeOutAnimation(android.content.Context c, boolean toRight)
Make an animation for objects becoming invisible. Uses a slide and fade effect.

param
c Context for loading resources
param
toRight is the object to be animated exiting to the right
return
The new animation

   
        
        Animation a;
        if (toRight) {
            a = AnimationUtils.loadAnimation(c, com.android.internal.R.anim.slide_out_right);
        } else {
            a = AnimationUtils.loadAnimation(c, com.android.internal.R.anim.slide_out_left);
        }
        
        a.setInterpolator(new AccelerateInterpolator());
        a.setStartTime(currentAnimationTimeMillis());
        return a;