• 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 
17 package com.android.systemui.statusbar.notification;
18 
19 import android.util.FloatProperty;
20 import android.util.Property;
21 import android.view.View;
22 
23 import com.android.systemui.res.R;
24 
25 import java.util.function.BiConsumer;
26 import java.util.function.Function;
27 
28 /**
29  * An animatable property of a view. Used with {@link PropertyAnimator}
30  */
31 public abstract class AnimatableProperty {
32 
33     public static final AnimatableProperty X = AnimatableProperty.from(View.X,
34             R.id.x_animator_tag, R.id.x_animator_tag_start_value, R.id.x_animator_tag_end_value);
35 
36     public static final AnimatableProperty Y = AnimatableProperty.from(View.Y,
37             R.id.y_animator_tag, R.id.y_animator_tag_start_value, R.id.y_animator_tag_end_value);
38 
39     public static final AnimatableProperty TRANSLATION_X = AnimatableProperty.from(
40             View.TRANSLATION_X, R.id.x_animator_tag, R.id.x_animator_tag_start_value,
41             R.id.x_animator_tag_end_value);
42 
43     public static final AnimatableProperty SCALE_X = AnimatableProperty.from(
44             View.SCALE_X, R.id.scale_x_animator_tag, R.id.scale_x_animator_start_value_tag,
45             R.id.scale_x_animator_end_value_tag);
46 
47     public static final AnimatableProperty SCALE_Y = AnimatableProperty.from(
48             View.SCALE_Y, R.id.scale_y_animator_tag, R.id.scale_y_animator_start_value_tag,
49             R.id.scale_y_animator_end_value_tag);
50 
51     public static final AnimatableProperty ALPHA = AnimatableProperty.from(
52             View.ALPHA, R.id.alpha_animator_tag, R.id.alpha_animator_start_value_tag,
53             R.id.alpha_animator_end_value_tag);
54 
55     /**
56      * Similar to X, however this doesn't allow for any other modifications other than from this
57      * property. When using X, it's possible that the view is laid out during the animation,
58      * which could break the continuity
59      */
60     public static final AnimatableProperty ABSOLUTE_X = AnimatableProperty.from(
61             new FloatProperty<View>("ViewAbsoluteX") {
62                 @Override
63                 public void setValue(View view, float value) {
64                     view.setTag(R.id.absolute_x_current_value, value);
65                     View.X.set(view, value);
66                 }
67 
68                 @Override
69                 public Float get(View view) {
70                     Object tag = view.getTag(R.id.absolute_x_current_value);
71                     if (tag instanceof Float) {
72                         return (Float) tag;
73                     }
74                     return View.X.get(view);
75                 }
76             },
77             R.id.absolute_x_animator_tag,
78             R.id.absolute_x_animator_start_tag,
79             R.id.absolute_x_animator_end_tag);
80 
81     /**
82      * Similar to Y, however this doesn't allow for any other modifications other than from this
83      * property. When using X, it's possible that the view is laid out during the animation,
84      * which could break the continuity
85      */
86     public static final AnimatableProperty ABSOLUTE_Y = AnimatableProperty.from(
87             new FloatProperty<View>("ViewAbsoluteY") {
88                 @Override
89                 public void setValue(View view, float value) {
90                     view.setTag(R.id.absolute_y_current_value, value);
91                     View.Y.set(view, value);
92                 }
93 
94                 @Override
95                 public Float get(View view) {
96                     Object tag = view.getTag(R.id.absolute_y_current_value);
97                     if (tag instanceof Float) {
98                         return (Float) tag;
99                     }
100                     return View.Y.get(view);
101                 }
102             },
103             R.id.absolute_y_animator_tag,
104             R.id.absolute_y_animator_start_tag,
105             R.id.absolute_y_animator_end_tag);
106 
107     public static final AnimatableProperty WIDTH = AnimatableProperty.from(
108             new FloatProperty<View>("ViewWidth") {
109                 @Override
110                 public void setValue(View view, float value) {
111                     view.setTag(R.id.view_width_current_value, value);
112                     view.setRight((int) (view.getLeft() + value));
113                 }
114 
115                 @Override
116                 public Float get(View view) {
117                     Object tag = view.getTag(R.id.view_width_current_value);
118                     if (tag instanceof Float) {
119                         return (Float) tag;
120                     }
121                     return (float) view.getWidth();
122                 }
123             },
124             R.id.view_width_animator_tag,
125             R.id.view_width_animator_start_tag,
126             R.id.view_width_animator_end_tag);
127 
128     public static final AnimatableProperty HEIGHT = AnimatableProperty.from(
129             new FloatProperty<View>("ViewHeight") {
130                 @Override
131                 public void setValue(View view, float value) {
132                     view.setTag(R.id.view_height_current_value, value);
133                     view.setBottom((int) (view.getTop() + value));
134                 }
135 
136                 @Override
137                 public Float get(View view) {
138                     Object tag = view.getTag(R.id.view_height_current_value);
139                     if (tag instanceof Float) {
140                         return (Float) tag;
141                     }
142                     return (float) view.getHeight();
143                 }
144             },
145             R.id.view_height_animator_tag,
146             R.id.view_height_animator_start_tag,
147             R.id.view_height_animator_end_tag);
148 
getAnimationStartTag()149     public abstract int getAnimationStartTag();
150 
getAnimationEndTag()151     public abstract int getAnimationEndTag();
152 
getAnimatorTag()153     public abstract int getAnimatorTag();
154 
getProperty()155     public abstract Property getProperty();
156 
from(String name, BiConsumer<T, Float> setter, Function<T, Float> getter, int animatorTag, int startValueTag, int endValueTag)157     public static <T extends View> AnimatableProperty from(String name, BiConsumer<T, Float> setter,
158             Function<T, Float> getter, int animatorTag, int startValueTag, int endValueTag) {
159         Property<T, Float> property = new FloatProperty<T>(name) {
160 
161             @Override
162             public Float get(T object) {
163                 return getter.apply(object);
164             }
165 
166             @Override
167             public void setValue(T object, float value) {
168                 setter.accept(object, value);
169             }
170         };
171         return new AnimatableProperty() {
172             @Override
173             public int getAnimationStartTag() {
174                 return startValueTag;
175             }
176 
177             @Override
178             public int getAnimationEndTag() {
179                 return endValueTag;
180             }
181 
182             @Override
183             public int getAnimatorTag() {
184                 return animatorTag;
185             }
186 
187             @Override
188             public Property getProperty() {
189                 return property;
190             }
191         };
192     }
193 
194     public static <T extends View> AnimatableProperty from(Property<T, Float> property,
195             int animatorTag, int startValueTag, int endValueTag) {
196         return new AnimatableProperty() {
197             @Override
198             public int getAnimationStartTag() {
199                 return startValueTag;
200             }
201 
202             @Override
203             public int getAnimationEndTag() {
204                 return endValueTag;
205             }
206 
207             @Override
208             public int getAnimatorTag() {
209                 return animatorTag;
210             }
211 
212             @Override
213             public Property getProperty() {
214                 return property;
215             }
216         };
217     }
218 }
219