13dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase/*
23dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase * Copyright (C) 2010 The Android Open Source Project
33dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase *
43dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase * Licensed under the Apache License, Version 2.0 (the "License");
53dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase * you may not use this file except in compliance with the License.
63dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase * You may obtain a copy of the License at
73dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase *
83dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase *      http://www.apache.org/licenses/LICENSE-2.0
93dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase *
103dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase * Unless required by applicable law or agreed to in writing, software
113dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase * distributed under the License is distributed on an "AS IS" BASIS,
123dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase * See the License for the specific language governing permissions and
143dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase * limitations under the License.
153dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase */
163dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase
173dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haasepackage android.animation;
183dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase
193dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase/**
203b69b6f0be85d1f97c1e6824cf986777ba4e5d00Chet Haase * This class holds a time/value pair for an animation. The Keyframe class is used
21a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase * by {@link ValueAnimator} to define the values that the animation target will have over the course
223dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase * of the animation. As the time proceeds from one keyframe to the other, the value of the
233dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase * target object will animate between the value at the previous keyframe and the value at the
24e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase * next keyframe. Each keyframe also holds an optional {@link TimeInterpolator}
253dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase * object, which defines the time interpolation over the intervalue preceding the keyframe.
267c608f25d494c8a0a671e7373efbb47ca635367eChet Haase *
277c608f25d494c8a0a671e7373efbb47ca635367eChet Haase * <p>The Keyframe class itself is abstract. The type-specific factory methods will return
287c608f25d494c8a0a671e7373efbb47ca635367eChet Haase * a subclass of Keyframe specific to the type of value being stored. This is done to improve
297c608f25d494c8a0a671e7373efbb47ca635367eChet Haase * performance when dealing with the most common cases (e.g., <code>float</code> and
307c608f25d494c8a0a671e7373efbb47ca635367eChet Haase * <code>int</code> values). Other types will fall into a more general Keyframe class that
317c608f25d494c8a0a671e7373efbb47ca635367eChet Haase * treats its values as Objects. Unless your animation requires dealing with a custom type
327c608f25d494c8a0a671e7373efbb47ca635367eChet Haase * or a data structure that needs to be animated directly (and evaluated using an implementation
337c608f25d494c8a0a671e7373efbb47ca635367eChet Haase * of {@link TypeEvaluator}), you should stick to using float and int as animations using those
347c608f25d494c8a0a671e7373efbb47ca635367eChet Haase * types have lower runtime overhead than other types.</p>
353dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase */
367c608f25d494c8a0a671e7373efbb47ca635367eChet Haasepublic abstract class Keyframe implements Cloneable {
373dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase    /**
388619f48fb353740f7fd3f6eaa86fe493377e6cadYigit Boyar     * Flag to indicate whether this keyframe has a valid value. This flag is used when an
398619f48fb353740f7fd3f6eaa86fe493377e6cadYigit Boyar     * animation first starts, to populate placeholder keyframes with real values derived
408619f48fb353740f7fd3f6eaa86fe493377e6cadYigit Boyar     * from the target object.
418619f48fb353740f7fd3f6eaa86fe493377e6cadYigit Boyar     */
428619f48fb353740f7fd3f6eaa86fe493377e6cadYigit Boyar    boolean mHasValue;
438619f48fb353740f7fd3f6eaa86fe493377e6cadYigit Boyar
448619f48fb353740f7fd3f6eaa86fe493377e6cadYigit Boyar    /**
458619f48fb353740f7fd3f6eaa86fe493377e6cadYigit Boyar     * Flag to indicate whether the value in the keyframe was read from the target object or not.
468619f48fb353740f7fd3f6eaa86fe493377e6cadYigit Boyar     * If so, its value will be recalculated if target changes.
478619f48fb353740f7fd3f6eaa86fe493377e6cadYigit Boyar     */
488619f48fb353740f7fd3f6eaa86fe493377e6cadYigit Boyar    boolean mValueWasSetOnStart;
498619f48fb353740f7fd3f6eaa86fe493377e6cadYigit Boyar
508619f48fb353740f7fd3f6eaa86fe493377e6cadYigit Boyar
518619f48fb353740f7fd3f6eaa86fe493377e6cadYigit Boyar    /**
523dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * The time at which mValue will hold true.
533dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     */
547c608f25d494c8a0a671e7373efbb47ca635367eChet Haase    float mFraction;
553dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase
563dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase    /**
573dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * The type of the value in this Keyframe. This type is determined at construction time,
583dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * based on the type of the <code>value</code> object passed into the constructor.
593dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     */
607c608f25d494c8a0a671e7373efbb47ca635367eChet Haase    Class mValueType;
613dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase
623dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase    /**
633dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * The optional time interpolator for the interval preceding this keyframe. A null interpolator
643dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * (the default) results in linear interpolation over the interval.
653dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     */
66e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase    private TimeInterpolator mInterpolator = null;
673dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase
688619f48fb353740f7fd3f6eaa86fe493377e6cadYigit Boyar
693dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase
703dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase    /**
713dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * Constructs a Keyframe object with the given time and value. The time defines the
723dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * time, as a proportion of an overall animation's duration, at which the value will hold true
733dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * for the animation. The value for the animation between keyframes will be calculated as
743dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * an interpolation between the values at those keyframes.
753dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     *
763dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * @param fraction The time, expressed as a value between 0 and 1, representing the fraction
773dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * of time elapsed of the overall animation duration.
783dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * @param value The value that the object will animate to as the animation time approaches
793dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * the time in this keyframe, and the the value animated from as the time passes the time in
803dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * this keyframe.
813dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     */
827c608f25d494c8a0a671e7373efbb47ca635367eChet Haase    public static Keyframe ofInt(float fraction, int value) {
837c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        return new IntKeyframe(fraction, value);
8421cd1389d2ef218b20994b617c57af120841a57fChet Haase    }
8521cd1389d2ef218b20994b617c57af120841a57fChet Haase
8621cd1389d2ef218b20994b617c57af120841a57fChet Haase    /**
877c608f25d494c8a0a671e7373efbb47ca635367eChet Haase     * Constructs a Keyframe object with the given time. The value at this time will be derived
887c608f25d494c8a0a671e7373efbb47ca635367eChet Haase     * from the target object when the animation first starts (note that this implies that keyframes
897c608f25d494c8a0a671e7373efbb47ca635367eChet Haase     * with no initial value must be used as part of an {@link ObjectAnimator}).
907c608f25d494c8a0a671e7373efbb47ca635367eChet Haase     * The time defines the
9121cd1389d2ef218b20994b617c57af120841a57fChet Haase     * time, as a proportion of an overall animation's duration, at which the value will hold true
9221cd1389d2ef218b20994b617c57af120841a57fChet Haase     * for the animation. The value for the animation between keyframes will be calculated as
9321cd1389d2ef218b20994b617c57af120841a57fChet Haase     * an interpolation between the values at those keyframes.
9421cd1389d2ef218b20994b617c57af120841a57fChet Haase     *
9521cd1389d2ef218b20994b617c57af120841a57fChet Haase     * @param fraction The time, expressed as a value between 0 and 1, representing the fraction
9621cd1389d2ef218b20994b617c57af120841a57fChet Haase     * of time elapsed of the overall animation duration.
9721cd1389d2ef218b20994b617c57af120841a57fChet Haase     */
987c608f25d494c8a0a671e7373efbb47ca635367eChet Haase    public static Keyframe ofInt(float fraction) {
997c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        return new IntKeyframe(fraction);
10021cd1389d2ef218b20994b617c57af120841a57fChet Haase    }
10121cd1389d2ef218b20994b617c57af120841a57fChet Haase
10221cd1389d2ef218b20994b617c57af120841a57fChet Haase    /**
1037c608f25d494c8a0a671e7373efbb47ca635367eChet Haase     * Constructs a Keyframe object with the given time and value. The time defines the
10421cd1389d2ef218b20994b617c57af120841a57fChet Haase     * time, as a proportion of an overall animation's duration, at which the value will hold true
10521cd1389d2ef218b20994b617c57af120841a57fChet Haase     * for the animation. The value for the animation between keyframes will be calculated as
10621cd1389d2ef218b20994b617c57af120841a57fChet Haase     * an interpolation between the values at those keyframes.
10721cd1389d2ef218b20994b617c57af120841a57fChet Haase     *
10821cd1389d2ef218b20994b617c57af120841a57fChet Haase     * @param fraction The time, expressed as a value between 0 and 1, representing the fraction
10921cd1389d2ef218b20994b617c57af120841a57fChet Haase     * of time elapsed of the overall animation duration.
11021cd1389d2ef218b20994b617c57af120841a57fChet Haase     * @param value The value that the object will animate to as the animation time approaches
11121cd1389d2ef218b20994b617c57af120841a57fChet Haase     * the time in this keyframe, and the the value animated from as the time passes the time in
11221cd1389d2ef218b20994b617c57af120841a57fChet Haase     * this keyframe.
11321cd1389d2ef218b20994b617c57af120841a57fChet Haase     */
1147c608f25d494c8a0a671e7373efbb47ca635367eChet Haase    public static Keyframe ofFloat(float fraction, float value) {
1157c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        return new FloatKeyframe(fraction, value);
11621cd1389d2ef218b20994b617c57af120841a57fChet Haase    }
11721cd1389d2ef218b20994b617c57af120841a57fChet Haase
11821cd1389d2ef218b20994b617c57af120841a57fChet Haase    /**
1197c608f25d494c8a0a671e7373efbb47ca635367eChet Haase     * Constructs a Keyframe object with the given time. The value at this time will be derived
1207c608f25d494c8a0a671e7373efbb47ca635367eChet Haase     * from the target object when the animation first starts (note that this implies that keyframes
1217c608f25d494c8a0a671e7373efbb47ca635367eChet Haase     * with no initial value must be used as part of an {@link ObjectAnimator}).
1227c608f25d494c8a0a671e7373efbb47ca635367eChet Haase     * The time defines the
12321cd1389d2ef218b20994b617c57af120841a57fChet Haase     * time, as a proportion of an overall animation's duration, at which the value will hold true
12421cd1389d2ef218b20994b617c57af120841a57fChet Haase     * for the animation. The value for the animation between keyframes will be calculated as
12521cd1389d2ef218b20994b617c57af120841a57fChet Haase     * an interpolation between the values at those keyframes.
12621cd1389d2ef218b20994b617c57af120841a57fChet Haase     *
12721cd1389d2ef218b20994b617c57af120841a57fChet Haase     * @param fraction The time, expressed as a value between 0 and 1, representing the fraction
12821cd1389d2ef218b20994b617c57af120841a57fChet Haase     * of time elapsed of the overall animation duration.
12921cd1389d2ef218b20994b617c57af120841a57fChet Haase     */
1307c608f25d494c8a0a671e7373efbb47ca635367eChet Haase    public static Keyframe ofFloat(float fraction) {
1317c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        return new FloatKeyframe(fraction);
1323dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase    }
1333dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase
1343dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase    /**
1357c608f25d494c8a0a671e7373efbb47ca635367eChet Haase     * Constructs a Keyframe object with the given time and value. The time defines the
1363dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * time, as a proportion of an overall animation's duration, at which the value will hold true
1373dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * for the animation. The value for the animation between keyframes will be calculated as
1383dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * an interpolation between the values at those keyframes.
1393dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     *
1403dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * @param fraction The time, expressed as a value between 0 and 1, representing the fraction
1413dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * of time elapsed of the overall animation duration.
1423dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * @param value The value that the object will animate to as the animation time approaches
1433dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * the time in this keyframe, and the the value animated from as the time passes the time in
1443dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * this keyframe.
1453dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     */
1467c608f25d494c8a0a671e7373efbb47ca635367eChet Haase    public static Keyframe ofObject(float fraction, Object value) {
1477c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        return new ObjectKeyframe(fraction, value);
1483dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase    }
1493dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase
1503dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase    /**
1517c608f25d494c8a0a671e7373efbb47ca635367eChet Haase     * Constructs a Keyframe object with the given time. The value at this time will be derived
1527c608f25d494c8a0a671e7373efbb47ca635367eChet Haase     * from the target object when the animation first starts (note that this implies that keyframes
1537c608f25d494c8a0a671e7373efbb47ca635367eChet Haase     * with no initial value must be used as part of an {@link ObjectAnimator}).
1547c608f25d494c8a0a671e7373efbb47ca635367eChet Haase     * The time defines the
1553dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * time, as a proportion of an overall animation's duration, at which the value will hold true
1563dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * for the animation. The value for the animation between keyframes will be calculated as
1573dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * an interpolation between the values at those keyframes.
1583dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     *
1593dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * @param fraction The time, expressed as a value between 0 and 1, representing the fraction
1603dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * of time elapsed of the overall animation duration.
1613dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     */
1627c608f25d494c8a0a671e7373efbb47ca635367eChet Haase    public static Keyframe ofObject(float fraction) {
1637c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        return new ObjectKeyframe(fraction, null);
1643dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase    }
1653dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase
1663dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase    /**
1677c608f25d494c8a0a671e7373efbb47ca635367eChet Haase     * Indicates whether this keyframe has a valid value. This method is called internally when
1687c608f25d494c8a0a671e7373efbb47ca635367eChet Haase     * an {@link ObjectAnimator} first starts; keyframes without values are assigned values at
1697c608f25d494c8a0a671e7373efbb47ca635367eChet Haase     * that time by deriving the value for the property from the target object.
1703dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     *
1717c608f25d494c8a0a671e7373efbb47ca635367eChet Haase     * @return boolean Whether this object has a value assigned.
1723dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     */
1737c608f25d494c8a0a671e7373efbb47ca635367eChet Haase    public boolean hasValue() {
1747c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        return mHasValue;
1753dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase    }
1763dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase
1773dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase    /**
1788619f48fb353740f7fd3f6eaa86fe493377e6cadYigit Boyar     * If the Keyframe's value was acquired from the target object, this flag should be set so that,
1798619f48fb353740f7fd3f6eaa86fe493377e6cadYigit Boyar     * if target changes, value will be reset.
1808619f48fb353740f7fd3f6eaa86fe493377e6cadYigit Boyar     *
1818619f48fb353740f7fd3f6eaa86fe493377e6cadYigit Boyar     * @return boolean Whether this Keyframe's value was retieved from the target object or not.
1828619f48fb353740f7fd3f6eaa86fe493377e6cadYigit Boyar     */
1838619f48fb353740f7fd3f6eaa86fe493377e6cadYigit Boyar    boolean valueWasSetOnStart() {
1848619f48fb353740f7fd3f6eaa86fe493377e6cadYigit Boyar        return mValueWasSetOnStart;
1858619f48fb353740f7fd3f6eaa86fe493377e6cadYigit Boyar    }
1868619f48fb353740f7fd3f6eaa86fe493377e6cadYigit Boyar
1878619f48fb353740f7fd3f6eaa86fe493377e6cadYigit Boyar    void setValueWasSetOnStart(boolean valueWasSetOnStart) {
1888619f48fb353740f7fd3f6eaa86fe493377e6cadYigit Boyar        mValueWasSetOnStart = valueWasSetOnStart;
1898619f48fb353740f7fd3f6eaa86fe493377e6cadYigit Boyar    }
1908619f48fb353740f7fd3f6eaa86fe493377e6cadYigit Boyar
1918619f48fb353740f7fd3f6eaa86fe493377e6cadYigit Boyar    /**
1923dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * Gets the value for this Keyframe.
1933dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     *
1943dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * @return The value for this Keyframe.
1953dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     */
1967c608f25d494c8a0a671e7373efbb47ca635367eChet Haase    public abstract Object getValue();
1973dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase
1983dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase    /**
199fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     * Sets the value for this Keyframe.
200fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     *
2013b69b6f0be85d1f97c1e6824cf986777ba4e5d00Chet Haase     * @param value value for this Keyframe.
202fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     */
2037c608f25d494c8a0a671e7373efbb47ca635367eChet Haase    public abstract void setValue(Object value);
204fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase
205fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase    /**
2063dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * Gets the time for this keyframe, as a fraction of the overall animation duration.
2073dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     *
2083dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * @return The time associated with this keyframe, as a fraction of the overall animation
2093dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * duration. This should be a value between 0 and 1.
2103dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     */
2113dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase    public float getFraction() {
2123dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase        return mFraction;
2133dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase    }
2143dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase
2153dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase    /**
216fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     * Sets the time for this keyframe, as a fraction of the overall animation duration.
217fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     *
2183b69b6f0be85d1f97c1e6824cf986777ba4e5d00Chet Haase     * @param fraction time associated with this keyframe, as a fraction of the overall animation
219fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     * duration. This should be a value between 0 and 1.
220fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase     */
221fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase    public void setFraction(float fraction) {
222fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase        mFraction = fraction;
223fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase    }
224fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase
225fe591563f8529305bd52e1f0640e83b9a93d562fChet Haase    /**
2263dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * Gets the optional interpolator for this Keyframe. A value of <code>null</code> indicates
2273dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * that there is no interpolation, which is the same as linear interpolation.
2283dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     *
2293dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * @return The optional interpolator for this Keyframe.
2303dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     */
231e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase    public TimeInterpolator getInterpolator() {
2323dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase        return mInterpolator;
2333dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase    }
2343dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase
2353dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase    /**
2363dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * Sets the optional interpolator for this Keyframe. A value of <code>null</code> indicates
2373dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * that there is no interpolation, which is the same as linear interpolation.
2383dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     *
2393dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * @return The optional interpolator for this Keyframe.
2403dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     */
241e0ee2e9f3102c3c14c873a75a7b04e49787e0fb9Chet Haase    public void setInterpolator(TimeInterpolator interpolator) {
2423dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase        mInterpolator = interpolator;
2433dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase    }
2443dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase
2453dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase    /**
246a18a86b43e40e3c15dcca0ae0148d641be9b25feChet Haase     * Gets the type of keyframe. This information is used by ValueAnimator to determine the type of
2473b69b6f0be85d1f97c1e6824cf986777ba4e5d00Chet Haase     * {@link TypeEvaluator} to use when calculating values between keyframes. The type is based
2483b69b6f0be85d1f97c1e6824cf986777ba4e5d00Chet Haase     * on the type of Keyframe created.
2493dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     *
2503dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     * @return The type of the value stored in the Keyframe.
2513dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase     */
2523dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase    public Class getType() {
2533dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase        return mValueType;
2543dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase    }
25521cd1389d2ef218b20994b617c57af120841a57fChet Haase
25621cd1389d2ef218b20994b617c57af120841a57fChet Haase    @Override
2577c608f25d494c8a0a671e7373efbb47ca635367eChet Haase    public abstract Keyframe clone();
2587c608f25d494c8a0a671e7373efbb47ca635367eChet Haase
2597c608f25d494c8a0a671e7373efbb47ca635367eChet Haase    /**
2607c608f25d494c8a0a671e7373efbb47ca635367eChet Haase     * This internal subclass is used for all types which are not int or float.
2617c608f25d494c8a0a671e7373efbb47ca635367eChet Haase     */
2627c608f25d494c8a0a671e7373efbb47ca635367eChet Haase    static class ObjectKeyframe extends Keyframe {
2637c608f25d494c8a0a671e7373efbb47ca635367eChet Haase
2647c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        /**
2657c608f25d494c8a0a671e7373efbb47ca635367eChet Haase         * The value of the animation at the time mFraction.
2667c608f25d494c8a0a671e7373efbb47ca635367eChet Haase         */
2677c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        Object mValue;
2687c608f25d494c8a0a671e7373efbb47ca635367eChet Haase
2697c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        ObjectKeyframe(float fraction, Object value) {
2707c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            mFraction = fraction;
2717c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            mValue = value;
2727c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            mHasValue = (value != null);
2737c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            mValueType = mHasValue ? value.getClass() : Object.class;
2747c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        }
2757c608f25d494c8a0a671e7373efbb47ca635367eChet Haase
2767c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        public Object getValue() {
2777c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            return mValue;
2787c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        }
2797c608f25d494c8a0a671e7373efbb47ca635367eChet Haase
2807c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        public void setValue(Object value) {
2817c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            mValue = value;
2827c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            mHasValue = (value != null);
2837c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        }
2847c608f25d494c8a0a671e7373efbb47ca635367eChet Haase
2857c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        @Override
2867c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        public ObjectKeyframe clone() {
2878619f48fb353740f7fd3f6eaa86fe493377e6cadYigit Boyar            ObjectKeyframe kfClone = new ObjectKeyframe(getFraction(), hasValue() ? mValue : null);
2888619f48fb353740f7fd3f6eaa86fe493377e6cadYigit Boyar            kfClone.mValueWasSetOnStart = mValueWasSetOnStart;
2897c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            kfClone.setInterpolator(getInterpolator());
2907c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            return kfClone;
2917c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        }
2927c608f25d494c8a0a671e7373efbb47ca635367eChet Haase    }
2937c608f25d494c8a0a671e7373efbb47ca635367eChet Haase
2947c608f25d494c8a0a671e7373efbb47ca635367eChet Haase    /**
2957c608f25d494c8a0a671e7373efbb47ca635367eChet Haase     * Internal subclass used when the keyframe value is of type int.
2967c608f25d494c8a0a671e7373efbb47ca635367eChet Haase     */
2977c608f25d494c8a0a671e7373efbb47ca635367eChet Haase    static class IntKeyframe extends Keyframe {
2987c608f25d494c8a0a671e7373efbb47ca635367eChet Haase
2997c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        /**
3007c608f25d494c8a0a671e7373efbb47ca635367eChet Haase         * The value of the animation at the time mFraction.
3017c608f25d494c8a0a671e7373efbb47ca635367eChet Haase         */
3027c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        int mValue;
3037c608f25d494c8a0a671e7373efbb47ca635367eChet Haase
3047c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        IntKeyframe(float fraction, int value) {
3057c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            mFraction = fraction;
3067c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            mValue = value;
3077c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            mValueType = int.class;
3087c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            mHasValue = true;
3097c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        }
3107c608f25d494c8a0a671e7373efbb47ca635367eChet Haase
3117c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        IntKeyframe(float fraction) {
3127c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            mFraction = fraction;
3137c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            mValueType = int.class;
3147c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        }
3157c608f25d494c8a0a671e7373efbb47ca635367eChet Haase
3167c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        public int getIntValue() {
3177c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            return mValue;
3187c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        }
3197c608f25d494c8a0a671e7373efbb47ca635367eChet Haase
3207c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        public Object getValue() {
3217c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            return mValue;
3227c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        }
3237c608f25d494c8a0a671e7373efbb47ca635367eChet Haase
3247c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        public void setValue(Object value) {
3257c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            if (value != null && value.getClass() == Integer.class) {
3267c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                mValue = ((Integer)value).intValue();
3277c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                mHasValue = true;
3287c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            }
3297c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        }
3307c608f25d494c8a0a671e7373efbb47ca635367eChet Haase
3317c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        @Override
3327c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        public IntKeyframe clone() {
33300177e483dd5b703545e742e6e2f8346dd4f624fChet Haase            IntKeyframe kfClone = mHasValue ?
33400177e483dd5b703545e742e6e2f8346dd4f624fChet Haase                    new IntKeyframe(getFraction(), mValue) :
33500177e483dd5b703545e742e6e2f8346dd4f624fChet Haase                    new IntKeyframe(getFraction());
3367c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            kfClone.setInterpolator(getInterpolator());
3378619f48fb353740f7fd3f6eaa86fe493377e6cadYigit Boyar            kfClone.mValueWasSetOnStart = mValueWasSetOnStart;
3387c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            return kfClone;
3397c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        }
3407c608f25d494c8a0a671e7373efbb47ca635367eChet Haase    }
3417c608f25d494c8a0a671e7373efbb47ca635367eChet Haase
3427c608f25d494c8a0a671e7373efbb47ca635367eChet Haase    /**
3437c608f25d494c8a0a671e7373efbb47ca635367eChet Haase     * Internal subclass used when the keyframe value is of type float.
3447c608f25d494c8a0a671e7373efbb47ca635367eChet Haase     */
3457c608f25d494c8a0a671e7373efbb47ca635367eChet Haase    static class FloatKeyframe extends Keyframe {
3467c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        /**
3477c608f25d494c8a0a671e7373efbb47ca635367eChet Haase         * The value of the animation at the time mFraction.
3487c608f25d494c8a0a671e7373efbb47ca635367eChet Haase         */
3497c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        float mValue;
3507c608f25d494c8a0a671e7373efbb47ca635367eChet Haase
3517c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        FloatKeyframe(float fraction, float value) {
3527c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            mFraction = fraction;
3537c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            mValue = value;
3547c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            mValueType = float.class;
3557c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            mHasValue = true;
3567c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        }
3577c608f25d494c8a0a671e7373efbb47ca635367eChet Haase
3587c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        FloatKeyframe(float fraction) {
3597c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            mFraction = fraction;
3607c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            mValueType = float.class;
3617c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        }
3627c608f25d494c8a0a671e7373efbb47ca635367eChet Haase
3637c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        public float getFloatValue() {
3647c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            return mValue;
3657c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        }
3667c608f25d494c8a0a671e7373efbb47ca635367eChet Haase
3677c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        public Object getValue() {
3687c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            return mValue;
3697c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        }
3707c608f25d494c8a0a671e7373efbb47ca635367eChet Haase
3717c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        public void setValue(Object value) {
3727c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            if (value != null && value.getClass() == Float.class) {
3737c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                mValue = ((Float)value).floatValue();
3747c608f25d494c8a0a671e7373efbb47ca635367eChet Haase                mHasValue = true;
3757c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            }
3767c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        }
3777c608f25d494c8a0a671e7373efbb47ca635367eChet Haase
3787c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        @Override
3797c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        public FloatKeyframe clone() {
38000177e483dd5b703545e742e6e2f8346dd4f624fChet Haase            FloatKeyframe kfClone = mHasValue ?
38100177e483dd5b703545e742e6e2f8346dd4f624fChet Haase                    new FloatKeyframe(getFraction(), mValue) :
38200177e483dd5b703545e742e6e2f8346dd4f624fChet Haase                    new FloatKeyframe(getFraction());
3837c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            kfClone.setInterpolator(getInterpolator());
3848619f48fb353740f7fd3f6eaa86fe493377e6cadYigit Boyar            kfClone.mValueWasSetOnStart = mValueWasSetOnStart;
3857c608f25d494c8a0a671e7373efbb47ca635367eChet Haase            return kfClone;
3867c608f25d494c8a0a671e7373efbb47ca635367eChet Haase        }
38721cd1389d2ef218b20994b617c57af120841a57fChet Haase    }
3883dd207a6dbd5d9244dc7fe213d5caa3cddaff0dbChet Haase}
389