1 /* 2 * Copyright (C) 2023 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.internal.widget.remotecompose.core.operations.utilities.easing; 17 18 /** Provide a specific bouncing easing function */ 19 public class BounceCurve extends Easing { 20 private static final float N1 = 7.5625f; 21 private static final float D1 = 2.75f; 22 BounceCurve(int type)23 BounceCurve(int type) { 24 mType = type; 25 } 26 27 @Override get(float x)28 public float get(float x) { 29 float t = x; 30 if (t < 0) { 31 return 0f; 32 } 33 if (t < 1 / D1) { 34 return 1 / (1 + 1 / D1) * (N1 * t * t + t); 35 } else if (t < 2 / D1) { 36 t -= 1.5f / D1; 37 return N1 * t * t + 0.75f; 38 } else if (t < 2.5 / D1) { 39 t -= 2.25f / D1; 40 return N1 * t * t + 0.9375f; 41 } else if (t <= 1) { 42 t -= 2.625f / D1; 43 return N1 * t * t + 0.984375f; 44 } 45 return 1f; 46 } 47 48 @Override getDiff(float x)49 public float getDiff(float x) { 50 if (x < 0) { 51 return 0f; 52 } 53 if (x < 1 / D1) { 54 return 2 * N1 * x / (1 + 1 / D1) + 1 / (1 + 1 / D1); 55 } else if (x < 2 / D1) { 56 return 2 * N1 * (x - 1.5f / D1); 57 } else if (x < 2.5 / D1) { 58 return 2 * N1 * (x - 2.25f / D1); 59 } else if (x <= 1) { 60 return 2 * N1 * (x - 2.625f / D1); 61 } 62 return 0f; 63 } 64 } 65