diff options
author | Chet Haase <chet@google.com> | 2010-07-22 14:44:59 -0700 |
---|---|---|
committer | Chet Haase <chet@google.com> | 2010-07-28 17:22:14 -0700 |
commit | f54a8d7c479485174941c38f151ea7083c658da3 (patch) | |
tree | 101ab5e8072c1dbbf2d9432f36a9e6ece369ef80 /core | |
parent | 2af53220c9374b727e4331caba6df74c3ad20c21 (diff) | |
download | frameworks_base-f54a8d7c479485174941c38f151ea7083c658da3.zip frameworks_base-f54a8d7c479485174941c38f151ea7083c658da3.tar.gz frameworks_base-f54a8d7c479485174941c38f151ea7083c658da3.tar.bz2 |
Adding xml declarations for new animation framework
Change-Id: Ic789e47790cf24d1c4b3bcbe9048b992ab93517b
Diffstat (limited to 'core')
-rwxr-xr-x | core/java/android/animation/Animator.java | 73 | ||||
-rw-r--r-- | core/java/android/animation/PropertyAnimator.java | 30 | ||||
-rw-r--r-- | core/java/android/animation/Sequencer.java | 21 | ||||
-rw-r--r-- | core/java/android/view/animation/AnimationUtils.java | 124 | ||||
-rwxr-xr-x | core/res/res/values/attrs.xml | 63 | ||||
-rw-r--r-- | core/res/res/values/public.xml | 5 |
6 files changed, 310 insertions, 6 deletions
diff --git a/core/java/android/animation/Animator.java b/core/java/android/animation/Animator.java index bf3596b..d8c6fff 100755 --- a/core/java/android/animation/Animator.java +++ b/core/java/android/animation/Animator.java @@ -16,8 +16,11 @@ package android.animation; +import android.content.Context; +import android.content.res.TypedArray; import android.os.Handler; import android.os.Message; +import android.util.AttributeSet; import android.view.animation.AccelerateDecelerateInterpolator; import android.view.animation.AnimationUtils; import android.view.animation.Interpolator; @@ -64,6 +67,15 @@ public class Animator extends Animatable { private static final int ENDED = 3; // end() called - need to end it /** + * Enum values used in XML attributes to indicate the value for mValueType + */ + private static final int VALUE_TYPE_FLOAT = 0; + private static final int VALUE_TYPE_INT = 1; + private static final int VALUE_TYPE_DOUBLE = 2; + private static final int VALUE_TYPE_COLOR = 3; + private static final int VALUE_TYPE_CUSTOM = 4; + + /** * Internal variables */ @@ -219,6 +231,67 @@ public class Animator extends Animatable { */ public static final int INFINITE = -1; + /** + * Creates a new animation whose parameters come from the specified context and + * attributes set. + * + * @param context the application environment + * @param attrs the set of attributes holding the animation parameters + */ + public Animator(Context context, AttributeSet attrs) { + TypedArray a = + context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.Animator); + + mDuration = (long) a.getInt(com.android.internal.R.styleable.Animator_duration, 0); + + mStartDelay = (long) a.getInt(com.android.internal.R.styleable.Animator_startOffset, 0); + + final int resID = + a.getResourceId(com.android.internal.R.styleable.Animator_interpolator, 0); + if (resID > 0) { + setInterpolator(AnimationUtils.loadInterpolator(context, resID)); + } + int valueType = a.getInt(com.android.internal.R.styleable.Animator_valueType, + VALUE_TYPE_FLOAT); + + switch (valueType) { + case VALUE_TYPE_FLOAT: + mValueFrom = a.getFloat(com.android.internal.R.styleable.Animator_valueFrom, 0f); + mValueTo = a.getFloat(com.android.internal.R.styleable.Animator_valueTo, 0f); + mValueType = float.class; + break; + case VALUE_TYPE_INT: + mValueFrom = a.getInt(com.android.internal.R.styleable.Animator_valueFrom, 0); + mValueTo = a.getInt(com.android.internal.R.styleable.Animator_valueTo, 0); + mValueType = int.class; + break; + case VALUE_TYPE_DOUBLE: + mValueFrom = (double) + a.getFloat(com.android.internal.R.styleable.Animator_valueFrom, 0f); + mValueTo = (double) + a.getFloat(com.android.internal.R.styleable.Animator_valueTo, 0f); + mValueType = double.class; + break; + case VALUE_TYPE_COLOR: + mValueFrom = a.getInt(com.android.internal.R.styleable.Animator_valueFrom, 0); + mValueTo = a.getInt(com.android.internal.R.styleable.Animator_valueTo, 0); + mEvaluator = new RGBEvaluator(); + mValueType = int.class; + break; + case VALUE_TYPE_CUSTOM: + // TODO: How to get an 'Object' value? + mValueFrom = a.getFloat(com.android.internal.R.styleable.Animator_valueFrom, 0f); + mValueTo = a.getFloat(com.android.internal.R.styleable.Animator_valueTo, 0f); + mValueType = Object.class; + break; + } + + mRepeatCount = a.getInt(com.android.internal.R.styleable.Animator_repeatCount, mRepeatCount); + mRepeatMode = a.getInt(com.android.internal.R.styleable.Animator_repeatMode, RESTART); + + a.recycle(); + } + private Animator(long duration, Object valueFrom, Object valueTo, Class valueType) { mDuration = duration; mValueFrom = valueFrom; diff --git a/core/java/android/animation/PropertyAnimator.java b/core/java/android/animation/PropertyAnimator.java index 4b4b021..4d7ab84 100644 --- a/core/java/android/animation/PropertyAnimator.java +++ b/core/java/android/animation/PropertyAnimator.java @@ -16,6 +16,9 @@ package android.animation; +import android.content.Context; +import android.content.res.TypedArray; +import android.util.AttributeSet; import android.util.Log; import java.lang.reflect.InvocationTargetException; @@ -152,6 +155,24 @@ public final class PropertyAnimator extends Animator { } /** + * Creates a new animation whose parameters come from the specified context and + * attributes set. + * + * @param context the application environment + * @param attrs the set of attributes holding the animation parameters + */ + public PropertyAnimator(Context context, AttributeSet attrs) { + super(context, attrs); + + TypedArray a = context.obtainStyledAttributes(attrs, + com.android.internal.R.styleable.PropertyAnimator); + + mPropertyName = a.getString(com.android.internal.R.styleable.PropertyAnimator_propertyName); + + + a.recycle(); + } + /** * Determine the setter or getter function using the JavaBeans convention of setFoo or * getFoo for a property named 'foo'. This function figures out what the name of the * function should be and uses reflection to find the Method with that name on the @@ -491,6 +512,15 @@ public final class PropertyAnimator extends Animator { } /** + * Sets the target object whose property will be animated by this animation + * + * @param target The object being animated + */ + public void setTarget(Object target) { + mTarget = target; + } + + /** * This method is called with the elapsed fraction of the animation during every * animation frame. This function turns the elapsed fraction into an interpolated fraction * and then into an animated value (from the evaluator. The function is called mostly during diff --git a/core/java/android/animation/Sequencer.java b/core/java/android/animation/Sequencer.java index 41ca58c..c283e0f 100644 --- a/core/java/android/animation/Sequencer.java +++ b/core/java/android/animation/Sequencer.java @@ -16,6 +16,11 @@ package android.animation; +import android.content.Context; +import android.content.res.TypedArray; +import android.util.AttributeSet; +import android.view.animation.AnimationUtils; + import java.util.ArrayList; import java.util.HashMap; @@ -119,6 +124,22 @@ public final class Sequencer extends Animatable { } /** + * Returns the current list of child Animatable objects controlled by this + * Sequencer. This is a copy of the internal list; modifications to the returned list + * will not affect the Sequencer, although changes to the underlying Animatable objects + * will affect those objects being managed by the Sequencer. + * + * @return ArrayList<Animatable> The list of child animations of this Sequencer. + */ + public ArrayList<Animatable> getChildAnimations() { + ArrayList<Animatable> childList = new ArrayList<Animatable>(); + for (Node node : mNodes) { + childList.add(node.animation); + } + return childList; + } + + /** * This method creates a <code>Builder</code> object, which is used to * set up playing constraints. This initial <code>play()</code> method * tells the <code>Builder</code> the animation that is the dependency for diff --git a/core/java/android/view/animation/AnimationUtils.java b/core/java/android/view/animation/AnimationUtils.java index 3088382..bfc30a5 100644 --- a/core/java/android/view/animation/AnimationUtils.java +++ b/core/java/android/view/animation/AnimationUtils.java @@ -16,6 +16,12 @@ package android.view.animation; +import android.animation.Animatable; +import android.animation.Animator; +import android.animation.PropertyAnimator; +import android.animation.Sequencer; +import android.content.res.TypedArray; +import android.util.TypedValue; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; @@ -27,12 +33,21 @@ import android.util.Xml; import android.os.SystemClock; import java.io.IOException; +import java.util.ArrayList; /** * Defines common utilities for working with animations. * */ public class AnimationUtils { + + /** + * These flags are used when parsing Sequencer objects + */ + private static final int TOGETHER = 0; + private static final int SEQUENTIALLY = 1; + + /** * 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 @@ -49,7 +64,7 @@ public class AnimationUtils { /** * 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 @@ -77,17 +92,54 @@ public class AnimationUtils { } } + /** + * 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 + * @hide + */ + public static Animatable loadAnimator(Context context, int id) + throws NotFoundException { + + XmlResourceParser parser = null; + try { + parser = context.getResources().getAnimation(id); + return createAnimatableFromXml(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(); + } + } + + private static Animatable createAnimatableFromXml(Context c, XmlPullParser parser) + throws XmlPullParserException, IOException { + + return createAnimatableFromXml(c, parser, Xml.asAttributeSet(parser), null, 0); + } + private static Animation createAnimationFromXml(Context c, XmlPullParser parser) throws XmlPullParserException, IOException { return createAnimationFromXml(c, parser, null, Xml.asAttributeSet(parser)); } - + private static Animation createAnimationFromXml(Context c, XmlPullParser parser, AnimationSet parent, AttributeSet attrs) throws XmlPullParserException, IOException { - + Animation anim = null; - + // Make sure we are on a start tag. int type; int depth = parser.getDepth(); @@ -100,7 +152,7 @@ public class AnimationUtils { } String name = parser.getName(); - + if (name.equals("set")) { anim = new AnimationSet(c, attrs); createAnimationFromXml(c, parser, (AnimationSet)anim, attrs); @@ -120,7 +172,67 @@ public class AnimationUtils { parent.addAnimation(anim); } } - + + return anim; + + } + + private static Animatable createAnimatableFromXml(Context c, XmlPullParser parser, + AttributeSet attrs, Sequencer parent, int sequenceOrdering) + throws XmlPullParserException, IOException { + + Animatable anim = null; + ArrayList<Animatable> childAnims = null; + + // 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("property")) { + anim = new PropertyAnimator(c, attrs); + } else if (name.equals("animator")) { + anim = new Animator(c, attrs); + } else if (name.equals("sequencer")) { + anim = new Sequencer(); + TypedArray a = c.obtainStyledAttributes(attrs, + com.android.internal.R.styleable.Sequencer); + int ordering = a.getInt(com.android.internal.R.styleable.Sequencer_ordering, + TOGETHER); + createAnimatableFromXml(c, parser, attrs, (Sequencer) anim, ordering); + a.recycle(); + } else { + throw new RuntimeException("Unknown animator name: " + parser.getName()); + } + + if (parent != null) { + if (childAnims == null) { + childAnims = new ArrayList<Animatable>(); + } + childAnims.add(anim); + } + } + if (parent != null && childAnims != null) { + Animatable[] animsArray = new Animatable[childAnims.size()]; + int index = 0; + for (Animatable a : childAnims) { + animsArray[index++] = a; + } + if (sequenceOrdering == TOGETHER) { + parent.playTogether(animsArray); + } else { + parent.playSequentially(animsArray); + } + } + return anim; } diff --git a/core/res/res/values/attrs.xml b/core/res/res/values/attrs.xml index d1024fe..6c14a3b 100755 --- a/core/res/res/values/attrs.xml +++ b/core/res/res/values/attrs.xml @@ -2929,6 +2929,69 @@ </declare-styleable> <!-- ========================== --> + <!-- Animator class attributes --> + <!-- ========================== --> + <eat-comment /> + + <declare-styleable name="Animator"> + <!-- Defines the interpolator used to smooth the animation movement in time. --> + <attr name="interpolator" /> + <!-- When set to true, fillAfter is taken into account. --> + <!-- Amount of time (in milliseconds) for the animation to run. --> + <attr name="duration" /> + <!-- Delay in milliseconds before the animation runs, once start time is reached. --> + <attr name="startOffset"/> + <!-- Defines how many times the animation should repeat. The default value is 0. --> + <attr name="repeatCount"/> + <!-- Defines the animation behavior when it reaches the end and the repeat count is + greater than 0 or infinite. The default value is restart. --> + <attr name="repeatMode"/> + <!-- Value the animation starts from. --> + <attr name="valueFrom" format="float|integer"/> + <!-- Value the animation animates to. --> + <attr name="valueTo" format="float|integer"/> + <!-- The type of valueFrom and valueTo. --> + <attr name="valueType"> + <!-- valueFrom and valueTo are floats. --> + <enum name="floatType" value="0" /> + <!-- valueFrom and valueTo are integers. --> + <enum name="intType" value="1" /> + <!-- valueFrom and valueTo are doubles. --> + <enum name="doubleType" value="2" /> + <!-- valueFrom and valueTo are colors. --> + <enum name="colorType" value="3" /> + <!-- valueFrom and valueTo are a custom type. --> + <enum name="customType" value="4" /> + </attr> + </declare-styleable> + + <!-- ========================== --> + <!-- PropertyAnimator class attributes --> + <!-- ========================== --> + <eat-comment /> + + <declare-styleable name="PropertyAnimator"> + <!-- Name of the property being animated. --> + <attr name="propertyName" format="string"/> + </declare-styleable> + + + <!-- ========================== --> + <!-- Sequencer class attributes --> + <!-- ========================== --> + <eat-comment /> + + <declare-styleable name="Sequencer"> + <!-- Name of the property being animated. --> + <attr name="ordering"> + <!-- child animations should be played together. --> + <enum name="together" value="0" /> + <!-- child animations should be played sequentially, in the same order as the xml. --> + <enum name="sequentially" value="1" /> + </attr> + </declare-styleable> + + <!-- ========================== --> <!-- State attributes --> <!-- ========================== --> <eat-comment /> diff --git a/core/res/res/values/public.xml b/core/res/res/values/public.xml index 657cd4f..d1021a5 100644 --- a/core/res/res/values/public.xml +++ b/core/res/res/values/public.xml @@ -1309,6 +1309,11 @@ <public type="attr" name="actionModeBackground" /> <public type="attr" name="actionModeCloseDrawable" /> <public type="attr" name="windowActionModeOverlay" /> + <public type="attr" name="valueFrom" /> + <public type="attr" name="valueTo" /> + <public type="attr" name="valueType" /> + <public type="attr" name="propertyName" /> + <public type="attr" name="ordering" /> <public type="id" name="home" /> <!-- Context menu ID for the "Select text..." menu item to switch to text |