ObjectAnimator.java revision 6e0ecb4eed5cd2e1f15766d7028467129974a12d
117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase/*
217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Copyright (C) 2010 The Android Open Source Project
317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase *
417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Licensed under the Apache License, Version 2.0 (the "License");
517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * you may not use this file except in compliance with the License.
617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * You may obtain a copy of the License at
717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase *
817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase *      http://www.apache.org/licenses/LICENSE-2.0
917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase *
1017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * Unless required by applicable law or agreed to in writing, software
1117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * distributed under the License is distributed on an "AS IS" BASIS,
1217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * See the License for the specific language governing permissions and
1417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * limitations under the License.
1517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */
1617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
1717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haasepackage android.animation;
1817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
1917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haaseimport android.util.Log;
2017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
2117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haaseimport java.lang.reflect.Method;
2217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
2317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase/**
24a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * This subclass of {@link ValueAnimator} provides support for animating properties on target objects.
2517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * The constructors of this class take parameters to define the target object that will be animated
2617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * as well as the name of the property that will be animated. Appropriate set/get functions
2717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * are then determined internally and the animation will call these functions as necessary to
2817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase * animate the property.
296e0ecb4eed5cd2e1f15766d7028467129974a12dChet Haase *
306e0ecb4eed5cd2e1f15766d7028467129974a12dChet Haase * @see #setPropertyName(String)
316e0ecb4eed5cd2e1f15766d7028467129974a12dChet Haase *
3217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase */
332794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haasepublic final class ObjectAnimator extends ValueAnimator {
3417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
3517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    // The target object on which the property exists, set in the constructor
3617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private Object mTarget;
3717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
3817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    private String mPropertyName;
3917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
4017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
4117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Sets the name of the property that will be animated. This name is used to derive
4217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * a setter function that will be called to set animated values.
4317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * For example, a property name of <code>foo</code> will result
4417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * in a call to the function <code>setFoo()</code> on the target object. If either
4517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <code>valueFrom</code> or <code>valueTo</code> is null, then a getter function will
4617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * also be derived and called.
4717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
486e0ecb4eed5cd2e1f15766d7028467129974a12dChet Haase     * <p>For best performance of the mechanism that calls the setter function determined by the
496e0ecb4eed5cd2e1f15766d7028467129974a12dChet Haase     * name of the property being animated, use <code>float</code> or <code>int</code> typed values,
506e0ecb4eed5cd2e1f15766d7028467129974a12dChet Haase     * and make the setter function for those properties have a <code>void</code> return value. This
516e0ecb4eed5cd2e1f15766d7028467129974a12dChet Haase     * will cause the code to take an optimized path for these constrained circumstances. Other
526e0ecb4eed5cd2e1f15766d7028467129974a12dChet Haase     * property types and return types will work, but will have more overhead in processing
536e0ecb4eed5cd2e1f15766d7028467129974a12dChet Haase     * the requests due to normal reflection mechanisms.</p>
546e0ecb4eed5cd2e1f15766d7028467129974a12dChet Haase     *
5517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <p>Note that the setter function derived from this property name
5617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * must take the same parameter type as the
5717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <code>valueFrom</code> and <code>valueTo</code> properties, otherwise the call to
5817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * the setter function will fail.</p>
5917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
60a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * <p>If this ObjectAnimator has been set up to animate several properties together,
61d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * using more than one PropertyValuesHolder objects, then setting the propertyName simply
62d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * sets the propertyName in the first of those PropertyValuesHolder objects.</p>
63d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     *
6417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param propertyName The name of the property being animated.
6517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
6617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public void setPropertyName(String propertyName) {
670e0590bf3cb32e73f423c0fe39a180d4b3c4343dChet Haase        // mValues could be null if this is being constructed piecemeal. Just record the
680e0590bf3cb32e73f423c0fe39a180d4b3c4343dChet Haase        // propertyName to be used later when setValues() is called if so.
69d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase        if (mValues != null) {
70602e4d3824bf8b9cb9f817375d195b969712176aChet Haase            PropertyValuesHolder valuesHolder = mValues[0];
71602e4d3824bf8b9cb9f817375d195b969712176aChet Haase            String oldName = valuesHolder.getPropertyName();
72d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase            valuesHolder.setPropertyName(propertyName);
73602e4d3824bf8b9cb9f817375d195b969712176aChet Haase            mValuesMap.remove(oldName);
74602e4d3824bf8b9cb9f817375d195b969712176aChet Haase            mValuesMap.put(propertyName, valuesHolder);
75d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase        }
7617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        mPropertyName = propertyName;
770e0590bf3cb32e73f423c0fe39a180d4b3c4343dChet Haase        // New property/values/target should cause re-initialization prior to starting
780e0590bf3cb32e73f423c0fe39a180d4b3c4343dChet Haase        mInitialized = false;
7917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
8017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
8117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
8217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Gets the name of the property that will be animated. This name will be used to derive
8317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * a setter function that will be called to set animated values.
8417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * For example, a property name of <code>foo</code> will result
8517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * in a call to the function <code>setFoo()</code> on the target object. If either
8617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <code>valueFrom</code> or <code>valueTo</code> is null, then a getter function will
8717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * also be derived and called.
8817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
8917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public String getPropertyName() {
9017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        return mPropertyName;
9117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
9217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
9317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
9417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * Determine the setter or getter function using the JavaBeans convention of setFoo or
9517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * getFoo for a property named 'foo'. This function figures out what the name of the
9617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * function should be and uses reflection to find the Method with that name on the
9717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * target object.
9817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
9917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param prefix "set" or "get", depending on whether we need a setter or getter.
10017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @return Method the method associated with mPropertyName.
10117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
102fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase    private Method getPropertyFunction(String prefix, Class valueType) {
10317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        // TODO: faster implementation...
10417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        Method returnVal = null;
10517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        String firstLetter = mPropertyName.substring(0, 1);
10617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        String theRest = mPropertyName.substring(1);
10717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        firstLetter = firstLetter.toUpperCase();
10817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        String setterName = prefix + firstLetter + theRest;
109fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase        Class args[] = null;
110fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase        if (valueType != null) {
111fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase            args = new Class[1];
112fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase            args[0] = valueType;
113fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase        }
11417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        try {
11517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase            returnVal = mTarget.getClass().getMethod(setterName, args);
11617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        } catch (NoSuchMethodException e) {
117a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase            Log.e("ObjectAnimator",
118fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase                    "Couldn't find setter/getter for property " + mPropertyName + ": " + e);
11917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
12017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        return returnVal;
12117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
12217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
12317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
124a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Creates a new ObjectAnimator object. This default constructor is primarily for
125d51d368f2d512ab657b8ae45780c82c0dbea94c3Chet Haase     * use internally; the other constructors which take parameters are more generally
126d51d368f2d512ab657b8ae45780c82c0dbea94c3Chet Haase     * useful.
127d51d368f2d512ab657b8ae45780c82c0dbea94c3Chet Haase     */
128a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public ObjectAnimator() {
129d51d368f2d512ab657b8ae45780c82c0dbea94c3Chet Haase    }
130d51d368f2d512ab657b8ae45780c82c0dbea94c3Chet Haase
131d51d368f2d512ab657b8ae45780c82c0dbea94c3Chet Haase    /**
132d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * A constructor that takes a single property name and set of values. This constructor is
133d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * used in the simple case of animating a single property.
134fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     *
135fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     * @param target The object whose property is to be animated. This object should
136d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * have a public method on it called <code>setName()</code>, where <code>name</code> is
137d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * the value of the <code>propertyName</code> parameter.
138d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * @param propertyName The name of the property being animated.
139fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     */
1402794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    private ObjectAnimator(Object target, String propertyName) {
141fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase        mTarget = target;
142d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase        setPropertyName(propertyName);
143fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase    }
144fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase
145fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase    /**
1462794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Constructs and returns an ObjectAnimator that animates between int values. A single
1472794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value implies that that value is the one being animated to. However, this is not typically
1482794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * useful in a ValueAnimator object because there is no way for the object to determine the
1492794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * starting value for the animation (unlike ObjectAnimator, which can derive that value
1502794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * from the target object and property being animated). Therefore, there should typically
1512794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * be two or more values.
1522794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
1532794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param target The object whose property is to be animated. This object should
1542794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * have a public method on it called <code>setName()</code>, where <code>name</code> is
1552794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * the value of the <code>propertyName</code> parameter.
1562794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param propertyName The name of the property being animated.
1572794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values A set of values that the animation will animate between over time.
1582794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @return A ValueAnimator object that is set up to animate between the given values.
1592794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
1602794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public static ObjectAnimator ofInt(Object target, String propertyName, int... values) {
1612794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        ObjectAnimator anim = new ObjectAnimator(target, propertyName);
1622794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        anim.setIntValues(values);
1632794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return anim;
1642794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
1652794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase
1662794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
1672794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Constructs and returns an ObjectAnimator that animates between float values. A single
1682794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value implies that that value is the one being animated to. However, this is not typically
1692794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * useful in a ValueAnimator object because there is no way for the object to determine the
1702794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * starting value for the animation (unlike ObjectAnimator, which can derive that value
1712794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * from the target object and property being animated). Therefore, there should typically
1722794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * be two or more values.
1732794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
1742794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param target The object whose property is to be animated. This object should
1752794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * have a public method on it called <code>setName()</code>, where <code>name</code> is
1762794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * the value of the <code>propertyName</code> parameter.
1772794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param propertyName The name of the property being animated.
1782794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values A set of values that the animation will animate between over time.
1792794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @return A ValueAnimator object that is set up to animate between the given values.
1802794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
1812794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public static ObjectAnimator ofFloat(Object target, String propertyName, float... values) {
1822794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        ObjectAnimator anim = new ObjectAnimator(target, propertyName);
1832794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        anim.setFloatValues(values);
1842794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return anim;
1852794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
1862794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase
1872794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
188d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * A constructor that takes <code>PropertyValueHolder</code> values. This constructor should
189a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * be used when animating several properties at once with the same ObjectAnimator, since
190d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * PropertyValuesHolder allows you to associate a set of animation values with a property
191d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * name.
19217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
19317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param target The object whose property is to be animated. This object should
194d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * have public methods on it called <code>setName()</code>, where <code>name</code> is
195d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * the name of the property passed in as the <code>propertyName</code> parameter for
196d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * each of the PropertyValuesHolder objects.
1972794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param propertyName The name of the property being animated.
1982794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param evaluator A TypeEvaluator that will be called on each animation frame to
1992794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * provide the ncessry interpolation between the Object values to derive the animated
2002794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value.
201d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * @param values The PropertyValuesHolder objects which hold each the property name and values
202d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     * to animate that property between.
203d953d08e9299072130d9f4411cbcf6678bbce822Chet Haase     */
2042794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public static ObjectAnimator ofObject(Object target, String propertyName,
2052794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            TypeEvaluator evaluator, Object... values) {
2062794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        ObjectAnimator anim = new ObjectAnimator(target, propertyName);
2072794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        anim.setObjectValues(values);
2082794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        anim.setEvaluator(evaluator);
2092794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return anim;
2102794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
2112794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase
2122794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
2132794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Constructs and returns an ObjectAnimator that animates between the sets of values
2142794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * specifed in <code>PropertyValueHolder</code> objects. This variant should
2152794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * be used when animating several properties at once with the same ObjectAnimator, since
2162794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * PropertyValuesHolder allows you to associate a set of animation values with a property
2172794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * name.
2182794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
2192794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param target The object whose property is to be animated. This object should
2202794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * have public methods on it called <code>setName()</code>, where <code>name</code> is
2212794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * the name of the property passed in as the <code>propertyName</code> parameter for
2222794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * each of the PropertyValuesHolder objects.
2232794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param values A set of PropertyValuesHolder objects whose values will be animated
2242794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * between over time.
2252794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @return A ValueAnimator object that is set up to animate between the given values.
2262794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
2272794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public static ObjectAnimator ofPropertyValuesHolder(Object target,
2282794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            PropertyValuesHolder... values) {
2292794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        ObjectAnimator anim = new ObjectAnimator();
2302794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        anim.mTarget = target;
2312794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        anim.setValues(values);
2322794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return anim;
2333dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase    }
2343dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase
23583d6e8213230fb0805aa019d266842253baeb114Romain Guy    @Override
2362794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public void setIntValues(int... values) {
23783d6e8213230fb0805aa019d266842253baeb114Romain Guy        if (mValues == null || mValues.length == 0) {
23883d6e8213230fb0805aa019d266842253baeb114Romain Guy            // No values yet - this animator is being constructed piecemeal. Init the values with
23983d6e8213230fb0805aa019d266842253baeb114Romain Guy            // whatever the current propertyName is
2402794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            setValues(PropertyValuesHolder.ofInt(mPropertyName, values));
24183d6e8213230fb0805aa019d266842253baeb114Romain Guy        } else {
2422794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            super.setIntValues(values);
2432794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        }
2442794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
2452794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase
2462794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    @Override
2472794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public void setFloatValues(float... values) {
2482794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (mValues == null || mValues.length == 0) {
2492794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            // No values yet - this animator is being constructed piecemeal. Init the values with
2502794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            // whatever the current propertyName is
2512794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            setValues(PropertyValuesHolder.ofFloat(mPropertyName, values));
2522794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        } else {
2532794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            super.setFloatValues(values);
2542794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        }
2552794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
2562794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase
2572794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    @Override
2582794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public void setObjectValues(Object... values) {
2592794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        if (mValues == null || mValues.length == 0) {
2602794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            // No values yet - this animator is being constructed piecemeal. Init the values with
2612794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            // whatever the current propertyName is
2622794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            setValues(PropertyValuesHolder.ofObject(mPropertyName, (TypeEvaluator)null, values));
2632794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        } else {
2642794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase            super.setObjectValues(values);
26583d6e8213230fb0805aa019d266842253baeb114Romain Guy        }
2660e0590bf3cb32e73f423c0fe39a180d4b3c4343dChet Haase    }
26783d6e8213230fb0805aa019d266842253baeb114Romain Guy
2683dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase    /**
26917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * This function is called immediately before processing the first animation
27017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * frame of an animation. If there is a nonzero <code>startDelay</code>, the
27117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * function is called after that delay ends.
27217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * It takes care of the final initialization steps for the
27317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * animation. This includes setting mEvaluator, if the user has not yet
27417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * set it up, and the setter/getter methods, if the user did not supply
27517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * them.
27617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
27717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *  <p>Overriders of this method should call the superclass method to cause
27817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *  internal mechanisms to be set up correctly.</p>
27917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
28017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    @Override
28117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    void initAnimation() {
28221cd1389d2ef218b20994b617c57af120841a57fChet Haase        if (!mInitialized) {
28321cd1389d2ef218b20994b617c57af120841a57fChet Haase            // mValueType may change due to setter/getter setup; do this before calling super.init(),
28421cd1389d2ef218b20994b617c57af120841a57fChet Haase            // which uses mValueType to set up the default type evaluator.
28521cd1389d2ef218b20994b617c57af120841a57fChet Haase            int numValues = mValues.length;
28621cd1389d2ef218b20994b617c57af120841a57fChet Haase            for (int i = 0; i < numValues; ++i) {
28721cd1389d2ef218b20994b617c57af120841a57fChet Haase                mValues[i].setupSetterAndGetter(mTarget);
28821cd1389d2ef218b20994b617c57af120841a57fChet Haase            }
28921cd1389d2ef218b20994b617c57af120841a57fChet Haase            super.initAnimation();
29017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
29117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
29217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
2932794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    /**
2942794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * Sets the length of the animation. The default duration is 300 milliseconds.
2952794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     *
2962794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @param duration The length of the animation, in milliseconds.
2972794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * @return ObjectAnimator The object called with setDuration(). This return
2982794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * value makes it easier to compose statements together that construct and then set the
2992794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * duration, as in
3002794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     * <code>ObjectAnimator.ofInt(target, propertyName, 0, 10).setDuration(500).start()</code>.
3012794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase     */
3022794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    @Override
3032794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    public ObjectAnimator setDuration(long duration) {
3042794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        super.setDuration(duration);
3052794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase        return this;
3062794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase    }
3072794eb3b02e2404d453d3ad22a8a85a138130a07Chet Haase
30817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
30917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
31017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * The target object whose property will be animated by this animation
31117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
31217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @return The object being animated
31317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
31417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    public Object getTarget() {
31517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        return mTarget;
31617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
31717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase
31817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    /**
319f54a8d7c479485174941c38f151ea7083c658da3Chet Haase     * Sets the target object whose property will be animated by this animation
320f54a8d7c479485174941c38f151ea7083c658da3Chet Haase     *
321f54a8d7c479485174941c38f151ea7083c658da3Chet Haase     * @param target The object being animated
322f54a8d7c479485174941c38f151ea7083c658da3Chet Haase     */
32321cd1389d2ef218b20994b617c57af120841a57fChet Haase    @Override
324f54a8d7c479485174941c38f151ea7083c658da3Chet Haase    public void setTarget(Object target) {
32570d4ba15b1f0c1133c5aabc86de828b41e482fffChet Haase        if (mTarget != target) {
32670d4ba15b1f0c1133c5aabc86de828b41e482fffChet Haase            mTarget = target;
32770d4ba15b1f0c1133c5aabc86de828b41e482fffChet Haase            if (mTarget  != null && target != null && mTarget.getClass() == target.getClass()) {
32870d4ba15b1f0c1133c5aabc86de828b41e482fffChet Haase                return;
32970d4ba15b1f0c1133c5aabc86de828b41e482fffChet Haase            }
33070d4ba15b1f0c1133c5aabc86de828b41e482fffChet Haase            // New target type should cause re-initialization prior to starting
33170d4ba15b1f0c1133c5aabc86de828b41e482fffChet Haase            mInitialized = false;
33270d4ba15b1f0c1133c5aabc86de828b41e482fffChet Haase        }
333f54a8d7c479485174941c38f151ea7083c658da3Chet Haase    }
334f54a8d7c479485174941c38f151ea7083c658da3Chet Haase
33521cd1389d2ef218b20994b617c57af120841a57fChet Haase    @Override
33621cd1389d2ef218b20994b617c57af120841a57fChet Haase    public void setupStartValues() {
33721cd1389d2ef218b20994b617c57af120841a57fChet Haase        initAnimation();
33821cd1389d2ef218b20994b617c57af120841a57fChet Haase        int numValues = mValues.length;
33921cd1389d2ef218b20994b617c57af120841a57fChet Haase        for (int i = 0; i < numValues; ++i) {
34021cd1389d2ef218b20994b617c57af120841a57fChet Haase            mValues[i].setupStartValue(mTarget);
34121cd1389d2ef218b20994b617c57af120841a57fChet Haase        }
34221cd1389d2ef218b20994b617c57af120841a57fChet Haase    }
34321cd1389d2ef218b20994b617c57af120841a57fChet Haase
34421cd1389d2ef218b20994b617c57af120841a57fChet Haase    @Override
34521cd1389d2ef218b20994b617c57af120841a57fChet Haase    public void setupEndValues() {
34621cd1389d2ef218b20994b617c57af120841a57fChet Haase        initAnimation();
34721cd1389d2ef218b20994b617c57af120841a57fChet Haase        int numValues = mValues.length;
34821cd1389d2ef218b20994b617c57af120841a57fChet Haase        for (int i = 0; i < numValues; ++i) {
34921cd1389d2ef218b20994b617c57af120841a57fChet Haase            mValues[i].setupEndValue(mTarget);
35021cd1389d2ef218b20994b617c57af120841a57fChet Haase        }
35121cd1389d2ef218b20994b617c57af120841a57fChet Haase    }
35221cd1389d2ef218b20994b617c57af120841a57fChet Haase
353f54a8d7c479485174941c38f151ea7083c658da3Chet Haase    /**
35417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * This method is called with the elapsed fraction of the animation during every
35517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * animation frame. This function turns the elapsed fraction into an interpolated fraction
35617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * and then into an animated value (from the evaluator. The function is called mostly during
35717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * animation updates, but it is also called when the <code>end()</code>
35817fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * function is called, to set the final value on the property.
35917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
36017fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * <p>Overrides of this method must call the superclass to perform the calculation
36117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * of the animated value.</p>
36217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     *
36317fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     * @param fraction The elapsed fraction of the animation.
36417fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase     */
36517fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    @Override
36617fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    void animateValue(float fraction) {
36717fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        super.animateValue(fraction);
368602e4d3824bf8b9cb9f817375d195b969712176aChet Haase        int numValues = mValues.length;
369602e4d3824bf8b9cb9f817375d195b969712176aChet Haase        for (int i = 0; i < numValues; ++i) {
370602e4d3824bf8b9cb9f817375d195b969712176aChet Haase            mValues[i].setAnimatedValue(mTarget);
37117fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase        }
37217fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase    }
37349afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase
37449afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase    @Override
375a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase    public ObjectAnimator clone() {
376a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase        final ObjectAnimator anim = (ObjectAnimator) super.clone();
37749afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase        return anim;
37849afa5bc100e5d4c069fea980dd6b09501f56397Chet Haase    }
37917fb4b0d1cfbad1f026fec704c86640f070b4c2fChet Haase}
380