1 /* 2 * Copyright (C) 2009 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.view.animation; 18 19 import android.content.Context; 20 import android.util.AttributeSet; 21 22 import com.android.internal.view.animation.HasNativeInterpolator; 23 import com.android.internal.view.animation.NativeInterpolatorFactory; 24 import com.android.internal.view.animation.NativeInterpolatorFactoryHelper; 25 26 /** 27 * An interpolator where the change bounces at the end. 28 */ 29 @HasNativeInterpolator 30 public class BounceInterpolator extends BaseInterpolator implements NativeInterpolatorFactory { 31 public BounceInterpolator() { 32 } 33 34 @SuppressWarnings({"UnusedDeclaration"}) 35 public BounceInterpolator(Context context, AttributeSet attrs) { 36 } 37 38 private static float bounce(float t) { 39 return t * t * 8.0f; 40 } 41 42 public float getInterpolation(float t) { 43 // _b(t) = t * t * 8 44 // bs(t) = _b(t) for t < 0.3535 45 // bs(t) = _b(t - 0.54719) + 0.7 for t < 0.7408 46 // bs(t) = _b(t - 0.8526) + 0.9 for t < 0.9644 47 // bs(t) = _b(t - 1.0435) + 0.95 for t <= 1.0 48 // b(t) = bs(t * 1.1226) 49 t *= 1.1226f; 50 if (t < 0.3535f) return bounce(t); 51 else if (t < 0.7408f) return bounce(t - 0.54719f) + 0.7f; 52 else if (t < 0.9644f) return bounce(t - 0.8526f) + 0.9f; 53 else return bounce(t - 1.0435f) + 0.95f; 54 } 55 56 /** @hide */ 57 @Override 58 public long createNativeInterpolator() { 59 return NativeInterpolatorFactoryHelper.createBounceInterpolator(); 60 } 61 }