21d15c02ac759d32bb2fdf826f6c392790dece15
[pub/Android/ownCloud.git] / actionbarsherlock / src / com / actionbarsherlock / internal / nineoldandroids / animation / ObjectAnimator.java
1 /*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.actionbarsherlock.internal.nineoldandroids.animation;
18
19 import android.util.Log;
20 //import android.util.Property;
21
22 //import java.lang.reflect.Method;
23 import java.util.ArrayList;
24
25 /**
26 * This subclass of {@link ValueAnimator} provides support for animating properties on target objects.
27 * The constructors of this class take parameters to define the target object that will be animated
28 * as well as the name of the property that will be animated. Appropriate set/get functions
29 * are then determined internally and the animation will call these functions as necessary to
30 * animate the property.
31 *
32 * @see #setPropertyName(String)
33 *
34 */
35 @SuppressWarnings("rawtypes")
36 public final class ObjectAnimator extends ValueAnimator {
37 private static final boolean DBG = false;
38
39 // The target object on which the property exists, set in the constructor
40 private Object mTarget;
41
42 private String mPropertyName;
43
44 //private Property mProperty;
45
46 /**
47 * Sets the name of the property that will be animated. This name is used to derive
48 * a setter function that will be called to set animated values.
49 * For example, a property name of <code>foo</code> will result
50 * in a call to the function <code>setFoo()</code> on the target object. If either
51 * <code>valueFrom</code> or <code>valueTo</code> is null, then a getter function will
52 * also be derived and called.
53 *
54 * <p>For best performance of the mechanism that calls the setter function determined by the
55 * name of the property being animated, use <code>float</code> or <code>int</code> typed values,
56 * and make the setter function for those properties have a <code>void</code> return value. This
57 * will cause the code to take an optimized path for these constrained circumstances. Other
58 * property types and return types will work, but will have more overhead in processing
59 * the requests due to normal reflection mechanisms.</p>
60 *
61 * <p>Note that the setter function derived from this property name
62 * must take the same parameter type as the
63 * <code>valueFrom</code> and <code>valueTo</code> properties, otherwise the call to
64 * the setter function will fail.</p>
65 *
66 * <p>If this ObjectAnimator has been set up to animate several properties together,
67 * using more than one PropertyValuesHolder objects, then setting the propertyName simply
68 * sets the propertyName in the first of those PropertyValuesHolder objects.</p>
69 *
70 * @param propertyName The name of the property being animated. Should not be null.
71 */
72 public void setPropertyName(String propertyName) {
73 // mValues could be null if this is being constructed piecemeal. Just record the
74 // propertyName to be used later when setValues() is called if so.
75 if (mValues != null) {
76 PropertyValuesHolder valuesHolder = mValues[0];
77 String oldName = valuesHolder.getPropertyName();
78 valuesHolder.setPropertyName(propertyName);
79 mValuesMap.remove(oldName);
80 mValuesMap.put(propertyName, valuesHolder);
81 }
82 mPropertyName = propertyName;
83 // New property/values/target should cause re-initialization prior to starting
84 mInitialized = false;
85 }
86
87 /**
88 * Sets the property that will be animated. Property objects will take precedence over
89 * properties specified by the {@link #setPropertyName(String)} method. Animations should
90 * be set up to use one or the other, not both.
91 *
92 * @param property The property being animated. Should not be null.
93 */
94 //public void setProperty(Property property) {
95 // // mValues could be null if this is being constructed piecemeal. Just record the
96 // // propertyName to be used later when setValues() is called if so.
97 // if (mValues != null) {
98 // PropertyValuesHolder valuesHolder = mValues[0];
99 // String oldName = valuesHolder.getPropertyName();
100 // valuesHolder.setProperty(property);
101 // mValuesMap.remove(oldName);
102 // mValuesMap.put(mPropertyName, valuesHolder);
103 // }
104 // if (mProperty != null) {
105 // mPropertyName = property.getName();
106 // }
107 // mProperty = property;
108 // // New property/values/target should cause re-initialization prior to starting
109 // mInitialized = false;
110 //}
111
112 /**
113 * Gets the name of the property that will be animated. This name will be used to derive
114 * a setter function that will be called to set animated values.
115 * For example, a property name of <code>foo</code> will result
116 * in a call to the function <code>setFoo()</code> on the target object. If either
117 * <code>valueFrom</code> or <code>valueTo</code> is null, then a getter function will
118 * also be derived and called.
119 */
120 public String getPropertyName() {
121 return mPropertyName;
122 }
123
124 /**
125 * Creates a new ObjectAnimator object. This default constructor is primarily for
126 * use internally; the other constructors which take parameters are more generally
127 * useful.
128 */
129 public ObjectAnimator() {
130 }
131
132 /**
133 * Private utility constructor that initializes the target object and name of the
134 * property being animated.
135 *
136 * @param target The object whose property is to be animated. This object should
137 * have a public method on it called <code>setName()</code>, where <code>name</code> is
138 * the value of the <code>propertyName</code> parameter.
139 * @param propertyName The name of the property being animated.
140 */
141 private ObjectAnimator(Object target, String propertyName) {
142 mTarget = target;
143 setPropertyName(propertyName);
144 }
145
146 /**
147 * Private utility constructor that initializes the target object and property being animated.
148 *
149 * @param target The object whose property is to be animated.
150 * @param property The property being animated.
151 */
152 //private <T> ObjectAnimator(T target, Property<T, ?> property) {
153 // mTarget = target;
154 // setProperty(property);
155 //}
156
157 /**
158 * Constructs and returns an ObjectAnimator that animates between int values. A single
159 * value implies that that value is the one being animated to. Two values imply a starting
160 * and ending values. More than two values imply a starting value, values to animate through
161 * along the way, and an ending value (these values will be distributed evenly across
162 * the duration of the animation).
163 *
164 * @param target The object whose property is to be animated. This object should
165 * have a public method on it called <code>setName()</code>, where <code>name</code> is
166 * the value of the <code>propertyName</code> parameter.
167 * @param propertyName The name of the property being animated.
168 * @param values A set of values that the animation will animate between over time.
169 * @return An ObjectAnimator object that is set up to animate between the given values.
170 */
171 public static ObjectAnimator ofInt(Object target, String propertyName, int... values) {
172 ObjectAnimator anim = new ObjectAnimator(target, propertyName);
173 anim.setIntValues(values);
174 return anim;
175 }
176
177 /**
178 * Constructs and returns an ObjectAnimator that animates between int values. A single
179 * value implies that that value is the one being animated to. Two values imply a starting
180 * and ending values. More than two values imply a starting value, values to animate through
181 * along the way, and an ending value (these values will be distributed evenly across
182 * the duration of the animation).
183 *
184 * @param target The object whose property is to be animated.
185 * @param property The property being animated.
186 * @param values A set of values that the animation will animate between over time.
187 * @return An ObjectAnimator object that is set up to animate between the given values.
188 */
189 //public static <T> ObjectAnimator ofInt(T target, Property<T, Integer> property, int... values) {
190 // ObjectAnimator anim = new ObjectAnimator(target, property);
191 // anim.setIntValues(values);
192 // return anim;
193 //}
194
195 /**
196 * Constructs and returns an ObjectAnimator that animates between float values. A single
197 * value implies that that value is the one being animated to. Two values imply a starting
198 * and ending values. More than two values imply a starting value, values to animate through
199 * along the way, and an ending value (these values will be distributed evenly across
200 * the duration of the animation).
201 *
202 * @param target The object whose property is to be animated. This object should
203 * have a public method on it called <code>setName()</code>, where <code>name</code> is
204 * the value of the <code>propertyName</code> parameter.
205 * @param propertyName The name of the property being animated.
206 * @param values A set of values that the animation will animate between over time.
207 * @return An ObjectAnimator object that is set up to animate between the given values.
208 */
209 public static ObjectAnimator ofFloat(Object target, String propertyName, float... values) {
210 ObjectAnimator anim = new ObjectAnimator(target, propertyName);
211 anim.setFloatValues(values);
212 return anim;
213 }
214
215 /**
216 * Constructs and returns an ObjectAnimator that animates between float values. A single
217 * value implies that that value is the one being animated to. Two values imply a starting
218 * and ending values. More than two values imply a starting value, values to animate through
219 * along the way, and an ending value (these values will be distributed evenly across
220 * the duration of the animation).
221 *
222 * @param target The object whose property is to be animated.
223 * @param property The property being animated.
224 * @param values A set of values that the animation will animate between over time.
225 * @return An ObjectAnimator object that is set up to animate between the given values.
226 */
227 //public static <T> ObjectAnimator ofFloat(T target, Property<T, Float> property,
228 // float... values) {
229 // ObjectAnimator anim = new ObjectAnimator(target, property);
230 // anim.setFloatValues(values);
231 // return anim;
232 //}
233
234 /**
235 * Constructs and returns an ObjectAnimator that animates between Object values. A single
236 * value implies that that value is the one being animated to. Two values imply a starting
237 * and ending values. More than two values imply a starting value, values to animate through
238 * along the way, and an ending value (these values will be distributed evenly across
239 * the duration of the animation).
240 *
241 * @param target The object whose property is to be animated. This object should
242 * have a public method on it called <code>setName()</code>, where <code>name</code> is
243 * the value of the <code>propertyName</code> parameter.
244 * @param propertyName The name of the property being animated.
245 * @param evaluator A TypeEvaluator that will be called on each animation frame to
246 * provide the necessary interpolation between the Object values to derive the animated
247 * value.
248 * @param values A set of values that the animation will animate between over time.
249 * @return An ObjectAnimator object that is set up to animate between the given values.
250 */
251 public static ObjectAnimator ofObject(Object target, String propertyName,
252 TypeEvaluator evaluator, Object... values) {
253 ObjectAnimator anim = new ObjectAnimator(target, propertyName);
254 anim.setObjectValues(values);
255 anim.setEvaluator(evaluator);
256 return anim;
257 }
258
259 /**
260 * Constructs and returns an ObjectAnimator that animates between Object values. A single
261 * value implies that that value is the one being animated to. Two values imply a starting
262 * and ending values. More than two values imply a starting value, values to animate through
263 * along the way, and an ending value (these values will be distributed evenly across
264 * the duration of the animation).
265 *
266 * @param target The object whose property is to be animated.
267 * @param property The property being animated.
268 * @param evaluator A TypeEvaluator that will be called on each animation frame to
269 * provide the necessary interpolation between the Object values to derive the animated
270 * value.
271 * @param values A set of values that the animation will animate between over time.
272 * @return An ObjectAnimator object that is set up to animate between the given values.
273 */
274 //public static <T, V> ObjectAnimator ofObject(T target, Property<T, V> property,
275 // TypeEvaluator<V> evaluator, V... values) {
276 // ObjectAnimator anim = new ObjectAnimator(target, property);
277 // anim.setObjectValues(values);
278 // anim.setEvaluator(evaluator);
279 // return anim;
280 //}
281
282 /**
283 * Constructs and returns an ObjectAnimator that animates between the sets of values specified
284 * in <code>PropertyValueHolder</code> objects. This variant should be used when animating
285 * several properties at once with the same ObjectAnimator, since PropertyValuesHolder allows
286 * you to associate a set of animation values with a property name.
287 *
288 * @param target The object whose property is to be animated. Depending on how the
289 * PropertyValuesObjects were constructed, the target object should either have the {@link
290 * android.util.Property} objects used to construct the PropertyValuesHolder objects or (if the
291 * PropertyValuesHOlder objects were created with property names) the target object should have
292 * public methods on it called <code>setName()</code>, where <code>name</code> is the name of
293 * the property passed in as the <code>propertyName</code> parameter for each of the
294 * PropertyValuesHolder objects.
295 * @param values A set of PropertyValuesHolder objects whose values will be animated between
296 * over time.
297 * @return An ObjectAnimator object that is set up to animate between the given values.
298 */
299 public static ObjectAnimator ofPropertyValuesHolder(Object target,
300 PropertyValuesHolder... values) {
301 ObjectAnimator anim = new ObjectAnimator();
302 anim.mTarget = target;
303 anim.setValues(values);
304 return anim;
305 }
306
307 @Override
308 public void setIntValues(int... values) {
309 if (mValues == null || mValues.length == 0) {
310 // No values yet - this animator is being constructed piecemeal. Init the values with
311 // whatever the current propertyName is
312 //if (mProperty != null) {
313 // setValues(PropertyValuesHolder.ofInt(mProperty, values));
314 //} else {
315 setValues(PropertyValuesHolder.ofInt(mPropertyName, values));
316 //}
317 } else {
318 super.setIntValues(values);
319 }
320 }
321
322 @Override
323 public void setFloatValues(float... values) {
324 if (mValues == null || mValues.length == 0) {
325 // No values yet - this animator is being constructed piecemeal. Init the values with
326 // whatever the current propertyName is
327 //if (mProperty != null) {
328 // setValues(PropertyValuesHolder.ofFloat(mProperty, values));
329 //} else {
330 setValues(PropertyValuesHolder.ofFloat(mPropertyName, values));
331 //}
332 } else {
333 super.setFloatValues(values);
334 }
335 }
336
337 @Override
338 public void setObjectValues(Object... values) {
339 if (mValues == null || mValues.length == 0) {
340 // No values yet - this animator is being constructed piecemeal. Init the values with
341 // whatever the current propertyName is
342 //if (mProperty != null) {
343 // setValues(PropertyValuesHolder.ofObject(mProperty, (TypeEvaluator)null, values));
344 //} else {
345 setValues(PropertyValuesHolder.ofObject(mPropertyName, (TypeEvaluator)null, values));
346 //}
347 } else {
348 super.setObjectValues(values);
349 }
350 }
351
352 @Override
353 public void start() {
354 if (DBG) {
355 Log.d("ObjectAnimator", "Anim target, duration: " + mTarget + ", " + getDuration());
356 for (int i = 0; i < mValues.length; ++i) {
357 PropertyValuesHolder pvh = mValues[i];
358 ArrayList<Keyframe> keyframes = pvh.mKeyframeSet.mKeyframes;
359 Log.d("ObjectAnimator", " Values[" + i + "]: " +
360 pvh.getPropertyName() + ", " + keyframes.get(0).getValue() + ", " +
361 keyframes.get(pvh.mKeyframeSet.mNumKeyframes - 1).getValue());
362 }
363 }
364 super.start();
365 }
366
367 /**
368 * This function is called immediately before processing the first animation
369 * frame of an animation. If there is a nonzero <code>startDelay</code>, the
370 * function is called after that delay ends.
371 * It takes care of the final initialization steps for the
372 * animation. This includes setting mEvaluator, if the user has not yet
373 * set it up, and the setter/getter methods, if the user did not supply
374 * them.
375 *
376 * <p>Overriders of this method should call the superclass method to cause
377 * internal mechanisms to be set up correctly.</p>
378 */
379 @Override
380 void initAnimation() {
381 if (!mInitialized) {
382 // mValueType may change due to setter/getter setup; do this before calling super.init(),
383 // which uses mValueType to set up the default type evaluator.
384 int numValues = mValues.length;
385 for (int i = 0; i < numValues; ++i) {
386 mValues[i].setupSetterAndGetter(mTarget);
387 }
388 super.initAnimation();
389 }
390 }
391
392 /**
393 * Sets the length of the animation. The default duration is 300 milliseconds.
394 *
395 * @param duration The length of the animation, in milliseconds.
396 * @return ObjectAnimator The object called with setDuration(). This return
397 * value makes it easier to compose statements together that construct and then set the
398 * duration, as in
399 * <code>ObjectAnimator.ofInt(target, propertyName, 0, 10).setDuration(500).start()</code>.
400 */
401 @Override
402 public ObjectAnimator setDuration(long duration) {
403 super.setDuration(duration);
404 return this;
405 }
406
407
408 /**
409 * The target object whose property will be animated by this animation
410 *
411 * @return The object being animated
412 */
413 public Object getTarget() {
414 return mTarget;
415 }
416
417 /**
418 * Sets the target object whose property will be animated by this animation
419 *
420 * @param target The object being animated
421 */
422 @Override
423 public void setTarget(Object target) {
424 if (mTarget != target) {
425 final Object oldTarget = mTarget;
426 mTarget = target;
427 if (oldTarget != null && target != null && oldTarget.getClass() == target.getClass()) {
428 return;
429 }
430 // New target type should cause re-initialization prior to starting
431 mInitialized = false;
432 }
433 }
434
435 @Override
436 public void setupStartValues() {
437 initAnimation();
438 int numValues = mValues.length;
439 for (int i = 0; i < numValues; ++i) {
440 mValues[i].setupStartValue(mTarget);
441 }
442 }
443
444 @Override
445 public void setupEndValues() {
446 initAnimation();
447 int numValues = mValues.length;
448 for (int i = 0; i < numValues; ++i) {
449 mValues[i].setupEndValue(mTarget);
450 }
451 }
452
453 /**
454 * This method is called with the elapsed fraction of the animation during every
455 * animation frame. This function turns the elapsed fraction into an interpolated fraction
456 * and then into an animated value (from the evaluator. The function is called mostly during
457 * animation updates, but it is also called when the <code>end()</code>
458 * function is called, to set the final value on the property.
459 *
460 * <p>Overrides of this method must call the superclass to perform the calculation
461 * of the animated value.</p>
462 *
463 * @param fraction The elapsed fraction of the animation.
464 */
465 @Override
466 void animateValue(float fraction) {
467 super.animateValue(fraction);
468 int numValues = mValues.length;
469 for (int i = 0; i < numValues; ++i) {
470 mValues[i].setAnimatedValue(mTarget);
471 }
472 }
473
474 @Override
475 public ObjectAnimator clone() {
476 final ObjectAnimator anim = (ObjectAnimator) super.clone();
477 return anim;
478 }
479
480 @Override
481 public String toString() {
482 String returnVal = "ObjectAnimator@" + Integer.toHexString(hashCode()) + ", target " +
483 mTarget;
484 if (mValues != null) {
485 for (int i = 0; i < mValues.length; ++i) {
486 returnVal += "\n " + mValues[i].toString();
487 }
488 }
489 return returnVal;
490 }
491 }