OvershootInterpolator.java revision c8ac775659fd252ce2cc9a61837c170ff70f0a1a
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.content.res.TypedArray;
218b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queruimport android.util.AttributeSet;
228b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
23c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reckimport com.android.internal.view.animation.HasNativeInterpolator;
24c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reckimport com.android.internal.view.animation.NativeInterpolatorFactory;
25c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reckimport com.android.internal.view.animation.NativeInterpolatorFactoryHelper;
26c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck
278b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru/**
288b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru * An interpolator where the change flings forward and overshoots the last value
298b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru * then comes back.
308b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru */
31c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck@HasNativeInterpolator
32c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reckpublic class OvershootInterpolator implements Interpolator, NativeInterpolatorFactory {
338b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    private final float mTension;
348b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
358b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    public OvershootInterpolator() {
368b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        mTension = 2.0f;
378b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    }
388b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
398b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    /**
408b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru     * @param tension Amount of overshoot. When tension equals 0.0f, there is
418b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru     *                no overshoot and the interpolator becomes a simple
428b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru     *                deceleration interpolator.
438b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru     */
448b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    public OvershootInterpolator(float tension) {
458b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        mTension = tension;
468b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    }
478b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
488b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    public OvershootInterpolator(Context context, AttributeSet attrs) {
498b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        TypedArray a = context.obtainStyledAttributes(attrs,
508b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru                com.android.internal.R.styleable.OvershootInterpolator);
518b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
528b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        mTension =
538b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru                a.getFloat(com.android.internal.R.styleable.OvershootInterpolator_tension, 2.0f);
548b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
558b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        a.recycle();
568b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    }
578b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru
588b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    public float getInterpolation(float t) {
598b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        // _o(t) = t * t * ((tension + 1) * t + tension)
608b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        // o(t) = _o(t - 1) + 1
618b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        t -= 1.0f;
628b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru        return t * t * ((mTension + 1) * t + mTension) + 1.0f;
638b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru    }
64c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck
65c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck    /** @hide */
66c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck    @Override
67c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck    public long createNativeInterpolator() {
68c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck        return NativeInterpolatorFactoryHelper.createOvershootInterpolator(mTension);
69c8ac775659fd252ce2cc9a61837c170ff70f0a1aJohn Reck    }
708b0662878eae69ab62e859b07165f086ea65cad5Jean-Baptiste Queru}
71