176086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren/*
276086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren * Copyright (C) 2007 The Android Open Source Project
376086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren *
476086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren * Licensed under the Apache License, Version 2.0 (the "License");
576086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren * you may not use this file except in compliance with the License.
676086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren * You may obtain a copy of the License at
776086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren *
876086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren *      http://www.apache.org/licenses/LICENSE-2.0
976086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren *
1076086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren * Unless required by applicable law or agreed to in writing, software
1176086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren * distributed under the License is distributed on an "AS IS" BASIS,
1276086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1376086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren * See the License for the specific language governing permissions and
1476086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren * limitations under the License.
1576086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren */
1676086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren
1776086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wrenpackage com.android.dreams.phototable;
1876086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren
1976086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wrenimport android.view.animation.Interpolator;
2076086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wrenimport android.view.animation.DecelerateInterpolator;
2176086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wrenimport android.view.animation.LinearInterpolator;
2276086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wrenimport android.util.Log;
2376086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren
2476086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren/**
2576086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren * An interpolator where the rate of change starts out quickly and
2676086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren * and then decelerates.
2776086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren *
2876086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren */
2976086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wrenpublic class SoftLandingInterpolator implements Interpolator {
3076086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren    private final LinearInterpolator fly;
3176086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren    private final DecelerateInterpolator slide;
3276086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren    private final float mI;
3376086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren    private final float mO;
3476086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren    private final float lowerRange;
3576086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren    private final float upperRange;
3676086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren    private final float bottom;
3776086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren    private final float top;
3876086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren
3976086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren    public SoftLandingInterpolator(float i, float o) {
4076086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren        fly = new LinearInterpolator();
4176086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren        slide = new  DecelerateInterpolator();
4276086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren        mI = i;
4376086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren        mO = o;
4476086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren        final float epsilon = Math.min(mI / 2f, (1f - mI) / 2f);
4576086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren        bottom = mI - epsilon;
4676086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren        top = mI + epsilon;
4776086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren        lowerRange = top;
4876086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren        upperRange = 1f - bottom;
4976086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren    }
5076086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren
5176086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren    public float getInterpolation(float input) {
5276086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren        final float f = fly.getInterpolation(input / upperRange) * mO;
5376086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren        final float s = slide.getInterpolation((input - bottom) / upperRange) * (1f - mO) + mO;
5476086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren
5576086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren        float value;
5676086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren        if (input < bottom) {
5776086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren            value = f;
5876086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren        } else if (input < top) {
5976086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren            final float alpha = (input - bottom) / (top - bottom);
6076086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren            value = (1f - alpha) * f + alpha * s;
6176086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren        } else {
6276086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren            value = s;
6376086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren        }
6476086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren
6576086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren        return value;
6676086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren    }
6776086400f8e68b810bf1b3db0dc4f7133c8644aeChris Wren}
68