1 /*
2  * Copyright (C) 2010 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 
17 package com.replica.replicaisland;
18 
19 public final class Lerp {
20 
lerp(float start, float target, float duration, float timeSinceStart)21     public static float lerp(float start, float target, float duration, float timeSinceStart)
22     {
23         float value = start;
24         if (timeSinceStart > 0.0f && timeSinceStart < duration)
25         {
26             final float range = target - start;
27             final float percent = timeSinceStart / duration;
28             value = start + (range * percent);
29         }
30         else if (timeSinceStart >= duration)
31         {
32             value = target;
33         }
34         return value;
35     }
36 
ease(float start, float target, float duration, float timeSinceStart)37     public static float ease(float start, float target, float duration, float timeSinceStart)
38     {
39         float value = start;
40         if (timeSinceStart > 0.0f && timeSinceStart < duration)
41         {
42             final float range = target - start;
43             final float percent = timeSinceStart / (duration / 2.0f);
44             if (percent < 1.0f)
45             {
46                 value = start + ((range / 2.0f) * percent * percent * percent);
47             }
48             else
49             {
50                 final float shiftedPercent = percent - 2.0f;
51                 value = start + ((range / 2.0f) *
52                         ((shiftedPercent * shiftedPercent * shiftedPercent) + 2.0f));
53             }
54         }
55         else if (timeSinceStart >= duration)
56         {
57             value = target;
58         }
59         return value;
60     }
61 }
62