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