1 /* 2 * Copyright (C) 2017 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 package com.android.launcher3.touch; 17 18 /** 19 * Utility methods for overscroll damping and related effect. 20 */ 21 public class OverScroll { 22 23 public static final float OVERSCROLL_DAMP_FACTOR = 0.07f; 24 25 /** 26 * This curve determines how the effect of scrolling over the limits of the page diminishes 27 * as the user pulls further and further from the bounds 28 * 29 * @param f The percentage of how much the user has overscrolled. 30 * @return A transformed percentage based on the influence curve. 31 */ overScrollInfluenceCurve(float f)32 private static float overScrollInfluenceCurve(float f) { 33 f -= 1.0f; 34 return f * f * f + 1.0f; 35 } 36 37 /** 38 * @param amount The original amount overscrolled. 39 * @param max The maximum amount that the View can overscroll. 40 * @return The dampened overscroll amount. 41 */ dampedScroll(float amount, int max)42 public static int dampedScroll(float amount, int max) { 43 if (Float.compare(amount, 0) == 0) return 0; 44 45 float f = amount / max; 46 f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f))); 47 48 // Clamp this factor, f, to -1 < f < 1 49 if (Math.abs(f) >= 1) { 50 f /= Math.abs(f); 51 } 52 53 return Math.round(OVERSCROLL_DAMP_FACTOR * f * max); 54 } 55 } 56