blob: 73f9af19a4d59e140f299ab500d8d06a4ee0937c [file] [log] [blame]
Chet Haase7c608f22010-10-22 17:54:04 -07001/*
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
17package android.animation;
18
19import android.animation.Keyframe.IntKeyframe;
20
Yigit Boyard422dc32014-09-25 12:23:35 -070021import java.util.List;
Chet Haase7c608f22010-10-22 17:54:04 -070022
23/**
24 * This class holds a collection of IntKeyframe 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 Haaseb2ab04f2011-01-16 11:03:22 -080028 * <p>This type-specific subclass of KeyframeSet, along with the other type-specific subclass for
29 * float, exists to speed up the getValue() method when there is no custom
Chet Haase7c608f22010-10-22 17:54:04 -070030 * 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 Mount984011f2014-08-21 14:28:01 -070033class IntKeyframeSet extends KeyframeSet implements Keyframes.IntKeyframes {
Chet Haase7c608f22010-10-22 17:54:04 -070034 private int firstValue;
35 private int lastValue;
36 private int deltaValue;
37 private boolean firstTime = true;
38
39 public IntKeyframeSet(IntKeyframe... keyframes) {
40 super(keyframes);
41 }
42
43 @Override
44 public Object getValue(float fraction) {
45 return getIntValue(fraction);
46 }
47
48 @Override
49 public IntKeyframeSet clone() {
Yigit Boyard422dc32014-09-25 12:23:35 -070050 List<Keyframe> keyframes = mKeyframes;
Chet Haase7c608f22010-10-22 17:54:04 -070051 int numKeyframes = mKeyframes.size();
52 IntKeyframe[] newKeyframes = new IntKeyframe[numKeyframes];
53 for (int i = 0; i < numKeyframes; ++i) {
54 newKeyframes[i] = (IntKeyframe) keyframes.get(i).clone();
55 }
56 IntKeyframeSet newSet = new IntKeyframeSet(newKeyframes);
57 return newSet;
58 }
59
Yigit Boyar8619f482014-07-15 17:28:07 -070060 @Override
George Mount984011f2014-08-21 14:28:01 -070061 public void invalidateCache() {
Yigit Boyar8619f482014-07-15 17:28:07 -070062 firstTime = true;
63 }
64
George Mount984011f2014-08-21 14:28:01 -070065 @Override
Chet Haase7c608f22010-10-22 17:54:04 -070066 public int getIntValue(float fraction) {
67 if (mNumKeyframes == 2) {
68 if (firstTime) {
69 firstTime = false;
70 firstValue = ((IntKeyframe) mKeyframes.get(0)).getIntValue();
71 lastValue = ((IntKeyframe) mKeyframes.get(1)).getIntValue();
72 deltaValue = lastValue - firstValue;
73 }
74 if (mInterpolator != null) {
75 fraction = mInterpolator.getInterpolation(fraction);
76 }
77 if (mEvaluator == null) {
78 return firstValue + (int)(fraction * deltaValue);
79 } else {
80 return ((Number)mEvaluator.evaluate(fraction, firstValue, lastValue)).intValue();
81 }
82 }
83 if (fraction <= 0f) {
84 final IntKeyframe prevKeyframe = (IntKeyframe) mKeyframes.get(0);
85 final IntKeyframe nextKeyframe = (IntKeyframe) mKeyframes.get(1);
86 int prevValue = prevKeyframe.getIntValue();
87 int nextValue = nextKeyframe.getIntValue();
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 Haase750e12e2011-02-11 15:21:35 -080096 prevValue + (int)(intervalFraction * (nextValue - prevValue)) :
Chet Haase7c608f22010-10-22 17:54:04 -070097 ((Number)mEvaluator.evaluate(intervalFraction, prevValue, nextValue)).
98 intValue();
99 } else if (fraction >= 1f) {
100 final IntKeyframe prevKeyframe = (IntKeyframe) mKeyframes.get(mNumKeyframes - 2);
101 final IntKeyframe nextKeyframe = (IntKeyframe) mKeyframes.get(mNumKeyframes - 1);
102 int prevValue = prevKeyframe.getIntValue();
103 int nextValue = nextKeyframe.getIntValue();
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 Haase750e12e2011-02-11 15:21:35 -0800112 prevValue + (int)(intervalFraction * (nextValue - prevValue)) :
Chet Haase7c608f22010-10-22 17:54:04 -0700113 ((Number)mEvaluator.evaluate(intervalFraction, prevValue, nextValue)).intValue();
114 }
115 IntKeyframe prevKeyframe = (IntKeyframe) mKeyframes.get(0);
116 for (int i = 1; i < mNumKeyframes; ++i) {
117 IntKeyframe nextKeyframe = (IntKeyframe) mKeyframes.get(i);
118 if (fraction < nextKeyframe.getFraction()) {
119 final TimeInterpolator interpolator = nextKeyframe.getInterpolator();
Chet Haase7c608f22010-10-22 17:54:04 -0700120 float intervalFraction = (fraction - prevKeyframe.getFraction()) /
121 (nextKeyframe.getFraction() - prevKeyframe.getFraction());
122 int prevValue = prevKeyframe.getIntValue();
123 int nextValue = nextKeyframe.getIntValue();
Doris Liu6df99052015-03-30 16:03:59 -0700124 // Apply interpolator on the proportional duration.
125 if (interpolator != null) {
126 intervalFraction = interpolator.getInterpolation(intervalFraction);
127 }
Chet Haase7c608f22010-10-22 17:54:04 -0700128 return mEvaluator == null ?
Chet Haase750e12e2011-02-11 15:21:35 -0800129 prevValue + (int)(intervalFraction * (nextValue - prevValue)) :
Chet Haase7c608f22010-10-22 17:54:04 -0700130 ((Number)mEvaluator.evaluate(intervalFraction, prevValue, nextValue)).
131 intValue();
132 }
133 prevKeyframe = nextKeyframe;
134 }
135 // shouldn't get here
136 return ((Number)mKeyframes.get(mNumKeyframes - 1).getValue()).intValue();
137 }
138
George Mount984011f2014-08-21 14:28:01 -0700139 @Override
140 public Class getType() {
141 return Integer.class;
142 }
Chet Haase7c608f22010-10-22 17:54:04 -0700143}
144