blob: c20f32305575d24dff53601102df3d458b8feecb [file] [log] [blame]
Michael Jurka2ecf9952012-06-18 12:52:28 -07001/*
2 * Copyright (C) 2012 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
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Michael Jurka2ecf9952012-06-18 12:52:28 -070018
Sunny Goyalff9e7d62020-10-21 15:42:05 -070019import android.animation.Animator;
20import android.animation.Animator.AnimatorListener;
21import android.animation.AnimatorListenerAdapter;
Zak Cohen4d35ac32021-04-23 16:28:12 -070022import android.graphics.Color;
23import android.graphics.drawable.ColorDrawable;
Jon Miranda2d89ea82017-05-04 11:47:53 -070024import android.graphics.drawable.Drawable;
Sunny Goyalb80941b2019-06-19 21:30:40 -070025import android.util.FloatProperty;
Sunny Goyaldbcc63e2020-03-06 15:35:55 -080026import android.util.IntProperty;
Michael Jurkaf1ad6082013-03-13 12:55:46 +010027import android.view.View;
Sunny Goyal55bdeed2018-09-18 16:17:22 -070028import android.view.ViewGroup.LayoutParams;
Pat Manninge68ae412023-01-29 01:25:27 +000029import android.widget.ImageView;
Luca Zuccarini8009c1d2022-05-24 16:17:19 +000030import android.widget.TextView;
Michael Jurka2ecf9952012-06-18 12:52:28 -070031
Nicolo' Mazzucato12131a42022-02-07 20:36:35 +010032import com.android.launcher3.util.MultiScalePropertyFactory;
33
Michael Jurka2ecf9952012-06-18 12:52:28 -070034public class LauncherAnimUtils {
Sunny Goyalaeb16432017-10-16 11:46:41 -070035 /**
36 * Durations for various state animations. These are not defined in resources to allow
37 * easier access from static classes and enums
38 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -070039 public static final int SPRING_LOADED_EXIT_DELAY = 500;
Sunny Goyalaeb16432017-10-16 11:46:41 -070040
Sunny Goyalff9e7d62020-10-21 15:42:05 -070041 // Progress after which the transition is assumed to be a success
42 public static final float SUCCESS_TRANSITION_PROGRESS = 0.5f;
Alex Chauc83ea5f2022-03-31 14:47:56 +010043 public static final float TABLET_BOTTOM_SHEET_SUCCESS_TRANSITION_PROGRESS = 0.3f;
Tony Wickham0f640b62018-05-09 16:00:14 -070044
Sunny Goyaldbcc63e2020-03-06 15:35:55 -080045 public static final IntProperty<Drawable> DRAWABLE_ALPHA =
46 new IntProperty<Drawable>("drawableAlpha") {
Jon Miranda2d89ea82017-05-04 11:47:53 -070047 @Override
48 public Integer get(Drawable drawable) {
49 return drawable.getAlpha();
50 }
51
52 @Override
Sunny Goyaldbcc63e2020-03-06 15:35:55 -080053 public void setValue(Drawable drawable, int alpha) {
Jon Miranda2d89ea82017-05-04 11:47:53 -070054 drawable.setAlpha(alpha);
55 }
56 };
Sunny Goyalaeb16432017-10-16 11:46:41 -070057
Sunny Goyalb80941b2019-06-19 21:30:40 -070058 public static final FloatProperty<View> SCALE_PROPERTY =
59 new FloatProperty<View>("scale") {
Sunny Goyalaeb16432017-10-16 11:46:41 -070060 @Override
61 public Float get(View view) {
62 return view.getScaleX();
63 }
64
65 @Override
Sunny Goyalb80941b2019-06-19 21:30:40 -070066 public void setValue(View view, float scale) {
Sunny Goyalaeb16432017-10-16 11:46:41 -070067 view.setScaleX(scale);
68 view.setScaleY(scale);
69 }
70 };
Sunny Goyalea529082017-10-31 13:33:03 -070071
Nicolo' Mazzucato12131a42022-02-07 20:36:35 +010072 /**
Nicolo' Mazzucato5765d422022-02-21 18:11:23 +010073 * Property to set the scale of workspace. The value is based on a combination
Nicolo' Mazzucato12131a42022-02-07 20:36:35 +010074 * of all the ones set, to have a smooth experience even in the case of overlapping scaling
75 * animation.
76 */
Shikha Malhotraf78da1b2022-04-11 10:23:18 +000077 public static final MultiScalePropertyFactory<Workspace<?>> WORKSPACE_SCALE_PROPERTY_FACTORY =
78 new MultiScalePropertyFactory<Workspace<?>>("workspace_scale_property");
Nicolo' Mazzucato5765d422022-02-21 18:11:23 +010079
80 /** Property to set the scale of hotseat. */
81 public static final MultiScalePropertyFactory<Hotseat> HOTSEAT_SCALE_PROPERTY_FACTORY =
82 new MultiScalePropertyFactory<Hotseat>("hotseat_scale_property");
Nicolo' Mazzucato12131a42022-02-07 20:36:35 +010083
84 public static final int SCALE_INDEX_UNFOLD_ANIMATION = 1;
Alex Chau0c4e11b2022-07-08 18:41:36 +010085 public static final int SCALE_INDEX_WORKSPACE_STATE = 2;
86 public static final int SCALE_INDEX_REVEAL_ANIM = 3;
87 public static final int SCALE_INDEX_WIDGET_TRANSITION = 4;
Nicolo' Mazzucato12131a42022-02-07 20:36:35 +010088
Tony Wickham0f640b62018-05-09 16:00:14 -070089 /** Increase the duration if we prevented the fling, as we are going against a high velocity. */
90 public static int blockedFlingDurationFactor(float velocity) {
91 return (int) Utilities.boundToRange(Math.abs(velocity) / 2, 2f, 6f);
92 }
Sunny Goyal55bdeed2018-09-18 16:17:22 -070093
Sunny Goyaldbcc63e2020-03-06 15:35:55 -080094 public static final IntProperty<LayoutParams> LAYOUT_WIDTH =
95 new IntProperty<LayoutParams>("width") {
Sunny Goyal55bdeed2018-09-18 16:17:22 -070096 @Override
97 public Integer get(LayoutParams lp) {
98 return lp.width;
99 }
100
101 @Override
Sunny Goyaldbcc63e2020-03-06 15:35:55 -0800102 public void setValue(LayoutParams lp, int width) {
Sunny Goyal55bdeed2018-09-18 16:17:22 -0700103 lp.width = width;
104 }
105 };
106
Sunny Goyaldbcc63e2020-03-06 15:35:55 -0800107 public static final IntProperty<LayoutParams> LAYOUT_HEIGHT =
108 new IntProperty<LayoutParams>("height") {
Sunny Goyal55bdeed2018-09-18 16:17:22 -0700109 @Override
110 public Integer get(LayoutParams lp) {
111 return lp.height;
112 }
113
114 @Override
Sunny Goyaldbcc63e2020-03-06 15:35:55 -0800115 public void setValue(LayoutParams lp, int height) {
Sunny Goyal55bdeed2018-09-18 16:17:22 -0700116 lp.height = height;
117 }
118 };
Jon Miranda86f6c442019-01-29 11:14:38 -0800119
Luca Zuccarini8009c1d2022-05-24 16:17:19 +0000120 public static final IntProperty<TextView> TEXT_COLOR =
121 new IntProperty<TextView>("textColor") {
122 @Override
123 public Integer get(TextView view) {
124 return view.getTextColors().getDefaultColor();
125 }
126
127 @Override
128 public void setValue(TextView view, int color) {
129 view.setTextColor(color);
130 }
131 };
132
133 public static final IntProperty<TextView> HINT_TEXT_COLOR =
134 new IntProperty<TextView>("hintTextColor") {
135 @Override
136 public Integer get(TextView view) {
137 return view.getHintTextColors().getDefaultColor();
138 }
139
140 @Override
141 public void setValue(TextView view, int color) {
142 view.setHintTextColor(color);
143 }
144 };
145
Sunny Goyalb80941b2019-06-19 21:30:40 -0700146 public static final FloatProperty<View> VIEW_TRANSLATE_X =
147 View.TRANSLATION_X instanceof FloatProperty ? (FloatProperty) View.TRANSLATION_X
148 : new FloatProperty<View>("translateX") {
149 @Override
150 public void setValue(View view, float v) {
151 view.setTranslationX(v);
152 }
Jon Miranda86f6c442019-01-29 11:14:38 -0800153
Sunny Goyalb80941b2019-06-19 21:30:40 -0700154 @Override
155 public Float get(View view) {
156 return view.getTranslationX();
157 }
158 };
Jon Miranda86f6c442019-01-29 11:14:38 -0800159
Sunny Goyalb80941b2019-06-19 21:30:40 -0700160 public static final FloatProperty<View> VIEW_TRANSLATE_Y =
161 View.TRANSLATION_Y instanceof FloatProperty ? (FloatProperty) View.TRANSLATION_Y
162 : new FloatProperty<View>("translateY") {
163 @Override
164 public void setValue(View view, float v) {
165 view.setTranslationY(v);
166 }
Jon Miranda86f6c442019-01-29 11:14:38 -0800167
Sunny Goyalb80941b2019-06-19 21:30:40 -0700168 @Override
169 public Float get(View view) {
170 return view.getTranslationY();
171 }
172 };
Sunny Goyaldbcc63e2020-03-06 15:35:55 -0800173
174 public static final FloatProperty<View> VIEW_ALPHA =
175 View.ALPHA instanceof FloatProperty ? (FloatProperty) View.ALPHA
176 : new FloatProperty<View>("alpha") {
177 @Override
178 public void setValue(View view, float v) {
179 view.setAlpha(v);
180 }
181
182 @Override
183 public Float get(View view) {
184 return view.getAlpha();
185 }
186 };
Sunny Goyalff9e7d62020-10-21 15:42:05 -0700187
Zak Cohen4d35ac32021-04-23 16:28:12 -0700188 public static final IntProperty<View> VIEW_BACKGROUND_COLOR =
189 new IntProperty<View>("backgroundColor") {
190 @Override
191 public void setValue(View view, int color) {
192 view.setBackgroundColor(color);
193 }
194
195 @Override
196 public Integer get(View view) {
197 if (!(view.getBackground() instanceof ColorDrawable)) {
198 return Color.TRANSPARENT;
199 }
200 return ((ColorDrawable) view.getBackground()).getColor();
201 }
202 };
203
Pat Manninge68ae412023-01-29 01:25:27 +0000204 public static final FloatProperty<ImageView> ROTATION_DRAWABLE_PERCENT =
205 new FloatProperty<ImageView>("drawableRotationPercent") {
206 // RotateDrawable linearly interpolates the rotation degrees between fromDegrees
207 // and toDegrees using the drawable level as a percent of its MAX_LEVEL.
208 private static final int MAX_LEVEL = 10000;
209
210 @Override
211 public void setValue(ImageView view, float percent) {
212 view.setImageLevel((int) (percent * MAX_LEVEL));
213 }
214
215 @Override
216 public Float get(ImageView view) {
217 return view.getDrawable().getLevel() / (float) MAX_LEVEL;
218 }
219 };
220
Sunny Goyalff9e7d62020-10-21 15:42:05 -0700221 /**
222 * Utility method to create an {@link AnimatorListener} which executes a callback on animation
223 * cancel.
224 */
225 public static AnimatorListener newCancelListener(Runnable callback) {
226 return new AnimatorListenerAdapter() {
227
228 boolean mDispatched = false;
229
230 @Override
231 public void onAnimationCancel(Animator animation) {
232 if (!mDispatched) {
233 mDispatched = true;
234 callback.run();
235 }
236 }
237 };
238 }
Alex Chau950a1072022-09-16 14:53:11 +0800239
240 /**
241 * A property that updates the specified property within a given range of values (ie. even if
242 * the animator goes beyond 0..1, the interpolated value will still be bounded).
243 * @param <T> the specified property
244 */
245 public static class ClampedProperty<T> extends FloatProperty<T> {
246 private final FloatProperty<T> mProperty;
247 private final float mMinValue;
248 private final float mMaxValue;
249
250 public ClampedProperty(FloatProperty<T> property, float minValue, float maxValue) {
251 super(property.getName() + "Clamped");
252 mProperty = property;
253 mMinValue = minValue;
254 mMaxValue = maxValue;
255 }
256
257 @Override
258 public void setValue(T t, float v) {
259 mProperty.set(t, Utilities.boundToRange(v, mMinValue, mMaxValue));
260 }
261
262 @Override
263 public Float get(T t) {
264 return mProperty.get(t);
265 }
266 }
Michael Jurka2ecf9952012-06-18 12:52:28 -0700267}