blob: 526e2aef4799dbaf0a065ab7f425dfe0fda4bdbc [file] [log] [blame]
John Reck315c3292014-05-09 19:21:04 -07001/*
2 * Copyright (C) 2014 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 com.android.internal.view.animation;
18
19import android.animation.TimeInterpolator;
20import android.util.TimeUtils;
21import android.view.Choreographer;
22
23/**
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}
John Reck918988c2014-05-19 10:28:35 -070027 *
28 * This implements TimeInterpolator to allow for easier interop with Animators
John Reck315c3292014-05-09 19:21:04 -070029 */
30@HasNativeInterpolator
John Reck918988c2014-05-19 10:28:35 -070031public class FallbackLUTInterpolator implements NativeInterpolatorFactory, TimeInterpolator {
John Reck315c3292014-05-09 19:21:04 -070032
John Reck918988c2014-05-19 10:28:35 -070033 private TimeInterpolator mSourceInterpolator;
John Reck315c3292014-05-09 19:21:04 -070034 private final float mLut[];
35
36 /**
37 * Used to cache the float[] LUT for use across multiple native
38 * interpolator creation
39 */
Alan Viverettead2f8e32014-05-16 13:28:33 -070040 public FallbackLUTInterpolator(TimeInterpolator interpolator, long duration) {
John Reck918988c2014-05-19 10:28:35 -070041 mSourceInterpolator = interpolator;
John Reck315c3292014-05-09 19:21:04 -070042 mLut = createLUT(interpolator, duration);
43 }
44
Alan Viverettead2f8e32014-05-16 13:28:33 -070045 private static float[] createLUT(TimeInterpolator interpolator, long duration) {
John Reck315c3292014-05-09 19:21:04 -070046 long frameIntervalNanos = Choreographer.getInstance().getFrameIntervalNanos();
47 int animIntervalMs = (int) (frameIntervalNanos / TimeUtils.NANOS_PER_MS);
Andreas Gampe65ac8a22015-03-15 14:10:23 -070048 int numAnimFrames = (int) Math.ceil(((double) duration) / animIntervalMs);
John Reck315c3292014-05-09 19:21:04 -070049 float values[] = new float[numAnimFrames];
50 float lastFrame = numAnimFrames - 1;
51 for (int i = 0; i < numAnimFrames; i++) {
52 float inValue = i / lastFrame;
53 values[i] = interpolator.getInterpolation(inValue);
54 }
55 return values;
56 }
57
58 @Override
59 public long createNativeInterpolator() {
60 return NativeInterpolatorFactoryHelper.createLutInterpolator(mLut);
61 }
62
63 /**
64 * Used to create a one-shot float[] LUT & native interpolator
65 */
Alan Viverettead2f8e32014-05-16 13:28:33 -070066 public static long createNativeInterpolator(TimeInterpolator interpolator, long duration) {
John Reck315c3292014-05-09 19:21:04 -070067 float[] lut = createLUT(interpolator, duration);
68 return NativeInterpolatorFactoryHelper.createLutInterpolator(lut);
69 }
John Reck918988c2014-05-19 10:28:35 -070070
71 @Override
72 public float getInterpolation(float input) {
73 return mSourceInterpolator.getInterpolation(input);
74 }
John Reck315c3292014-05-09 19:21:04 -070075}