1315c329544d7c593d1072b071cbb92d9afe74021John Reck/* 2315c329544d7c593d1072b071cbb92d9afe74021John Reck * Copyright (C) 2014 The Android Open Source Project 3315c329544d7c593d1072b071cbb92d9afe74021John Reck * 4315c329544d7c593d1072b071cbb92d9afe74021John Reck * Licensed under the Apache License, Version 2.0 (the "License"); 5315c329544d7c593d1072b071cbb92d9afe74021John Reck * you may not use this file except in compliance with the License. 6315c329544d7c593d1072b071cbb92d9afe74021John Reck * You may obtain a copy of the License at 7315c329544d7c593d1072b071cbb92d9afe74021John Reck * 8315c329544d7c593d1072b071cbb92d9afe74021John Reck * http://www.apache.org/licenses/LICENSE-2.0 9315c329544d7c593d1072b071cbb92d9afe74021John Reck * 10315c329544d7c593d1072b071cbb92d9afe74021John Reck * Unless required by applicable law or agreed to in writing, software 11315c329544d7c593d1072b071cbb92d9afe74021John Reck * distributed under the License is distributed on an "AS IS" BASIS, 12315c329544d7c593d1072b071cbb92d9afe74021John Reck * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13315c329544d7c593d1072b071cbb92d9afe74021John Reck * See the License for the specific language governing permissions and 14315c329544d7c593d1072b071cbb92d9afe74021John Reck * limitations under the License. 15315c329544d7c593d1072b071cbb92d9afe74021John Reck */ 16315c329544d7c593d1072b071cbb92d9afe74021John Reck 17315c329544d7c593d1072b071cbb92d9afe74021John Reckpackage com.android.internal.view.animation; 18315c329544d7c593d1072b071cbb92d9afe74021John Reck 19315c329544d7c593d1072b071cbb92d9afe74021John Reckimport android.animation.TimeInterpolator; 20315c329544d7c593d1072b071cbb92d9afe74021John Reckimport android.util.TimeUtils; 21315c329544d7c593d1072b071cbb92d9afe74021John Reckimport android.view.Choreographer; 22315c329544d7c593d1072b071cbb92d9afe74021John Reck 23315c329544d7c593d1072b071cbb92d9afe74021John Reck/** 24315c329544d7c593d1072b071cbb92d9afe74021John Reck * Interpolator that builds a lookup table to use. This is a fallback for 25315c329544d7c593d1072b071cbb92d9afe74021John Reck * building a native interpolator from a TimeInterpolator that is not marked 26315c329544d7c593d1072b071cbb92d9afe74021John Reck * with {@link HasNativeInterpolator} 27918988c1ce5af002d41c7ac37f3fa490558b0c90John Reck * 28918988c1ce5af002d41c7ac37f3fa490558b0c90John Reck * This implements TimeInterpolator to allow for easier interop with Animators 29315c329544d7c593d1072b071cbb92d9afe74021John Reck */ 30315c329544d7c593d1072b071cbb92d9afe74021John Reck@HasNativeInterpolator 31918988c1ce5af002d41c7ac37f3fa490558b0c90John Reckpublic class FallbackLUTInterpolator implements NativeInterpolatorFactory, TimeInterpolator { 32315c329544d7c593d1072b071cbb92d9afe74021John Reck 33918988c1ce5af002d41c7ac37f3fa490558b0c90John Reck private TimeInterpolator mSourceInterpolator; 34315c329544d7c593d1072b071cbb92d9afe74021John Reck private final float mLut[]; 35315c329544d7c593d1072b071cbb92d9afe74021John Reck 36315c329544d7c593d1072b071cbb92d9afe74021John Reck /** 37315c329544d7c593d1072b071cbb92d9afe74021John Reck * Used to cache the float[] LUT for use across multiple native 38315c329544d7c593d1072b071cbb92d9afe74021John Reck * interpolator creation 39315c329544d7c593d1072b071cbb92d9afe74021John Reck */ 40ad2f8e334f3ef22d3e412b0660a2e1f996f94116Alan Viverette public FallbackLUTInterpolator(TimeInterpolator interpolator, long duration) { 41918988c1ce5af002d41c7ac37f3fa490558b0c90John Reck mSourceInterpolator = interpolator; 42315c329544d7c593d1072b071cbb92d9afe74021John Reck mLut = createLUT(interpolator, duration); 43315c329544d7c593d1072b071cbb92d9afe74021John Reck } 44315c329544d7c593d1072b071cbb92d9afe74021John Reck 45ad2f8e334f3ef22d3e412b0660a2e1f996f94116Alan Viverette private static float[] createLUT(TimeInterpolator interpolator, long duration) { 46315c329544d7c593d1072b071cbb92d9afe74021John Reck long frameIntervalNanos = Choreographer.getInstance().getFrameIntervalNanos(); 47315c329544d7c593d1072b071cbb92d9afe74021John Reck int animIntervalMs = (int) (frameIntervalNanos / TimeUtils.NANOS_PER_MS); 48315c329544d7c593d1072b071cbb92d9afe74021John Reck int numAnimFrames = (int) Math.ceil(duration / animIntervalMs); 49315c329544d7c593d1072b071cbb92d9afe74021John Reck float values[] = new float[numAnimFrames]; 50315c329544d7c593d1072b071cbb92d9afe74021John Reck float lastFrame = numAnimFrames - 1; 51315c329544d7c593d1072b071cbb92d9afe74021John Reck for (int i = 0; i < numAnimFrames; i++) { 52315c329544d7c593d1072b071cbb92d9afe74021John Reck float inValue = i / lastFrame; 53315c329544d7c593d1072b071cbb92d9afe74021John Reck values[i] = interpolator.getInterpolation(inValue); 54315c329544d7c593d1072b071cbb92d9afe74021John Reck } 55315c329544d7c593d1072b071cbb92d9afe74021John Reck return values; 56315c329544d7c593d1072b071cbb92d9afe74021John Reck } 57315c329544d7c593d1072b071cbb92d9afe74021John Reck 58315c329544d7c593d1072b071cbb92d9afe74021John Reck @Override 59315c329544d7c593d1072b071cbb92d9afe74021John Reck public long createNativeInterpolator() { 60315c329544d7c593d1072b071cbb92d9afe74021John Reck return NativeInterpolatorFactoryHelper.createLutInterpolator(mLut); 61315c329544d7c593d1072b071cbb92d9afe74021John Reck } 62315c329544d7c593d1072b071cbb92d9afe74021John Reck 63315c329544d7c593d1072b071cbb92d9afe74021John Reck /** 64315c329544d7c593d1072b071cbb92d9afe74021John Reck * Used to create a one-shot float[] LUT & native interpolator 65315c329544d7c593d1072b071cbb92d9afe74021John Reck */ 66ad2f8e334f3ef22d3e412b0660a2e1f996f94116Alan Viverette public static long createNativeInterpolator(TimeInterpolator interpolator, long duration) { 67315c329544d7c593d1072b071cbb92d9afe74021John Reck float[] lut = createLUT(interpolator, duration); 68315c329544d7c593d1072b071cbb92d9afe74021John Reck return NativeInterpolatorFactoryHelper.createLutInterpolator(lut); 69315c329544d7c593d1072b071cbb92d9afe74021John Reck } 70918988c1ce5af002d41c7ac37f3fa490558b0c90John Reck 71918988c1ce5af002d41c7ac37f3fa490558b0c90John Reck @Override 72918988c1ce5af002d41c7ac37f3fa490558b0c90John Reck public float getInterpolation(float input) { 73918988c1ce5af002d41c7ac37f3fa490558b0c90John Reck return mSourceInterpolator.getInterpolation(input); 74918988c1ce5af002d41c7ac37f3fa490558b0c90John Reck } 75315c329544d7c593d1072b071cbb92d9afe74021John Reck} 76