• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 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.graphics;
18 
19 import android.os.SystemClock;
20 
21 @android.ravenwood.annotation.RavenwoodKeepWholeClass
22 @android.ravenwood.annotation.RavenwoodClassLoadHook(
23         android.ravenwood.annotation.RavenwoodClassLoadHook.LIBANDROID_LOADING_HOOK)
24 public class Interpolator {
25 
Interpolator(int valueCount)26     public Interpolator(int valueCount) {
27         mValueCount = valueCount;
28         mFrameCount = 2;
29         native_instance = nativeConstructor(valueCount, 2);
30     }
31 
Interpolator(int valueCount, int frameCount)32     public Interpolator(int valueCount, int frameCount) {
33         mValueCount = valueCount;
34         mFrameCount = frameCount;
35         native_instance = nativeConstructor(valueCount, frameCount);
36     }
37 
38     /**
39      * Reset the Interpolator to have the specified number of values and an
40      * implicit keyFrame count of 2 (just a start and end). After this call the
41      * values for each keyFrame must be assigned using setKeyFrame().
42      */
reset(int valueCount)43     public void reset(int valueCount) {
44         reset(valueCount, 2);
45     }
46 
47     /**
48      * Reset the Interpolator to have the specified number of values and
49      * keyFrames. After this call the values for each keyFrame must be assigned
50      * using setKeyFrame().
51      */
reset(int valueCount, int frameCount)52     public void reset(int valueCount, int frameCount) {
53         mValueCount = valueCount;
54         mFrameCount = frameCount;
55         nativeReset(native_instance, valueCount, frameCount);
56     }
57 
getKeyFrameCount()58     public final int getKeyFrameCount() {
59         return mFrameCount;
60     }
61 
getValueCount()62     public final int getValueCount() {
63         return mValueCount;
64     }
65 
66     /**
67      * Assign the keyFrame (specified by index) a time value and an array of key
68      * values (with an implicitly blend array of [0, 0, 1, 1] giving linear
69      * transition to the next set of key values).
70      *
71      * @param index The index of the key frame to assign
72      * @param msec The time (in mililiseconds) for this key frame. Based on the
73      *        SystemClock.uptimeMillis() clock
74      * @param values Array of values associated with theis key frame
75      */
setKeyFrame(int index, int msec, float[] values)76     public void setKeyFrame(int index, int msec, float[] values) {
77         setKeyFrame(index, msec, values, null);
78     }
79 
80     /**
81      * Assign the keyFrame (specified by index) a time value and an array of key
82      * values and blend array.
83      *
84      * @param index The index of the key frame to assign
85      * @param msec The time (in mililiseconds) for this key frame. Based on the
86      *        SystemClock.uptimeMillis() clock
87      * @param values Array of values associated with theis key frame
88      * @param blend (may be null) Optional array of 4 blend values
89      */
setKeyFrame(int index, int msec, float[] values, float[] blend)90     public void setKeyFrame(int index, int msec, float[] values, float[] blend) {
91         if (index < 0 || index >= mFrameCount) {
92             throw new IndexOutOfBoundsException();
93         }
94         if (values.length < mValueCount) {
95             throw new ArrayStoreException();
96         }
97         if (blend != null && blend.length < 4) {
98             throw new ArrayStoreException();
99         }
100         nativeSetKeyFrame(native_instance, index, msec, values, blend);
101     }
102 
103     /**
104      * Set a repeat count (which may be fractional) for the interpolator, and
105      * whether the interpolator should mirror its repeats. The default settings
106      * are repeatCount = 1, and mirror = false.
107      */
setRepeatMirror(float repeatCount, boolean mirror)108     public void setRepeatMirror(float repeatCount, boolean mirror) {
109         if (repeatCount >= 0) {
110             nativeSetRepeatMirror(native_instance, repeatCount, mirror);
111         }
112     }
113 
114     public enum Result {
115         NORMAL,
116         FREEZE_START,
117         FREEZE_END
118     }
119 
120     /**
121      * Calls timeToValues(msec, values) with the msec set to now (by calling
122      * (int)SystemClock.uptimeMillis().)
123      */
timeToValues(float[] values)124     public Result timeToValues(float[] values) {
125         return timeToValues((int)SystemClock.uptimeMillis(), values);
126     }
127 
128     /**
129      * Given a millisecond time value (msec), return the interpolated values and
130      * return whether the specified time was within the range of key times
131      * (NORMAL), was before the first key time (FREEZE_START) or after the last
132      * key time (FREEZE_END). In any event, computed values are always returned.
133      *
134      * @param msec The time (in milliseconds) used to sample into the
135      *        Interpolator. Based on the SystemClock.uptimeMillis() clock
136      * @param values Where to write the computed values (may be NULL).
137      * @return how the values were computed (even if values == null)
138      */
timeToValues(int msec, float[] values)139     public Result timeToValues(int msec, float[] values) {
140         if (values != null && values.length < mValueCount) {
141             throw new ArrayStoreException();
142         }
143         switch (nativeTimeToValues(native_instance, msec, values)) {
144             case 0: return Result.NORMAL;
145             case 1: return Result.FREEZE_START;
146             default: return Result.FREEZE_END;
147         }
148     }
149 
150     @Override
finalize()151     protected void finalize() throws Throwable {
152         nativeDestructor(native_instance);
153         native_instance = 0;  // Other finalizers can still call us.
154     }
155 
156     private int mValueCount;
157     private int mFrameCount;
158     private long native_instance;
159 
nativeConstructor(int valueCount, int frameCount)160     private static native long nativeConstructor(int valueCount, int frameCount);
nativeDestructor(long native_instance)161     private static native void nativeDestructor(long native_instance);
nativeReset(long native_instance, int valueCount, int frameCount)162     private static native void nativeReset(long native_instance, int valueCount, int frameCount);
nativeSetKeyFrame(long native_instance, int index, int msec, float[] values, float[] blend)163     private static native void nativeSetKeyFrame(long native_instance, int index, int msec, float[] values, float[] blend);
nativeSetRepeatMirror(long native_instance, float repeatCount, boolean mirror)164     private static native void nativeSetRepeatMirror(long native_instance, float repeatCount, boolean mirror);
nativeTimeToValues(long native_instance, int msec, float[] values)165     private static native int  nativeTimeToValues(long native_instance, int msec, float[] values);
166 }
167 
168