18b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru/*
28b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru * Copyright (C) 2009 The Android Open Source Project
38b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru *
48b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru * Licensed under the Apache License, Version 2.0 (the "License");
58b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru * you may not use this file except in compliance with the License.
68b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru * You may obtain a copy of the License at
78b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru *
88b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru *      http://www.apache.org/licenses/LICENSE-2.0
98b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru *
108b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru * Unless required by applicable law or agreed to in writing, software
118b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru * distributed under the License is distributed on an "AS IS" BASIS,
128b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru * See the License for the specific language governing permissions and
148b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru * limitations under the License.
158b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru */
168b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
178b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Querupackage android.view.animation;
188b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
198b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queruimport android.content.Context;
208b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queruimport android.util.AttributeSet;
218b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
22c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reckimport com.android.internal.view.animation.HasNativeInterpolator;
23c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reckimport com.android.internal.view.animation.NativeInterpolatorFactory;
24c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reckimport com.android.internal.view.animation.NativeInterpolatorFactoryHelper;
25c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck
268b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru/**
278b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru * An interpolator where the change bounces at the end.
288b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru */
29c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck@HasNativeInterpolator
30d422dc358f0100106dc07d7b903201eb9b043b11Yigit Boyarpublic class BounceInterpolator extends BaseInterpolator implements NativeInterpolatorFactory {
318b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    public BounceInterpolator() {
328b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    }
338b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
348b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    @SuppressWarnings({"UnusedDeclaration"})
358b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    public BounceInterpolator(Context context, AttributeSet attrs) {
368b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    }
378b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
388b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    private static float bounce(float t) {
398b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        return t * t * 8.0f;
408b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    }
418b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
428b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    public float getInterpolation(float t) {
438b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        // _b(t) = t * t * 8
448b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        // bs(t) = _b(t) for t < 0.3535
458b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        // bs(t) = _b(t - 0.54719) + 0.7 for t < 0.7408
468b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        // bs(t) = _b(t - 0.8526) + 0.9 for t < 0.9644
478b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        // bs(t) = _b(t - 1.0435) + 0.95 for t <= 1.0
488b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        // b(t) = bs(t * 1.1226)
498b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        t *= 1.1226f;
508b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        if (t < 0.3535f) return bounce(t);
518b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        else if (t < 0.7408f) return bounce(t - 0.54719f) + 0.7f;
528b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        else if (t < 0.9644f) return bounce(t - 0.8526f) + 0.9f;
538b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        else return bounce(t - 1.0435f) + 0.95f;
548b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    }
55c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck
56c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck    /** @hide */
57c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck    @Override
58c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck    public long createNativeInterpolator() {
59c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck        return NativeInterpolatorFactoryHelper.createBounceInterpolator();
60c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck    }
618b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru}