2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.internal.view.animation;
19 import android.animation.TimeInterpolator;
20 import android.util.TimeUtils;
21 import android.view.Choreographer;
24 * Interpolator that builds a lookup table to use. This is a fallback for
25 * building a native interpolator from a TimeInterpolator that is not marked
26 * with {@link HasNativeInterpolator}
28 * This implements TimeInterpolator to allow for easier interop with Animators
30 @HasNativeInterpolator
31 public class FallbackLUTInterpolator implements NativeInterpolatorFactory, TimeInterpolator {
33 // If the duration of an animation is more than 300 frames, we cap the sample size to 300.
34 private static final int MAX_SAMPLE_POINTS = 300;
35 private TimeInterpolator mSourceInterpolator;
36 private final float mLut[];
39 * Used to cache the float[] LUT for use across multiple native
40 * interpolator creation
42 public FallbackLUTInterpolator(TimeInterpolator interpolator, long duration) {
43 mSourceInterpolator = interpolator;
44 mLut = createLUT(interpolator, duration);
47 private static float[] createLUT(TimeInterpolator interpolator, long duration) {
48 long frameIntervalNanos = Choreographer.getInstance().getFrameIntervalNanos();
49 int animIntervalMs = (int) (frameIntervalNanos / TimeUtils.NANOS_PER_MS);
50 // We need 2 frame values as the minimal.
51 int numAnimFrames = Math.max(2, (int) Math.ceil(((double) duration) / animIntervalMs));
52 numAnimFrames = Math.min(numAnimFrames, MAX_SAMPLE_POINTS);
53 float values[] = new float[numAnimFrames];
54 float lastFrame = numAnimFrames - 1;
55 for (int i = 0; i < numAnimFrames; i++) {
56 float inValue = i / lastFrame;
57 values[i] = interpolator.getInterpolation(inValue);
63 public long createNativeInterpolator() {
64 return NativeInterpolatorFactoryHelper.createLutInterpolator(mLut);
68 * Used to create a one-shot float[] LUT & native interpolator
70 public static long createNativeInterpolator(TimeInterpolator interpolator, long duration) {
71 float[] lut = createLUT(interpolator, duration);
72 return NativeInterpolatorFactoryHelper.createLutInterpolator(lut);
76 public float getInterpolation(float input) {
77 return mSourceInterpolator.getInterpolation(input);