• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 FloatProperty<AnimatedFloat> VALUE = new FloatProperty<AnimatedFloat>("value") {
29         @Override
30         public void setValue(AnimatedFloat obj, float v) {
31             obj.updateValue(v);
32         }
33 
34         @Override
35         public Float get(AnimatedFloat obj) {
36             return obj.value;
37         }
38     };
39 
40     private final Runnable mUpdateCallback;
41     private ObjectAnimator mValueAnimator;
42 
43     public float value;
44 
AnimatedFloat(Runnable updateCallback)45     public AnimatedFloat(Runnable updateCallback) {
46         mUpdateCallback = updateCallback;
47     }
48 
animateToValue(float start, float end)49     public ObjectAnimator animateToValue(float start, float end) {
50         cancelAnimation();
51         mValueAnimator = ObjectAnimator.ofFloat(this, VALUE, start, end);
52         mValueAnimator.addListener(new AnimatorListenerAdapter() {
53             @Override
54             public void onAnimationEnd(Animator animator) {
55                 if (mValueAnimator == animator) {
56                     mValueAnimator = null;
57                 }
58             }
59         });
60         return mValueAnimator;
61     }
62 
63     /**
64      * Changes the value and calls the callback.
65      * Note that the value can be directly accessed as well to avoid notifying the callback.
66      */
updateValue(float v)67     public void updateValue(float v) {
68         if (Float.compare(v, value) != 0) {
69             value = v;
70             mUpdateCallback.run();
71         }
72     }
73 
cancelAnimation()74     public void cancelAnimation() {
75         if (mValueAnimator != null) {
76             mValueAnimator.cancel();
77         }
78     }
79 
finishAnimation()80     public void finishAnimation() {
81         if (mValueAnimator != null && mValueAnimator.isRunning()) {
82             mValueAnimator.end();
83         }
84     }
85 
getCurrentAnimation()86     public ObjectAnimator getCurrentAnimation() {
87         return mValueAnimator;
88     }
89 }
90