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