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.quickstep;
17 
18 import android.animation.Animator;
19 import android.animation.AnimatorListenerAdapter;
20 import android.animation.ObjectAnimator;
21 import android.util.FloatProperty;
22 
23 /**
24  * A mutable float which allows animating the value
25  */
26 public class AnimatedFloat {
27 
28     public static final FloatProperty<AnimatedFloat> VALUE =
29             new FloatProperty<AnimatedFloat>("value") {
30                 @Override
31                 public void setValue(AnimatedFloat obj, float v) {
32                     obj.updateValue(v);
33                 }
34 
35                 @Override
36                 public Float get(AnimatedFloat obj) {
37                     return obj.value;
38                 }
39             };
40 
41     private static final Runnable NO_OP = () -> { };
42 
43     private final Runnable mUpdateCallback;
44     private ObjectAnimator mValueAnimator;
45 
46     public float value;
47 
AnimatedFloat()48     public AnimatedFloat() {
49         this(NO_OP);
50     }
51 
AnimatedFloat(Runnable updateCallback)52     public AnimatedFloat(Runnable updateCallback) {
53         mUpdateCallback = updateCallback;
54     }
55 
animateToValue(float start, float end)56     public ObjectAnimator animateToValue(float start, float end) {
57         cancelAnimation();
58         mValueAnimator = ObjectAnimator.ofFloat(this, VALUE, start, end);
59         mValueAnimator.addListener(new AnimatorListenerAdapter() {
60             @Override
61             public void onAnimationEnd(Animator animator) {
62                 if (mValueAnimator == animator) {
63                     mValueAnimator = null;
64                 }
65             }
66         });
67         return mValueAnimator;
68     }
69 
70     /**
71      * Changes the value and calls the callback.
72      * Note that the value can be directly accessed as well to avoid notifying the callback.
73      */
updateValue(float v)74     public void updateValue(float v) {
75         if (Float.compare(v, value) != 0) {
76             value = v;
77             mUpdateCallback.run();
78         }
79     }
80 
cancelAnimation()81     public void cancelAnimation() {
82         if (mValueAnimator != null) {
83             mValueAnimator.cancel();
84         }
85     }
86 
finishAnimation()87     public void finishAnimation() {
88         if (mValueAnimator != null && mValueAnimator.isRunning()) {
89             mValueAnimator.end();
90         }
91     }
92 
getCurrentAnimation()93     public ObjectAnimator getCurrentAnimation() {
94         return mValueAnimator;
95     }
96 }
97