Chet Haase | 7c608f2 | 2010-10-22 17:54:04 -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 | |
| 19 | import android.animation.Keyframe.FloatKeyframe; |
| 20 | |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 21 | import java.util.List; |
Chet Haase | 7c608f2 | 2010-10-22 17:54:04 -0700 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * This class holds a collection of FloatKeyframe objects and is called by ValueAnimator to calculate |
| 25 | * values between those keyframes for a given animation. The class internal to the animation |
| 26 | * package because it is an implementation detail of how Keyframes are stored and used. |
| 27 | * |
Chet Haase | b2ab04f | 2011-01-16 11:03:22 -0800 | [diff] [blame] | 28 | * <p>This type-specific subclass of KeyframeSet, along with the other type-specific subclass for |
| 29 | * int, exists to speed up the getValue() method when there is no custom |
Chet Haase | 7c608f2 | 2010-10-22 17:54:04 -0700 | [diff] [blame] | 30 | * TypeEvaluator set for the animation, so that values can be calculated without autoboxing to the |
| 31 | * Object equivalents of these primitive types.</p> |
| 32 | */ |
George Mount | 984011f | 2014-08-21 14:28:01 -0700 | [diff] [blame] | 33 | class FloatKeyframeSet extends KeyframeSet implements Keyframes.FloatKeyframes { |
Chet Haase | 7c608f2 | 2010-10-22 17:54:04 -0700 | [diff] [blame] | 34 | private float firstValue; |
| 35 | private float lastValue; |
| 36 | private float deltaValue; |
| 37 | private boolean firstTime = true; |
| 38 | |
| 39 | public FloatKeyframeSet(FloatKeyframe... keyframes) { |
| 40 | super(keyframes); |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | public Object getValue(float fraction) { |
| 45 | return getFloatValue(fraction); |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public FloatKeyframeSet clone() { |
Yigit Boyar | d422dc3 | 2014-09-25 12:23:35 -0700 | [diff] [blame] | 50 | final List<Keyframe> keyframes = mKeyframes; |
| 51 | final int numKeyframes = mKeyframes.size(); |
Chet Haase | 7c608f2 | 2010-10-22 17:54:04 -0700 | [diff] [blame] | 52 | FloatKeyframe[] newKeyframes = new FloatKeyframe[numKeyframes]; |
| 53 | for (int i = 0; i < numKeyframes; ++i) { |
| 54 | newKeyframes[i] = (FloatKeyframe) keyframes.get(i).clone(); |
| 55 | } |
| 56 | FloatKeyframeSet newSet = new FloatKeyframeSet(newKeyframes); |
| 57 | return newSet; |
| 58 | } |
| 59 | |
Yigit Boyar | 8619f48 | 2014-07-15 17:28:07 -0700 | [diff] [blame] | 60 | @Override |
George Mount | 984011f | 2014-08-21 14:28:01 -0700 | [diff] [blame] | 61 | public void invalidateCache() { |
Yigit Boyar | 8619f48 | 2014-07-15 17:28:07 -0700 | [diff] [blame] | 62 | firstTime = true; |
| 63 | } |
| 64 | |
George Mount | 984011f | 2014-08-21 14:28:01 -0700 | [diff] [blame] | 65 | @Override |
Chet Haase | 7c608f2 | 2010-10-22 17:54:04 -0700 | [diff] [blame] | 66 | public float getFloatValue(float fraction) { |
| 67 | if (mNumKeyframes == 2) { |
| 68 | if (firstTime) { |
| 69 | firstTime = false; |
| 70 | firstValue = ((FloatKeyframe) mKeyframes.get(0)).getFloatValue(); |
| 71 | lastValue = ((FloatKeyframe) mKeyframes.get(1)).getFloatValue(); |
| 72 | deltaValue = lastValue - firstValue; |
| 73 | } |
| 74 | if (mInterpolator != null) { |
| 75 | fraction = mInterpolator.getInterpolation(fraction); |
| 76 | } |
| 77 | if (mEvaluator == null) { |
| 78 | return firstValue + fraction * deltaValue; |
| 79 | } else { |
| 80 | return ((Number)mEvaluator.evaluate(fraction, firstValue, lastValue)).floatValue(); |
| 81 | } |
| 82 | } |
| 83 | if (fraction <= 0f) { |
| 84 | final FloatKeyframe prevKeyframe = (FloatKeyframe) mKeyframes.get(0); |
| 85 | final FloatKeyframe nextKeyframe = (FloatKeyframe) mKeyframes.get(1); |
| 86 | float prevValue = prevKeyframe.getFloatValue(); |
| 87 | float nextValue = nextKeyframe.getFloatValue(); |
| 88 | float prevFraction = prevKeyframe.getFraction(); |
| 89 | float nextFraction = nextKeyframe.getFraction(); |
| 90 | final TimeInterpolator interpolator = nextKeyframe.getInterpolator(); |
| 91 | if (interpolator != null) { |
| 92 | fraction = interpolator.getInterpolation(fraction); |
| 93 | } |
| 94 | float intervalFraction = (fraction - prevFraction) / (nextFraction - prevFraction); |
| 95 | return mEvaluator == null ? |
Chet Haase | 750e12e | 2011-02-11 15:21:35 -0800 | [diff] [blame] | 96 | prevValue + intervalFraction * (nextValue - prevValue) : |
Chet Haase | 7c608f2 | 2010-10-22 17:54:04 -0700 | [diff] [blame] | 97 | ((Number)mEvaluator.evaluate(intervalFraction, prevValue, nextValue)). |
| 98 | floatValue(); |
| 99 | } else if (fraction >= 1f) { |
| 100 | final FloatKeyframe prevKeyframe = (FloatKeyframe) mKeyframes.get(mNumKeyframes - 2); |
| 101 | final FloatKeyframe nextKeyframe = (FloatKeyframe) mKeyframes.get(mNumKeyframes - 1); |
| 102 | float prevValue = prevKeyframe.getFloatValue(); |
| 103 | float nextValue = nextKeyframe.getFloatValue(); |
| 104 | float prevFraction = prevKeyframe.getFraction(); |
| 105 | float nextFraction = nextKeyframe.getFraction(); |
| 106 | final TimeInterpolator interpolator = nextKeyframe.getInterpolator(); |
| 107 | if (interpolator != null) { |
| 108 | fraction = interpolator.getInterpolation(fraction); |
| 109 | } |
| 110 | float intervalFraction = (fraction - prevFraction) / (nextFraction - prevFraction); |
| 111 | return mEvaluator == null ? |
Chet Haase | 750e12e | 2011-02-11 15:21:35 -0800 | [diff] [blame] | 112 | prevValue + intervalFraction * (nextValue - prevValue) : |
Chet Haase | 7c608f2 | 2010-10-22 17:54:04 -0700 | [diff] [blame] | 113 | ((Number)mEvaluator.evaluate(intervalFraction, prevValue, nextValue)). |
| 114 | floatValue(); |
| 115 | } |
| 116 | FloatKeyframe prevKeyframe = (FloatKeyframe) mKeyframes.get(0); |
| 117 | for (int i = 1; i < mNumKeyframes; ++i) { |
| 118 | FloatKeyframe nextKeyframe = (FloatKeyframe) mKeyframes.get(i); |
| 119 | if (fraction < nextKeyframe.getFraction()) { |
| 120 | final TimeInterpolator interpolator = nextKeyframe.getInterpolator(); |
Chet Haase | 7c608f2 | 2010-10-22 17:54:04 -0700 | [diff] [blame] | 121 | float intervalFraction = (fraction - prevKeyframe.getFraction()) / |
| 122 | (nextKeyframe.getFraction() - prevKeyframe.getFraction()); |
| 123 | float prevValue = prevKeyframe.getFloatValue(); |
| 124 | float nextValue = nextKeyframe.getFloatValue(); |
Doris Liu | 6df9905 | 2015-03-30 16:03:59 -0700 | [diff] [blame] | 125 | // Apply interpolator on the proportional duration. |
| 126 | if (interpolator != null) { |
| 127 | intervalFraction = interpolator.getInterpolation(intervalFraction); |
| 128 | } |
Chet Haase | 7c608f2 | 2010-10-22 17:54:04 -0700 | [diff] [blame] | 129 | return mEvaluator == null ? |
Chet Haase | 750e12e | 2011-02-11 15:21:35 -0800 | [diff] [blame] | 130 | prevValue + intervalFraction * (nextValue - prevValue) : |
Chet Haase | 7c608f2 | 2010-10-22 17:54:04 -0700 | [diff] [blame] | 131 | ((Number)mEvaluator.evaluate(intervalFraction, prevValue, nextValue)). |
| 132 | floatValue(); |
| 133 | } |
| 134 | prevKeyframe = nextKeyframe; |
| 135 | } |
| 136 | // shouldn't get here |
| 137 | return ((Number)mKeyframes.get(mNumKeyframes - 1).getValue()).floatValue(); |
| 138 | } |
| 139 | |
George Mount | 984011f | 2014-08-21 14:28:01 -0700 | [diff] [blame] | 140 | @Override |
| 141 | public Class getType() { |
| 142 | return Float.class; |
| 143 | } |
Chet Haase | 7c608f2 | 2010-10-22 17:54:04 -0700 | [diff] [blame] | 144 | } |
| 145 | |