John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.internal.view.animation; |
| 18 | |
| 19 | import android.animation.TimeInterpolator; |
| 20 | import android.util.TimeUtils; |
| 21 | import 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 Reck | 918988c | 2014-05-19 10:28:35 -0700 | [diff] [blame] | 27 | * |
| 28 | * This implements TimeInterpolator to allow for easier interop with Animators |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 29 | */ |
| 30 | @HasNativeInterpolator |
John Reck | 918988c | 2014-05-19 10:28:35 -0700 | [diff] [blame] | 31 | public class FallbackLUTInterpolator implements NativeInterpolatorFactory, TimeInterpolator { |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 32 | |
John Reck | 918988c | 2014-05-19 10:28:35 -0700 | [diff] [blame] | 33 | private TimeInterpolator mSourceInterpolator; |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 34 | private final float mLut[]; |
| 35 | |
| 36 | /** |
| 37 | * Used to cache the float[] LUT for use across multiple native |
| 38 | * interpolator creation |
| 39 | */ |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 40 | public FallbackLUTInterpolator(TimeInterpolator interpolator, long duration) { |
John Reck | 918988c | 2014-05-19 10:28:35 -0700 | [diff] [blame] | 41 | mSourceInterpolator = interpolator; |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 42 | mLut = createLUT(interpolator, duration); |
| 43 | } |
| 44 | |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 45 | private static float[] createLUT(TimeInterpolator interpolator, long duration) { |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 46 | long frameIntervalNanos = Choreographer.getInstance().getFrameIntervalNanos(); |
| 47 | int animIntervalMs = (int) (frameIntervalNanos / TimeUtils.NANOS_PER_MS); |
Teng-Hui Zhu | a5dcc6c | 2016-03-30 11:34:55 -0700 | [diff] [blame] | 48 | // We need 2 frame values as the minimal. |
| 49 | int numAnimFrames = Math.max(2, (int) Math.ceil(((double) duration) / animIntervalMs)); |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 50 | float values[] = new float[numAnimFrames]; |
| 51 | float lastFrame = numAnimFrames - 1; |
| 52 | for (int i = 0; i < numAnimFrames; i++) { |
| 53 | float inValue = i / lastFrame; |
| 54 | values[i] = interpolator.getInterpolation(inValue); |
| 55 | } |
| 56 | return values; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public long createNativeInterpolator() { |
| 61 | return NativeInterpolatorFactoryHelper.createLutInterpolator(mLut); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Used to create a one-shot float[] LUT & native interpolator |
| 66 | */ |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 67 | public static long createNativeInterpolator(TimeInterpolator interpolator, long duration) { |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 68 | float[] lut = createLUT(interpolator, duration); |
| 69 | return NativeInterpolatorFactoryHelper.createLutInterpolator(lut); |
| 70 | } |
John Reck | 918988c | 2014-05-19 10:28:35 -0700 | [diff] [blame] | 71 | |
| 72 | @Override |
| 73 | public float getInterpolation(float input) { |
| 74 | return mSourceInterpolator.getInterpolation(input); |
| 75 | } |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 76 | } |