Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 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 android.animation; |
| 18 | |
George Mount | c96c7b2 | 2013-08-23 13:31:31 -0700 | [diff] [blame] | 19 | import android.graphics.Path; |
| 20 | import android.graphics.PointF; |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 21 | import android.util.Log; |
Chet Haase | b39f051 | 2011-05-24 14:36:40 -0700 | [diff] [blame] | 22 | import android.util.Property; |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 23 | |
Chet Haase | e2ab7cc | 2010-12-06 16:10:07 -0800 | [diff] [blame] | 24 | import java.util.ArrayList; |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 25 | |
| 26 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 27 | * This subclass of {@link ValueAnimator} provides support for animating properties on target objects. |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 28 | * The constructors of this class take parameters to define the target object that will be animated |
| 29 | * as well as the name of the property that will be animated. Appropriate set/get functions |
| 30 | * are then determined internally and the animation will call these functions as necessary to |
| 31 | * animate the property. |
Chet Haase | 6e0ecb4 | 2010-11-03 19:41:18 -0700 | [diff] [blame] | 32 | * |
Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 33 | * <div class="special reference"> |
| 34 | * <h3>Developer Guides</h3> |
| 35 | * <p>For more information about animating with {@code ObjectAnimator}, read the |
| 36 | * <a href="{@docRoot}guide/topics/graphics/prop-animation.html#object-animator">Property |
| 37 | * Animation</a> developer guide.</p> |
| 38 | * </div> |
| 39 | * |
Chet Haase | 6e0ecb4 | 2010-11-03 19:41:18 -0700 | [diff] [blame] | 40 | * @see #setPropertyName(String) |
| 41 | * |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 42 | */ |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 43 | public final class ObjectAnimator extends ValueAnimator { |
Chet Haase | e2ab7cc | 2010-12-06 16:10:07 -0800 | [diff] [blame] | 44 | private static final boolean DBG = false; |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 45 | |
| 46 | // The target object on which the property exists, set in the constructor |
Patrick Dubroy | 51ae5fc | 2011-01-16 14:23:15 -0800 | [diff] [blame] | 47 | private Object mTarget; |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 48 | |
| 49 | private String mPropertyName; |
| 50 | |
Chet Haase | b39f051 | 2011-05-24 14:36:40 -0700 | [diff] [blame] | 51 | private Property mProperty; |
| 52 | |
Chet Haase | be19e03 | 2013-03-15 17:08:55 -0700 | [diff] [blame] | 53 | private boolean mAutoCancel = false; |
| 54 | |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 55 | /** |
| 56 | * Sets the name of the property that will be animated. This name is used to derive |
| 57 | * a setter function that will be called to set animated values. |
| 58 | * For example, a property name of <code>foo</code> will result |
| 59 | * in a call to the function <code>setFoo()</code> on the target object. If either |
| 60 | * <code>valueFrom</code> or <code>valueTo</code> is null, then a getter function will |
| 61 | * also be derived and called. |
| 62 | * |
Chet Haase | 6e0ecb4 | 2010-11-03 19:41:18 -0700 | [diff] [blame] | 63 | * <p>For best performance of the mechanism that calls the setter function determined by the |
| 64 | * name of the property being animated, use <code>float</code> or <code>int</code> typed values, |
| 65 | * and make the setter function for those properties have a <code>void</code> return value. This |
| 66 | * will cause the code to take an optimized path for these constrained circumstances. Other |
| 67 | * property types and return types will work, but will have more overhead in processing |
| 68 | * the requests due to normal reflection mechanisms.</p> |
| 69 | * |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 70 | * <p>Note that the setter function derived from this property name |
| 71 | * must take the same parameter type as the |
| 72 | * <code>valueFrom</code> and <code>valueTo</code> properties, otherwise the call to |
| 73 | * the setter function will fail.</p> |
| 74 | * |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 75 | * <p>If this ObjectAnimator has been set up to animate several properties together, |
Chet Haase | d953d08 | 2010-08-16 17:44:28 -0700 | [diff] [blame] | 76 | * using more than one PropertyValuesHolder objects, then setting the propertyName simply |
| 77 | * sets the propertyName in the first of those PropertyValuesHolder objects.</p> |
| 78 | * |
Chet Haase | b39f051 | 2011-05-24 14:36:40 -0700 | [diff] [blame] | 79 | * @param propertyName The name of the property being animated. Should not be null. |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 80 | */ |
| 81 | public void setPropertyName(String propertyName) { |
Chet Haase | 0e0590b | 2010-09-26 11:57:28 -0700 | [diff] [blame] | 82 | // mValues could be null if this is being constructed piecemeal. Just record the |
| 83 | // propertyName to be used later when setValues() is called if so. |
Chet Haase | d953d08 | 2010-08-16 17:44:28 -0700 | [diff] [blame] | 84 | if (mValues != null) { |
Chet Haase | 602e4d3 | 2010-08-16 08:57:23 -0700 | [diff] [blame] | 85 | PropertyValuesHolder valuesHolder = mValues[0]; |
| 86 | String oldName = valuesHolder.getPropertyName(); |
Chet Haase | d953d08 | 2010-08-16 17:44:28 -0700 | [diff] [blame] | 87 | valuesHolder.setPropertyName(propertyName); |
Chet Haase | 602e4d3 | 2010-08-16 08:57:23 -0700 | [diff] [blame] | 88 | mValuesMap.remove(oldName); |
| 89 | mValuesMap.put(propertyName, valuesHolder); |
Chet Haase | d953d08 | 2010-08-16 17:44:28 -0700 | [diff] [blame] | 90 | } |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 91 | mPropertyName = propertyName; |
Chet Haase | 0e0590b | 2010-09-26 11:57:28 -0700 | [diff] [blame] | 92 | // New property/values/target should cause re-initialization prior to starting |
| 93 | mInitialized = false; |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** |
Chet Haase | b39f051 | 2011-05-24 14:36:40 -0700 | [diff] [blame] | 97 | * Sets the property that will be animated. Property objects will take precedence over |
| 98 | * properties specified by the {@link #setPropertyName(String)} method. Animations should |
| 99 | * be set up to use one or the other, not both. |
| 100 | * |
| 101 | * @param property The property being animated. Should not be null. |
| 102 | */ |
| 103 | public void setProperty(Property property) { |
| 104 | // mValues could be null if this is being constructed piecemeal. Just record the |
| 105 | // propertyName to be used later when setValues() is called if so. |
| 106 | if (mValues != null) { |
| 107 | PropertyValuesHolder valuesHolder = mValues[0]; |
| 108 | String oldName = valuesHolder.getPropertyName(); |
| 109 | valuesHolder.setProperty(property); |
| 110 | mValuesMap.remove(oldName); |
| 111 | mValuesMap.put(mPropertyName, valuesHolder); |
| 112 | } |
| 113 | if (mProperty != null) { |
| 114 | mPropertyName = property.getName(); |
| 115 | } |
| 116 | mProperty = property; |
| 117 | // New property/values/target should cause re-initialization prior to starting |
| 118 | mInitialized = false; |
| 119 | } |
| 120 | |
| 121 | /** |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 122 | * Gets the name of the property that will be animated. This name will be used to derive |
| 123 | * a setter function that will be called to set animated values. |
| 124 | * For example, a property name of <code>foo</code> will result |
| 125 | * in a call to the function <code>setFoo()</code> on the target object. If either |
| 126 | * <code>valueFrom</code> or <code>valueTo</code> is null, then a getter function will |
| 127 | * also be derived and called. |
Chet Haase | fdd3ad7 | 2013-04-24 16:38:20 -0700 | [diff] [blame] | 128 | * |
| 129 | * <p>If this animator was created with a {@link Property} object instead of the |
| 130 | * string name of a property, then this method will return the {@link |
| 131 | * Property#getName() name} of that Property object instead. If this animator was |
| 132 | * created with one or more {@link PropertyValuesHolder} objects, then this method |
| 133 | * will return the {@link PropertyValuesHolder#getPropertyName() name} of that |
| 134 | * object (if there was just one) or a comma-separated list of all of the |
| 135 | * names (if there are more than one).</p> |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 136 | */ |
| 137 | public String getPropertyName() { |
Chet Haase | fdd3ad7 | 2013-04-24 16:38:20 -0700 | [diff] [blame] | 138 | String propertyName = null; |
| 139 | if (mPropertyName != null) { |
| 140 | propertyName = mPropertyName; |
| 141 | } else if (mProperty != null) { |
| 142 | propertyName = mProperty.getName(); |
| 143 | } else if (mValues != null && mValues.length > 0) { |
| 144 | for (int i = 0; i < mValues.length; ++i) { |
| 145 | if (i == 0) { |
| 146 | propertyName = ""; |
| 147 | } else { |
| 148 | propertyName += ","; |
| 149 | } |
| 150 | propertyName += mValues[i].getPropertyName(); |
| 151 | } |
| 152 | } |
| 153 | return propertyName; |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | String getNameForTrace() { |
| 158 | return "animator:" + getPropertyName(); |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 162 | * Creates a new ObjectAnimator object. This default constructor is primarily for |
Chet Haase | d51d368 | 2010-08-11 19:46:48 -0700 | [diff] [blame] | 163 | * use internally; the other constructors which take parameters are more generally |
| 164 | * useful. |
| 165 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 166 | public ObjectAnimator() { |
Chet Haase | d51d368 | 2010-08-11 19:46:48 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /** |
Chet Haase | b39f051 | 2011-05-24 14:36:40 -0700 | [diff] [blame] | 170 | * Private utility constructor that initializes the target object and name of the |
| 171 | * property being animated. |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 172 | * |
Patrick Dubroy | 51ae5fc | 2011-01-16 14:23:15 -0800 | [diff] [blame] | 173 | * @param target The object whose property is to be animated. This object should |
| 174 | * have a public method on it called <code>setName()</code>, where <code>name</code> is |
| 175 | * the value of the <code>propertyName</code> parameter. |
Chet Haase | d953d08 | 2010-08-16 17:44:28 -0700 | [diff] [blame] | 176 | * @param propertyName The name of the property being animated. |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 177 | */ |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 178 | private ObjectAnimator(Object target, String propertyName) { |
Patrick Dubroy | 51ae5fc | 2011-01-16 14:23:15 -0800 | [diff] [blame] | 179 | mTarget = target; |
Chet Haase | d953d08 | 2010-08-16 17:44:28 -0700 | [diff] [blame] | 180 | setPropertyName(propertyName); |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /** |
Chet Haase | b39f051 | 2011-05-24 14:36:40 -0700 | [diff] [blame] | 184 | * Private utility constructor that initializes the target object and property being animated. |
| 185 | * |
| 186 | * @param target The object whose property is to be animated. |
| 187 | * @param property The property being animated. |
| 188 | */ |
| 189 | private <T> ObjectAnimator(T target, Property<T, ?> property) { |
| 190 | mTarget = target; |
| 191 | setProperty(property); |
| 192 | } |
| 193 | |
| 194 | /** |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 195 | * Constructs and returns an ObjectAnimator that animates between int values. A single |
George Mount | 16d2c9c | 2013-09-17 09:07:48 -0700 | [diff] [blame] | 196 | * value implies that that value is the one being animated to. Two values imply starting |
Chet Haase | b39f051 | 2011-05-24 14:36:40 -0700 | [diff] [blame] | 197 | * and ending values. More than two values imply a starting value, values to animate through |
| 198 | * along the way, and an ending value (these values will be distributed evenly across |
| 199 | * the duration of the animation). |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 200 | * |
Patrick Dubroy | 51ae5fc | 2011-01-16 14:23:15 -0800 | [diff] [blame] | 201 | * @param target The object whose property is to be animated. This object should |
| 202 | * have a public method on it called <code>setName()</code>, where <code>name</code> is |
| 203 | * the value of the <code>propertyName</code> parameter. |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 204 | * @param propertyName The name of the property being animated. |
| 205 | * @param values A set of values that the animation will animate between over time. |
Chet Haase | b39f051 | 2011-05-24 14:36:40 -0700 | [diff] [blame] | 206 | * @return An ObjectAnimator object that is set up to animate between the given values. |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 207 | */ |
| 208 | public static ObjectAnimator ofInt(Object target, String propertyName, int... values) { |
| 209 | ObjectAnimator anim = new ObjectAnimator(target, propertyName); |
| 210 | anim.setIntValues(values); |
| 211 | return anim; |
| 212 | } |
| 213 | |
| 214 | /** |
George Mount | c96c7b2 | 2013-08-23 13:31:31 -0700 | [diff] [blame] | 215 | * Constructs and returns an ObjectAnimator that animates coordinates along a <code>Path</code> |
| 216 | * using two properties. A <code>Path</code></> animation moves in two dimensions, animating |
| 217 | * coordinates <code>(x, y)</code> together to follow the line. In this variation, the |
| 218 | * coordinates are integers that are set to separate properties designated by |
| 219 | * <code>xPropertyName</code> and <code>yPropertyName</code>. |
| 220 | * |
| 221 | * @param target The object whose properties are to be animated. This object should |
| 222 | * have public methods on it called <code>setNameX()</code> and |
| 223 | * <code>setNameY</code>, where <code>nameX</code> and <code>nameY</code> |
| 224 | * are the value of <code>xPropertyName</code> and <code>yPropertyName</code> |
| 225 | * parameters, respectively. |
| 226 | * @param xPropertyName The name of the property for the x coordinate being animated. |
| 227 | * @param yPropertyName The name of the property for the y coordinate being animated. |
| 228 | * @param path The <code>Path</code> to animate values along. |
| 229 | * @return An ObjectAnimator object that is set up to animate along <code>path</code>. |
| 230 | */ |
| 231 | public static ObjectAnimator ofInt(Object target, String xPropertyName, String yPropertyName, |
| 232 | Path path) { |
| 233 | Keyframe[][] keyframes = PropertyValuesHolder.createKeyframes(path, true); |
| 234 | PropertyValuesHolder x = PropertyValuesHolder.ofKeyframe(xPropertyName, keyframes[0]); |
| 235 | PropertyValuesHolder y = PropertyValuesHolder.ofKeyframe(yPropertyName, keyframes[1]); |
| 236 | return ofPropertyValuesHolder(target, x, y); |
| 237 | } |
| 238 | |
| 239 | /** |
Chet Haase | b39f051 | 2011-05-24 14:36:40 -0700 | [diff] [blame] | 240 | * Constructs and returns an ObjectAnimator that animates between int values. A single |
George Mount | 16d2c9c | 2013-09-17 09:07:48 -0700 | [diff] [blame] | 241 | * value implies that that value is the one being animated to. Two values imply starting |
Chet Haase | b39f051 | 2011-05-24 14:36:40 -0700 | [diff] [blame] | 242 | * and ending values. More than two values imply a starting value, values to animate through |
| 243 | * along the way, and an ending value (these values will be distributed evenly across |
| 244 | * the duration of the animation). |
| 245 | * |
| 246 | * @param target The object whose property is to be animated. |
| 247 | * @param property The property being animated. |
| 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 <T> ObjectAnimator ofInt(T target, Property<T, Integer> property, int... values) { |
| 252 | ObjectAnimator anim = new ObjectAnimator(target, property); |
| 253 | anim.setIntValues(values); |
| 254 | return anim; |
| 255 | } |
| 256 | |
| 257 | /** |
George Mount | c96c7b2 | 2013-08-23 13:31:31 -0700 | [diff] [blame] | 258 | * Constructs and returns an ObjectAnimator that animates coordinates along a <code>Path</code> |
| 259 | * using two properties. A <code>Path</code></> animation moves in two dimensions, animating |
| 260 | * coordinates <code>(x, y)</code> together to follow the line. In this variation, the |
| 261 | * coordinates are integers that are set to separate properties, <code>xProperty</code> and |
| 262 | * <code>yProperty</code>. |
| 263 | * |
| 264 | * @param target The object whose properties are to be animated. |
| 265 | * @param xProperty The property for the x coordinate being animated. |
| 266 | * @param yProperty The property for the y coordinate being animated. |
| 267 | * @param path The <code>Path</code> to animate values along. |
| 268 | * @return An ObjectAnimator object that is set up to animate along <code>path</code>. |
| 269 | */ |
| 270 | public static <T> ObjectAnimator ofInt(T target, Property<T, Integer> xProperty, |
| 271 | Property<T, Integer> yProperty, Path path) { |
| 272 | Keyframe[][] keyframes = PropertyValuesHolder.createKeyframes(path, true); |
| 273 | PropertyValuesHolder x = PropertyValuesHolder.ofKeyframe(xProperty, keyframes[0]); |
| 274 | PropertyValuesHolder y = PropertyValuesHolder.ofKeyframe(yProperty, keyframes[1]); |
| 275 | return ofPropertyValuesHolder(target, x, y); |
| 276 | } |
| 277 | |
| 278 | /** |
George Mount | 4eed529 | 2013-08-30 13:56:01 -0700 | [diff] [blame] | 279 | * Constructs and returns an ObjectAnimator that animates over int values for a multiple |
| 280 | * parameters setter. Only public methods that take only int parameters are supported. |
| 281 | * Each <code>int[]</code> contains a complete set of parameters to the setter method. |
| 282 | * At least two <code>int[]</code> values must be provided, a start and end. More than two |
| 283 | * values imply a starting value, values to animate through along the way, and an ending |
| 284 | * value (these values will be distributed evenly across the duration of the animation). |
| 285 | * |
| 286 | * @param target The object whose property is to be animated. This object may |
| 287 | * have a public method on it called <code>setName()</code>, where <code>name</code> is |
| 288 | * the value of the <code>propertyName</code> parameter. <code>propertyName</code> may also |
| 289 | * be the case-sensitive complete name of the public setter method. |
| 290 | * @param propertyName The name of the property being animated or the name of the setter method. |
| 291 | * @param values A set of values that the animation will animate between over time. |
| 292 | * @return An ObjectAnimator object that is set up to animate between the given values. |
| 293 | */ |
| 294 | public static ObjectAnimator ofMultiInt(Object target, String propertyName, int[][] values) { |
| 295 | PropertyValuesHolder pvh = PropertyValuesHolder.ofMultiInt(propertyName, values); |
| 296 | return ofPropertyValuesHolder(target, pvh); |
| 297 | } |
| 298 | |
| 299 | /** |
George Mount | c96c7b2 | 2013-08-23 13:31:31 -0700 | [diff] [blame] | 300 | * Constructs and returns an ObjectAnimator that animates the target using a multi-int setter |
| 301 | * along the given <code>Path</code>. A <code>Path</code></> animation moves in two dimensions, |
| 302 | * animating coordinates <code>(x, y)</code> together to follow the line. In this variation, the |
| 303 | * coordinates are integer x and y coordinates used in the first and second parameter of the |
| 304 | * setter, respectively. |
| 305 | * |
| 306 | * @param target The object whose property is to be animated. This object may |
| 307 | * have a public method on it called <code>setName()</code>, where <code>name</code> is |
| 308 | * the value of the <code>propertyName</code> parameter. <code>propertyName</code> may also |
| 309 | * be the case-sensitive complete name of the public setter method. |
| 310 | * @param propertyName The name of the property being animated or the name of the setter method. |
| 311 | * @param path The <code>Path</code> to animate values along. |
| 312 | * @return An ObjectAnimator object that is set up to animate along <code>path</code>. |
| 313 | */ |
| 314 | public static ObjectAnimator ofMultiInt(Object target, String propertyName, Path path) { |
| 315 | PropertyValuesHolder pvh = PropertyValuesHolder.ofMultiInt(propertyName, path); |
| 316 | return ofPropertyValuesHolder(target, pvh); |
| 317 | } |
| 318 | |
| 319 | /** |
George Mount | 4eed529 | 2013-08-30 13:56:01 -0700 | [diff] [blame] | 320 | * Constructs and returns an ObjectAnimator that animates over values for a multiple int |
| 321 | * parameters setter. Only public methods that take only int parameters are supported. |
| 322 | * <p>At least two values must be provided, a start and end. More than two |
| 323 | * values imply a starting value, values to animate through along the way, and an ending |
| 324 | * value (these values will be distributed evenly across the duration of the animation).</p> |
| 325 | * |
| 326 | * @param target The object whose property is to be animated. This object may |
| 327 | * have a public method on it called <code>setName()</code>, where <code>name</code> is |
| 328 | * the value of the <code>propertyName</code> parameter. <code>propertyName</code> may also |
George Mount | c96c7b2 | 2013-08-23 13:31:31 -0700 | [diff] [blame] | 329 | * be the case-sensitive complete name of the public setter method. |
George Mount | 4eed529 | 2013-08-30 13:56:01 -0700 | [diff] [blame] | 330 | * @param propertyName The name of the property being animated or the name of the setter method. |
| 331 | * @param converter Converts T objects into int parameters for the multi-value setter. |
| 332 | * @param evaluator A TypeEvaluator that will be called on each animation frame to |
| 333 | * provide the necessary interpolation between the Object values to derive the animated |
| 334 | * value. |
| 335 | * @param values A set of values that the animation will animate between over time. |
| 336 | * @return An ObjectAnimator object that is set up to animate between the given values. |
| 337 | */ |
| 338 | public static <T> ObjectAnimator ofMultiInt(Object target, String propertyName, |
| 339 | TypeConverter<T, int[]> converter, TypeEvaluator<T> evaluator, T... values) { |
| 340 | PropertyValuesHolder pvh = PropertyValuesHolder.ofMultiInt(propertyName, converter, |
| 341 | evaluator, values); |
| 342 | return ObjectAnimator.ofPropertyValuesHolder(target, pvh); |
| 343 | } |
| 344 | |
| 345 | /** |
George Mount | 1ffb280 | 2013-10-09 16:13:54 -0700 | [diff] [blame] | 346 | * Constructs and returns an ObjectAnimator that animates between color values. A single |
| 347 | * value implies that that value is the one being animated to. Two values imply starting |
| 348 | * and ending values. More than two values imply a starting value, values to animate through |
| 349 | * along the way, and an ending value (these values will be distributed evenly across |
| 350 | * the duration of the animation). |
| 351 | * |
| 352 | * @param target The object whose property is to be animated. This object should |
| 353 | * have a public method on it called <code>setName()</code>, where <code>name</code> is |
| 354 | * the value of the <code>propertyName</code> parameter. |
| 355 | * @param propertyName The name of the property being animated. |
| 356 | * @param values A set of values that the animation will animate between over time. |
| 357 | * @return An ObjectAnimator object that is set up to animate between the given values. |
| 358 | */ |
| 359 | public static ObjectAnimator ofArgb(Object target, String propertyName, int... values) { |
| 360 | ObjectAnimator animator = ofInt(target, propertyName, values); |
| 361 | animator.setEvaluator(ArgbEvaluator.getInstance()); |
| 362 | return animator; |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Constructs and returns an ObjectAnimator that animates between color values. A single |
| 367 | * value implies that that value is the one being animated to. Two values imply starting |
| 368 | * and ending values. More than two values imply a starting value, values to animate through |
| 369 | * along the way, and an ending value (these values will be distributed evenly across |
| 370 | * the duration of the animation). |
| 371 | * |
| 372 | * @param target The object whose property is to be animated. |
| 373 | * @param property The property being animated. |
| 374 | * @param values A set of values that the animation will animate between over time. |
| 375 | * @return An ObjectAnimator object that is set up to animate between the given values. |
| 376 | */ |
| 377 | public static <T> ObjectAnimator ofArgb(T target, Property<T, Integer> property, |
| 378 | int... values) { |
| 379 | ObjectAnimator animator = ofInt(target, property, values); |
| 380 | animator.setEvaluator(ArgbEvaluator.getInstance()); |
| 381 | return animator; |
| 382 | } |
| 383 | |
| 384 | /** |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 385 | * Constructs and returns an ObjectAnimator that animates between float values. A single |
George Mount | 16d2c9c | 2013-09-17 09:07:48 -0700 | [diff] [blame] | 386 | * value implies that that value is the one being animated to. Two values imply starting |
Chet Haase | b39f051 | 2011-05-24 14:36:40 -0700 | [diff] [blame] | 387 | * and ending values. More than two values imply a starting value, values to animate through |
| 388 | * along the way, and an ending value (these values will be distributed evenly across |
| 389 | * the duration of the animation). |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 390 | * |
Patrick Dubroy | 51ae5fc | 2011-01-16 14:23:15 -0800 | [diff] [blame] | 391 | * @param target The object whose property is to be animated. This object should |
| 392 | * have a public method on it called <code>setName()</code>, where <code>name</code> is |
| 393 | * the value of the <code>propertyName</code> parameter. |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 394 | * @param propertyName The name of the property being animated. |
| 395 | * @param values A set of values that the animation will animate between over time. |
Chet Haase | b39f051 | 2011-05-24 14:36:40 -0700 | [diff] [blame] | 396 | * @return An ObjectAnimator object that is set up to animate between the given values. |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 397 | */ |
| 398 | public static ObjectAnimator ofFloat(Object target, String propertyName, float... values) { |
| 399 | ObjectAnimator anim = new ObjectAnimator(target, propertyName); |
| 400 | anim.setFloatValues(values); |
| 401 | return anim; |
| 402 | } |
| 403 | |
| 404 | /** |
George Mount | c96c7b2 | 2013-08-23 13:31:31 -0700 | [diff] [blame] | 405 | * Constructs and returns an ObjectAnimator that animates coordinates along a <code>Path</code> |
| 406 | * using two properties. A <code>Path</code></> animation moves in two dimensions, animating |
| 407 | * coordinates <code>(x, y)</code> together to follow the line. In this variation, the |
| 408 | * coordinates are floats that are set to separate properties designated by |
| 409 | * <code>xPropertyName</code> and <code>yPropertyName</code>. |
| 410 | * |
| 411 | * @param target The object whose properties are to be animated. This object should |
| 412 | * have public methods on it called <code>setNameX()</code> and |
| 413 | * <code>setNameY</code>, where <code>nameX</code> and <code>nameY</code> |
| 414 | * are the value of the <code>xPropertyName</code> and <code>yPropertyName</code> |
| 415 | * parameters, respectively. |
| 416 | * @param xPropertyName The name of the property for the x coordinate being animated. |
| 417 | * @param yPropertyName The name of the property for the y coordinate being animated. |
| 418 | * @param path The <code>Path</code> to animate values along. |
| 419 | * @return An ObjectAnimator object that is set up to animate along <code>path</code>. |
| 420 | */ |
| 421 | public static ObjectAnimator ofFloat(Object target, String xPropertyName, String yPropertyName, |
| 422 | Path path) { |
| 423 | Keyframe[][] keyframes = PropertyValuesHolder.createKeyframes(path, false); |
| 424 | PropertyValuesHolder x = PropertyValuesHolder.ofKeyframe(xPropertyName, keyframes[0]); |
| 425 | PropertyValuesHolder y = PropertyValuesHolder.ofKeyframe(yPropertyName, keyframes[1]); |
| 426 | return ofPropertyValuesHolder(target, x, y); |
| 427 | } |
| 428 | |
| 429 | /** |
Chet Haase | b39f051 | 2011-05-24 14:36:40 -0700 | [diff] [blame] | 430 | * Constructs and returns an ObjectAnimator that animates between float values. A single |
George Mount | 16d2c9c | 2013-09-17 09:07:48 -0700 | [diff] [blame] | 431 | * value implies that that value is the one being animated to. Two values imply starting |
Chet Haase | b39f051 | 2011-05-24 14:36:40 -0700 | [diff] [blame] | 432 | * and ending values. More than two values imply a starting value, values to animate through |
| 433 | * along the way, and an ending value (these values will be distributed evenly across |
| 434 | * the duration of the animation). |
| 435 | * |
| 436 | * @param target The object whose property is to be animated. |
| 437 | * @param property The property being animated. |
| 438 | * @param values A set of values that the animation will animate between over time. |
| 439 | * @return An ObjectAnimator object that is set up to animate between the given values. |
| 440 | */ |
| 441 | public static <T> ObjectAnimator ofFloat(T target, Property<T, Float> property, |
| 442 | float... values) { |
| 443 | ObjectAnimator anim = new ObjectAnimator(target, property); |
| 444 | anim.setFloatValues(values); |
| 445 | return anim; |
| 446 | } |
| 447 | |
| 448 | /** |
George Mount | c96c7b2 | 2013-08-23 13:31:31 -0700 | [diff] [blame] | 449 | * Constructs and returns an ObjectAnimator that animates coordinates along a <code>Path</code> |
| 450 | * using two properties. A <code>Path</code></> animation moves in two dimensions, animating |
| 451 | * coordinates <code>(x, y)</code> together to follow the line. In this variation, the |
| 452 | * coordinates are floats that are set to separate properties, <code>xProperty</code> and |
| 453 | * <code>yProperty</code>. |
| 454 | * |
| 455 | * @param target The object whose properties are to be animated. |
| 456 | * @param xProperty The property for the x coordinate being animated. |
| 457 | * @param yProperty The property for the y coordinate being animated. |
| 458 | * @param path The <code>Path</code> to animate values along. |
| 459 | * @return An ObjectAnimator object that is set up to animate along <code>path</code>. |
| 460 | */ |
| 461 | public static <T> ObjectAnimator ofFloat(T target, Property<T, Float> xProperty, |
| 462 | Property<T, Float> yProperty, Path path) { |
| 463 | return ofFloat(target, xProperty.getName(), yProperty.getName(), path); |
| 464 | } |
| 465 | |
| 466 | /** |
George Mount | 4eed529 | 2013-08-30 13:56:01 -0700 | [diff] [blame] | 467 | * Constructs and returns an ObjectAnimator that animates over float values for a multiple |
| 468 | * parameters setter. Only public methods that take only float parameters are supported. |
| 469 | * Each <code>float[]</code> contains a complete set of parameters to the setter method. |
| 470 | * At least two <code>float[]</code> values must be provided, a start and end. More than two |
| 471 | * values imply a starting value, values to animate through along the way, and an ending |
| 472 | * value (these values will be distributed evenly across the duration of the animation). |
| 473 | * |
| 474 | * @param target The object whose property is to be animated. This object may |
| 475 | * have a public method on it called <code>setName()</code>, where <code>name</code> is |
| 476 | * the value of the <code>propertyName</code> parameter. <code>propertyName</code> may also |
George Mount | c96c7b2 | 2013-08-23 13:31:31 -0700 | [diff] [blame] | 477 | * be the case-sensitive complete name of the public setter method. |
George Mount | 4eed529 | 2013-08-30 13:56:01 -0700 | [diff] [blame] | 478 | * @param propertyName The name of the property being animated or the name of the setter method. |
| 479 | * @param values A set of values that the animation will animate between over time. |
| 480 | * @return An ObjectAnimator object that is set up to animate between the given values. |
| 481 | */ |
| 482 | public static ObjectAnimator ofMultiFloat(Object target, String propertyName, |
| 483 | float[][] values) { |
| 484 | PropertyValuesHolder pvh = PropertyValuesHolder.ofMultiFloat(propertyName, values); |
| 485 | return ofPropertyValuesHolder(target, pvh); |
| 486 | } |
| 487 | |
| 488 | /** |
George Mount | c96c7b2 | 2013-08-23 13:31:31 -0700 | [diff] [blame] | 489 | * Constructs and returns an ObjectAnimator that animates the target using a multi-float setter |
| 490 | * along the given <code>Path</code>. A <code>Path</code></> animation moves in two dimensions, |
| 491 | * animating coordinates <code>(x, y)</code> together to follow the line. In this variation, the |
| 492 | * coordinates are float x and y coordinates used in the first and second parameter of the |
| 493 | * setter, respectively. |
| 494 | * |
| 495 | * @param target The object whose property is to be animated. This object may |
| 496 | * have a public method on it called <code>setName()</code>, where <code>name</code> is |
| 497 | * the value of the <code>propertyName</code> parameter. <code>propertyName</code> may also |
| 498 | * be the case-sensitive complete name of the public setter method. |
| 499 | * @param propertyName The name of the property being animated or the name of the setter method. |
| 500 | * @param path The <code>Path</code> to animate values along. |
| 501 | * @return An ObjectAnimator object that is set up to animate along <code>path</code>. |
| 502 | */ |
| 503 | public static ObjectAnimator ofMultiFloat(Object target, String propertyName, Path path) { |
| 504 | PropertyValuesHolder pvh = PropertyValuesHolder.ofMultiFloat(propertyName, path); |
| 505 | return ofPropertyValuesHolder(target, pvh); |
| 506 | } |
| 507 | |
| 508 | /** |
George Mount | 4eed529 | 2013-08-30 13:56:01 -0700 | [diff] [blame] | 509 | * Constructs and returns an ObjectAnimator that animates over values for a multiple float |
| 510 | * parameters setter. Only public methods that take only float parameters are supported. |
| 511 | * <p>At least two values must be provided, a start and end. More than two |
| 512 | * values imply a starting value, values to animate through along the way, and an ending |
| 513 | * value (these values will be distributed evenly across the duration of the animation).</p> |
| 514 | * |
| 515 | * @param target The object whose property is to be animated. This object may |
| 516 | * have a public method on it called <code>setName()</code>, where <code>name</code> is |
| 517 | * the value of the <code>propertyName</code> parameter. <code>propertyName</code> may also |
| 518 | * be the case-sensitive complete name of the public setter method. |
| 519 | * @param propertyName The name of the property being animated or the name of the setter method. |
| 520 | * @param converter Converts T objects into float parameters for the multi-value setter. |
| 521 | * @param evaluator A TypeEvaluator that will be called on each animation frame to |
| 522 | * provide the necessary interpolation between the Object values to derive the animated |
| 523 | * value. |
| 524 | * @param values A set of values that the animation will animate between over time. |
| 525 | * @return An ObjectAnimator object that is set up to animate between the given values. |
| 526 | */ |
| 527 | public static <T> ObjectAnimator ofMultiFloat(Object target, String propertyName, |
| 528 | TypeConverter<T, float[]> converter, TypeEvaluator<T> evaluator, T... values) { |
| 529 | PropertyValuesHolder pvh = PropertyValuesHolder.ofMultiFloat(propertyName, converter, |
| 530 | evaluator, values); |
| 531 | return ObjectAnimator.ofPropertyValuesHolder(target, pvh); |
| 532 | } |
| 533 | |
| 534 | /** |
Chet Haase | b39f051 | 2011-05-24 14:36:40 -0700 | [diff] [blame] | 535 | * Constructs and returns an ObjectAnimator that animates between Object values. A single |
George Mount | 16d2c9c | 2013-09-17 09:07:48 -0700 | [diff] [blame] | 536 | * value implies that that value is the one being animated to. Two values imply starting |
Chet Haase | b39f051 | 2011-05-24 14:36:40 -0700 | [diff] [blame] | 537 | * and ending values. More than two values imply a starting value, values to animate through |
| 538 | * along the way, and an ending value (these values will be distributed evenly across |
| 539 | * the duration of the animation). |
Chet Haase | fe59156 | 2010-07-27 11:15:37 -0700 | [diff] [blame] | 540 | * |
Patrick Dubroy | 51ae5fc | 2011-01-16 14:23:15 -0800 | [diff] [blame] | 541 | * @param target The object whose property is to be animated. This object should |
Chet Haase | b39f051 | 2011-05-24 14:36:40 -0700 | [diff] [blame] | 542 | * have a public method on it called <code>setName()</code>, where <code>name</code> is |
| 543 | * the value of the <code>propertyName</code> parameter. |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 544 | * @param propertyName The name of the property being animated. |
| 545 | * @param evaluator A TypeEvaluator that will be called on each animation frame to |
Chet Haase | b39f051 | 2011-05-24 14:36:40 -0700 | [diff] [blame] | 546 | * provide the necessary interpolation between the Object values to derive the animated |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 547 | * value. |
Chet Haase | b39f051 | 2011-05-24 14:36:40 -0700 | [diff] [blame] | 548 | * @param values A set of values that the animation will animate between over time. |
| 549 | * @return An ObjectAnimator object that is set up to animate between the given values. |
Chet Haase | fe59156 | 2010-07-27 11:15:37 -0700 | [diff] [blame] | 550 | */ |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 551 | public static ObjectAnimator ofObject(Object target, String propertyName, |
| 552 | TypeEvaluator evaluator, Object... values) { |
| 553 | ObjectAnimator anim = new ObjectAnimator(target, propertyName); |
| 554 | anim.setObjectValues(values); |
| 555 | anim.setEvaluator(evaluator); |
| 556 | return anim; |
| 557 | } |
| 558 | |
| 559 | /** |
George Mount | c96c7b2 | 2013-08-23 13:31:31 -0700 | [diff] [blame] | 560 | * Constructs and returns an ObjectAnimator that animates a property along a <code>Path</code>. |
| 561 | * A <code>Path</code></> animation moves in two dimensions, animating coordinates |
| 562 | * <code>(x, y)</code> together to follow the line. This variant animates the coordinates |
| 563 | * in a <code>PointF</code> to follow the <code>Path</code>. If the <code>Property</code> |
| 564 | * associated with <code>propertyName</code> uses a type other than <code>PointF</code>, |
| 565 | * <code>converter</code> can be used to change from <code>PointF</code> to the type |
| 566 | * associated with the <code>Property</code>. |
| 567 | * |
| 568 | * @param target The object whose property is to be animated. This object should |
| 569 | * have a public method on it called <code>setName()</code>, where <code>name</code> is |
| 570 | * the value of the <code>propertyName</code> parameter. |
| 571 | * @param propertyName The name of the property being animated. |
| 572 | * @param converter Converts a PointF to the type associated with the setter. May be |
| 573 | * null if conversion is unnecessary. |
| 574 | * @param path The <code>Path</code> to animate values along. |
| 575 | * @return An ObjectAnimator object that is set up to animate along <code>path</code>. |
| 576 | */ |
| 577 | public static ObjectAnimator ofObject(Object target, String propertyName, |
| 578 | TypeConverter<PointF, ?> converter, Path path) { |
| 579 | PropertyValuesHolder pvh = PropertyValuesHolder.ofObject(propertyName, converter, path); |
| 580 | return ofPropertyValuesHolder(target, pvh); |
| 581 | } |
| 582 | |
| 583 | /** |
Chet Haase | b39f051 | 2011-05-24 14:36:40 -0700 | [diff] [blame] | 584 | * Constructs and returns an ObjectAnimator that animates between Object values. A single |
George Mount | 16d2c9c | 2013-09-17 09:07:48 -0700 | [diff] [blame] | 585 | * value implies that that value is the one being animated to. Two values imply starting |
Chet Haase | b39f051 | 2011-05-24 14:36:40 -0700 | [diff] [blame] | 586 | * and ending values. More than two values imply a starting value, values to animate through |
| 587 | * along the way, and an ending value (these values will be distributed evenly across |
| 588 | * the duration of the animation). |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 589 | * |
Chet Haase | b39f051 | 2011-05-24 14:36:40 -0700 | [diff] [blame] | 590 | * @param target The object whose property is to be animated. |
| 591 | * @param property The property being animated. |
| 592 | * @param evaluator A TypeEvaluator that will be called on each animation frame to |
| 593 | * provide the necessary interpolation between the Object values to derive the animated |
| 594 | * value. |
| 595 | * @param values A set of values that the animation will animate between over time. |
| 596 | * @return An ObjectAnimator object that is set up to animate between the given values. |
| 597 | */ |
| 598 | public static <T, V> ObjectAnimator ofObject(T target, Property<T, V> property, |
| 599 | TypeEvaluator<V> evaluator, V... values) { |
| 600 | ObjectAnimator anim = new ObjectAnimator(target, property); |
| 601 | anim.setObjectValues(values); |
| 602 | anim.setEvaluator(evaluator); |
| 603 | return anim; |
| 604 | } |
| 605 | |
| 606 | /** |
George Mount | 16d2c9c | 2013-09-17 09:07:48 -0700 | [diff] [blame] | 607 | * Constructs and returns an ObjectAnimator that animates between Object values. A single |
| 608 | * value implies that that value is the one being animated to. Two values imply starting |
| 609 | * and ending values. More than two values imply a starting value, values to animate through |
| 610 | * along the way, and an ending value (these values will be distributed evenly across |
| 611 | * the duration of the animation). This variant supplies a <code>TypeConverter</code> to |
| 612 | * convert from the animated values to the type of the property. If only one value is |
| 613 | * supplied, the <code>TypeConverter</code> must implement |
| 614 | * {@link TypeConverter#convertBack(Object)} to retrieve the current value. |
| 615 | * |
| 616 | * @param target The object whose property is to be animated. |
| 617 | * @param property The property being animated. |
| 618 | * @param converter Converts the animated object to the Property type. |
| 619 | * @param evaluator A TypeEvaluator that will be called on each animation frame to |
| 620 | * provide the necessary interpolation between the Object values to derive the animated |
| 621 | * value. |
| 622 | * @param values A set of values that the animation will animate between over time. |
| 623 | * @return An ObjectAnimator object that is set up to animate between the given values. |
| 624 | */ |
| 625 | public static <T, V, P> ObjectAnimator ofObject(T target, Property<T, P> property, |
| 626 | TypeConverter<V, P> converter, TypeEvaluator<V> evaluator, V... values) { |
| 627 | PropertyValuesHolder pvh = PropertyValuesHolder.ofObject(property, converter, evaluator, |
| 628 | values); |
| 629 | return ofPropertyValuesHolder(target, pvh); |
| 630 | } |
| 631 | |
| 632 | /** |
George Mount | c96c7b2 | 2013-08-23 13:31:31 -0700 | [diff] [blame] | 633 | * Constructs and returns an ObjectAnimator that animates a property along a <code>Path</code>. |
| 634 | * A <code>Path</code></> animation moves in two dimensions, animating coordinates |
| 635 | * <code>(x, y)</code> together to follow the line. This variant animates the coordinates |
| 636 | * in a <code>PointF</code> to follow the <code>Path</code>. If <code>property</code> |
| 637 | * uses a type other than <code>PointF</code>, <code>converter</code> can be used to change |
| 638 | * from <code>PointF</code> to the type associated with the <code>Property</code>. |
| 639 | * |
| 640 | * @param target The object whose property is to be animated. |
| 641 | * @param property The property being animated. Should not be null. |
| 642 | * @param converter Converts a PointF to the type associated with the setter. May be |
| 643 | * null if conversion is unnecessary. |
| 644 | * @param path The <code>Path</code> to animate values along. |
| 645 | * @return An ObjectAnimator object that is set up to animate along <code>path</code>. |
| 646 | */ |
| 647 | public static <T, V> ObjectAnimator ofObject(T target, Property<T, V> property, |
| 648 | TypeConverter<PointF, V> converter, Path path) { |
| 649 | PropertyValuesHolder pvh = PropertyValuesHolder.ofObject(property, converter, path); |
| 650 | return ofPropertyValuesHolder(target, pvh); |
| 651 | } |
| 652 | |
| 653 | /** |
Chet Haase | b39f051 | 2011-05-24 14:36:40 -0700 | [diff] [blame] | 654 | * Constructs and returns an ObjectAnimator that animates between the sets of values specified |
| 655 | * in <code>PropertyValueHolder</code> objects. This variant should be used when animating |
| 656 | * several properties at once with the same ObjectAnimator, since PropertyValuesHolder allows |
| 657 | * you to associate a set of animation values with a property name. |
| 658 | * |
| 659 | * @param target The object whose property is to be animated. Depending on how the |
| 660 | * PropertyValuesObjects were constructed, the target object should either have the {@link |
| 661 | * android.util.Property} objects used to construct the PropertyValuesHolder objects or (if the |
| 662 | * PropertyValuesHOlder objects were created with property names) the target object should have |
| 663 | * public methods on it called <code>setName()</code>, where <code>name</code> is the name of |
| 664 | * the property passed in as the <code>propertyName</code> parameter for each of the |
| 665 | * PropertyValuesHolder objects. |
| 666 | * @param values A set of PropertyValuesHolder objects whose values will be animated between |
| 667 | * over time. |
| 668 | * @return An ObjectAnimator object that is set up to animate between the given values. |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 669 | */ |
| 670 | public static ObjectAnimator ofPropertyValuesHolder(Object target, |
| 671 | PropertyValuesHolder... values) { |
| 672 | ObjectAnimator anim = new ObjectAnimator(); |
Patrick Dubroy | 51ae5fc | 2011-01-16 14:23:15 -0800 | [diff] [blame] | 673 | anim.mTarget = target; |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 674 | anim.setValues(values); |
| 675 | return anim; |
Chet Haase | 3dd207a | 2010-07-20 14:00:01 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Romain Guy | 83d6e82 | 2010-10-14 10:13:53 -0700 | [diff] [blame] | 678 | @Override |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 679 | public void setIntValues(int... values) { |
Romain Guy | 83d6e82 | 2010-10-14 10:13:53 -0700 | [diff] [blame] | 680 | if (mValues == null || mValues.length == 0) { |
| 681 | // No values yet - this animator is being constructed piecemeal. Init the values with |
| 682 | // whatever the current propertyName is |
Chet Haase | b39f051 | 2011-05-24 14:36:40 -0700 | [diff] [blame] | 683 | if (mProperty != null) { |
| 684 | setValues(PropertyValuesHolder.ofInt(mProperty, values)); |
| 685 | } else { |
| 686 | setValues(PropertyValuesHolder.ofInt(mPropertyName, values)); |
| 687 | } |
Romain Guy | 83d6e82 | 2010-10-14 10:13:53 -0700 | [diff] [blame] | 688 | } else { |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 689 | super.setIntValues(values); |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | @Override |
| 694 | public void setFloatValues(float... values) { |
| 695 | if (mValues == null || mValues.length == 0) { |
| 696 | // No values yet - this animator is being constructed piecemeal. Init the values with |
| 697 | // whatever the current propertyName is |
Chet Haase | b39f051 | 2011-05-24 14:36:40 -0700 | [diff] [blame] | 698 | if (mProperty != null) { |
| 699 | setValues(PropertyValuesHolder.ofFloat(mProperty, values)); |
| 700 | } else { |
| 701 | setValues(PropertyValuesHolder.ofFloat(mPropertyName, values)); |
| 702 | } |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 703 | } else { |
| 704 | super.setFloatValues(values); |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | @Override |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 709 | public void setObjectValues(Object... values) { |
| 710 | if (mValues == null || mValues.length == 0) { |
| 711 | // No values yet - this animator is being constructed piecemeal. Init the values with |
| 712 | // whatever the current propertyName is |
Chet Haase | b39f051 | 2011-05-24 14:36:40 -0700 | [diff] [blame] | 713 | if (mProperty != null) { |
Chet Haase | be19e03 | 2013-03-15 17:08:55 -0700 | [diff] [blame] | 714 | setValues(PropertyValuesHolder.ofObject(mProperty, (TypeEvaluator) null, values)); |
Chet Haase | b39f051 | 2011-05-24 14:36:40 -0700 | [diff] [blame] | 715 | } else { |
Chet Haase | be19e03 | 2013-03-15 17:08:55 -0700 | [diff] [blame] | 716 | setValues(PropertyValuesHolder.ofObject(mPropertyName, |
| 717 | (TypeEvaluator) null, values)); |
Chet Haase | b39f051 | 2011-05-24 14:36:40 -0700 | [diff] [blame] | 718 | } |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 719 | } else { |
| 720 | super.setObjectValues(values); |
Romain Guy | 83d6e82 | 2010-10-14 10:13:53 -0700 | [diff] [blame] | 721 | } |
Chet Haase | 0e0590b | 2010-09-26 11:57:28 -0700 | [diff] [blame] | 722 | } |
Romain Guy | 83d6e82 | 2010-10-14 10:13:53 -0700 | [diff] [blame] | 723 | |
Chet Haase | be19e03 | 2013-03-15 17:08:55 -0700 | [diff] [blame] | 724 | /** |
| 725 | * autoCancel controls whether an ObjectAnimator will be canceled automatically |
| 726 | * when any other ObjectAnimator with the same target and properties is started. |
| 727 | * Setting this flag may make it easier to run different animators on the same target |
| 728 | * object without having to keep track of whether there are conflicting animators that |
| 729 | * need to be manually canceled. Canceling animators must have the same exact set of |
| 730 | * target properties, in the same order. |
| 731 | * |
| 732 | * @param cancel Whether future ObjectAnimators with the same target and properties |
| 733 | * as this ObjectAnimator will cause this ObjectAnimator to be canceled. |
| 734 | */ |
| 735 | public void setAutoCancel(boolean cancel) { |
| 736 | mAutoCancel = cancel; |
| 737 | } |
| 738 | |
| 739 | private boolean hasSameTargetAndProperties(Animator anim) { |
| 740 | if (anim instanceof ObjectAnimator) { |
| 741 | PropertyValuesHolder[] theirValues = ((ObjectAnimator) anim).getValues(); |
| 742 | if (((ObjectAnimator) anim).getTarget() == mTarget && |
| 743 | mValues.length == theirValues.length) { |
| 744 | for (int i = 0; i < mValues.length; ++i) { |
| 745 | PropertyValuesHolder pvhMine = mValues[i]; |
| 746 | PropertyValuesHolder pvhTheirs = theirValues[i]; |
| 747 | if (pvhMine.getPropertyName() == null || |
| 748 | !pvhMine.getPropertyName().equals(pvhTheirs.getPropertyName())) { |
| 749 | return false; |
| 750 | } |
| 751 | } |
| 752 | return true; |
| 753 | } |
| 754 | } |
| 755 | return false; |
| 756 | } |
| 757 | |
Chet Haase | e2ab7cc | 2010-12-06 16:10:07 -0800 | [diff] [blame] | 758 | @Override |
| 759 | public void start() { |
Chet Haase | be19e03 | 2013-03-15 17:08:55 -0700 | [diff] [blame] | 760 | // See if any of the current active/pending animators need to be canceled |
| 761 | AnimationHandler handler = sAnimationHandler.get(); |
| 762 | if (handler != null) { |
| 763 | int numAnims = handler.mAnimations.size(); |
| 764 | for (int i = numAnims - 1; i >= 0; i--) { |
| 765 | if (handler.mAnimations.get(i) instanceof ObjectAnimator) { |
| 766 | ObjectAnimator anim = (ObjectAnimator) handler.mAnimations.get(i); |
| 767 | if (anim.mAutoCancel && hasSameTargetAndProperties(anim)) { |
| 768 | anim.cancel(); |
| 769 | } |
| 770 | } |
| 771 | } |
| 772 | numAnims = handler.mPendingAnimations.size(); |
| 773 | for (int i = numAnims - 1; i >= 0; i--) { |
| 774 | if (handler.mPendingAnimations.get(i) instanceof ObjectAnimator) { |
| 775 | ObjectAnimator anim = (ObjectAnimator) handler.mPendingAnimations.get(i); |
| 776 | if (anim.mAutoCancel && hasSameTargetAndProperties(anim)) { |
| 777 | anim.cancel(); |
| 778 | } |
| 779 | } |
| 780 | } |
| 781 | numAnims = handler.mDelayedAnims.size(); |
| 782 | for (int i = numAnims - 1; i >= 0; i--) { |
| 783 | if (handler.mDelayedAnims.get(i) instanceof ObjectAnimator) { |
| 784 | ObjectAnimator anim = (ObjectAnimator) handler.mDelayedAnims.get(i); |
| 785 | if (anim.mAutoCancel && hasSameTargetAndProperties(anim)) { |
| 786 | anim.cancel(); |
| 787 | } |
| 788 | } |
| 789 | } |
| 790 | } |
Chet Haase | e2ab7cc | 2010-12-06 16:10:07 -0800 | [diff] [blame] | 791 | if (DBG) { |
Chet Haase | 3c4ce72 | 2011-09-02 15:37:25 -0700 | [diff] [blame] | 792 | Log.d("ObjectAnimator", "Anim target, duration: " + mTarget + ", " + getDuration()); |
Chet Haase | e2ab7cc | 2010-12-06 16:10:07 -0800 | [diff] [blame] | 793 | for (int i = 0; i < mValues.length; ++i) { |
| 794 | PropertyValuesHolder pvh = mValues[i]; |
| 795 | ArrayList<Keyframe> keyframes = pvh.mKeyframeSet.mKeyframes; |
| 796 | Log.d("ObjectAnimator", " Values[" + i + "]: " + |
| 797 | pvh.getPropertyName() + ", " + keyframes.get(0).getValue() + ", " + |
| 798 | keyframes.get(pvh.mKeyframeSet.mNumKeyframes - 1).getValue()); |
| 799 | } |
| 800 | } |
| 801 | super.start(); |
| 802 | } |
| 803 | |
Chet Haase | 3dd207a | 2010-07-20 14:00:01 -0700 | [diff] [blame] | 804 | /** |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 805 | * This function is called immediately before processing the first animation |
| 806 | * frame of an animation. If there is a nonzero <code>startDelay</code>, the |
| 807 | * function is called after that delay ends. |
| 808 | * It takes care of the final initialization steps for the |
| 809 | * animation. This includes setting mEvaluator, if the user has not yet |
| 810 | * set it up, and the setter/getter methods, if the user did not supply |
| 811 | * them. |
| 812 | * |
| 813 | * <p>Overriders of this method should call the superclass method to cause |
| 814 | * internal mechanisms to be set up correctly.</p> |
| 815 | */ |
| 816 | @Override |
| 817 | void initAnimation() { |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 818 | if (!mInitialized) { |
| 819 | // mValueType may change due to setter/getter setup; do this before calling super.init(), |
| 820 | // which uses mValueType to set up the default type evaluator. |
| 821 | int numValues = mValues.length; |
| 822 | for (int i = 0; i < numValues; ++i) { |
Patrick Dubroy | 51ae5fc | 2011-01-16 14:23:15 -0800 | [diff] [blame] | 823 | mValues[i].setupSetterAndGetter(mTarget); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 824 | } |
| 825 | super.initAnimation(); |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 826 | } |
| 827 | } |
| 828 | |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 829 | /** |
| 830 | * Sets the length of the animation. The default duration is 300 milliseconds. |
| 831 | * |
| 832 | * @param duration The length of the animation, in milliseconds. |
| 833 | * @return ObjectAnimator The object called with setDuration(). This return |
| 834 | * value makes it easier to compose statements together that construct and then set the |
| 835 | * duration, as in |
| 836 | * <code>ObjectAnimator.ofInt(target, propertyName, 0, 10).setDuration(500).start()</code>. |
| 837 | */ |
| 838 | @Override |
| 839 | public ObjectAnimator setDuration(long duration) { |
| 840 | super.setDuration(duration); |
| 841 | return this; |
| 842 | } |
| 843 | |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 844 | |
| 845 | /** |
| 846 | * The target object whose property will be animated by this animation |
| 847 | * |
Patrick Dubroy | 51ae5fc | 2011-01-16 14:23:15 -0800 | [diff] [blame] | 848 | * @return The object being animated |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 849 | */ |
| 850 | public Object getTarget() { |
Patrick Dubroy | 51ae5fc | 2011-01-16 14:23:15 -0800 | [diff] [blame] | 851 | return mTarget; |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | /** |
Patrick Dubroy | 51ae5fc | 2011-01-16 14:23:15 -0800 | [diff] [blame] | 855 | * Sets the target object whose property will be animated by this animation |
Chet Haase | f54a8d7 | 2010-07-22 14:44:59 -0700 | [diff] [blame] | 856 | * |
| 857 | * @param target The object being animated |
| 858 | */ |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 859 | @Override |
Chet Haase | f54a8d7 | 2010-07-22 14:44:59 -0700 | [diff] [blame] | 860 | public void setTarget(Object target) { |
Patrick Dubroy | 51ae5fc | 2011-01-16 14:23:15 -0800 | [diff] [blame] | 861 | if (mTarget != target) { |
Patrick Dubroy | 7beecfa | 2011-01-16 14:42:39 -0800 | [diff] [blame] | 862 | final Object oldTarget = mTarget; |
Patrick Dubroy | 51ae5fc | 2011-01-16 14:23:15 -0800 | [diff] [blame] | 863 | mTarget = target; |
Patrick Dubroy | 7beecfa | 2011-01-16 14:42:39 -0800 | [diff] [blame] | 864 | if (oldTarget != null && target != null && oldTarget.getClass() == target.getClass()) { |
Chet Haase | 70d4ba1 | 2010-10-06 09:46:45 -0700 | [diff] [blame] | 865 | return; |
| 866 | } |
| 867 | // New target type should cause re-initialization prior to starting |
| 868 | mInitialized = false; |
| 869 | } |
Chet Haase | f54a8d7 | 2010-07-22 14:44:59 -0700 | [diff] [blame] | 870 | } |
| 871 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 872 | @Override |
| 873 | public void setupStartValues() { |
| 874 | initAnimation(); |
| 875 | int numValues = mValues.length; |
| 876 | for (int i = 0; i < numValues; ++i) { |
Patrick Dubroy | 51ae5fc | 2011-01-16 14:23:15 -0800 | [diff] [blame] | 877 | mValues[i].setupStartValue(mTarget); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 878 | } |
| 879 | } |
| 880 | |
| 881 | @Override |
| 882 | public void setupEndValues() { |
| 883 | initAnimation(); |
| 884 | int numValues = mValues.length; |
| 885 | for (int i = 0; i < numValues; ++i) { |
Patrick Dubroy | 51ae5fc | 2011-01-16 14:23:15 -0800 | [diff] [blame] | 886 | mValues[i].setupEndValue(mTarget); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 887 | } |
| 888 | } |
| 889 | |
Chet Haase | f54a8d7 | 2010-07-22 14:44:59 -0700 | [diff] [blame] | 890 | /** |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 891 | * This method is called with the elapsed fraction of the animation during every |
| 892 | * animation frame. This function turns the elapsed fraction into an interpolated fraction |
| 893 | * and then into an animated value (from the evaluator. The function is called mostly during |
| 894 | * animation updates, but it is also called when the <code>end()</code> |
| 895 | * function is called, to set the final value on the property. |
| 896 | * |
| 897 | * <p>Overrides of this method must call the superclass to perform the calculation |
| 898 | * of the animated value.</p> |
| 899 | * |
| 900 | * @param fraction The elapsed fraction of the animation. |
| 901 | */ |
| 902 | @Override |
| 903 | void animateValue(float fraction) { |
| 904 | super.animateValue(fraction); |
Chet Haase | 602e4d3 | 2010-08-16 08:57:23 -0700 | [diff] [blame] | 905 | int numValues = mValues.length; |
| 906 | for (int i = 0; i < numValues; ++i) { |
Patrick Dubroy | 51ae5fc | 2011-01-16 14:23:15 -0800 | [diff] [blame] | 907 | mValues[i].setAnimatedValue(mTarget); |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 908 | } |
| 909 | } |
Chet Haase | 49afa5b | 2010-08-23 11:39:53 -0700 | [diff] [blame] | 910 | |
| 911 | @Override |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 912 | public ObjectAnimator clone() { |
| 913 | final ObjectAnimator anim = (ObjectAnimator) super.clone(); |
Chet Haase | 49afa5b | 2010-08-23 11:39:53 -0700 | [diff] [blame] | 914 | return anim; |
| 915 | } |
Chet Haase | e9140a7 | 2011-02-16 16:23:29 -0800 | [diff] [blame] | 916 | |
| 917 | @Override |
| 918 | public String toString() { |
| 919 | String returnVal = "ObjectAnimator@" + Integer.toHexString(hashCode()) + ", target " + |
| 920 | mTarget; |
| 921 | if (mValues != null) { |
| 922 | for (int i = 0; i < mValues.length; ++i) { |
| 923 | returnVal += "\n " + mValues[i].toString(); |
| 924 | } |
| 925 | } |
| 926 | return returnVal; |
| 927 | } |
Chet Haase | 17fb4b0 | 2010-06-28 17:55:07 -0700 | [diff] [blame] | 928 | } |