blob: b858d1a13aeef92de055c79f550f8acb1ff6a595 [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;
Luca Zuccarini8009c1d2022-05-24 16:17:19 +000029import android.widget.TextView;
Michael Jurka2ecf9952012-06-18 12:52:28 -070030
Nicolo' Mazzucato12131a42022-02-07 20:36:35 +010031import com.android.launcher3.util.MultiScalePropertyFactory;
32
Michael Jurka2ecf9952012-06-18 12:52:28 -070033public class LauncherAnimUtils {
Sunny Goyalaeb16432017-10-16 11:46:41 -070034 /**
35 * Durations for various state animations. These are not defined in resources to allow
36 * easier access from static classes and enums
37 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -070038 public static final int SPRING_LOADED_EXIT_DELAY = 500;
Sunny Goyalaeb16432017-10-16 11:46:41 -070039
Sunny Goyalff9e7d62020-10-21 15:42:05 -070040 // Progress after which the transition is assumed to be a success
41 public static final float SUCCESS_TRANSITION_PROGRESS = 0.5f;
Alex Chauc83ea5f2022-03-31 14:47:56 +010042 public static final float TABLET_BOTTOM_SHEET_SUCCESS_TRANSITION_PROGRESS = 0.3f;
Tony Wickham0f640b62018-05-09 16:00:14 -070043
Sunny Goyaldbcc63e2020-03-06 15:35:55 -080044 public static final IntProperty<Drawable> DRAWABLE_ALPHA =
45 new IntProperty<Drawable>("drawableAlpha") {
Jon Miranda2d89ea82017-05-04 11:47:53 -070046 @Override
47 public Integer get(Drawable drawable) {
48 return drawable.getAlpha();
49 }
50
51 @Override
Sunny Goyaldbcc63e2020-03-06 15:35:55 -080052 public void setValue(Drawable drawable, int alpha) {
Jon Miranda2d89ea82017-05-04 11:47:53 -070053 drawable.setAlpha(alpha);
54 }
55 };
Sunny Goyalaeb16432017-10-16 11:46:41 -070056
Sunny Goyalb80941b2019-06-19 21:30:40 -070057 public static final FloatProperty<View> SCALE_PROPERTY =
58 new FloatProperty<View>("scale") {
Sunny Goyalaeb16432017-10-16 11:46:41 -070059 @Override
60 public Float get(View view) {
61 return view.getScaleX();
62 }
63
64 @Override
Sunny Goyalb80941b2019-06-19 21:30:40 -070065 public void setValue(View view, float scale) {
Sunny Goyalaeb16432017-10-16 11:46:41 -070066 view.setScaleX(scale);
67 view.setScaleY(scale);
68 }
69 };
Sunny Goyalea529082017-10-31 13:33:03 -070070
Nicolo' Mazzucato12131a42022-02-07 20:36:35 +010071 /**
Nicolo' Mazzucato5765d422022-02-21 18:11:23 +010072 * Property to set the scale of workspace. The value is based on a combination
Nicolo' Mazzucato12131a42022-02-07 20:36:35 +010073 * of all the ones set, to have a smooth experience even in the case of overlapping scaling
74 * animation.
75 */
Shikha Malhotraf78da1b2022-04-11 10:23:18 +000076 public static final MultiScalePropertyFactory<Workspace<?>> WORKSPACE_SCALE_PROPERTY_FACTORY =
77 new MultiScalePropertyFactory<Workspace<?>>("workspace_scale_property");
Nicolo' Mazzucato5765d422022-02-21 18:11:23 +010078
79 /** Property to set the scale of hotseat. */
80 public static final MultiScalePropertyFactory<Hotseat> HOTSEAT_SCALE_PROPERTY_FACTORY =
81 new MultiScalePropertyFactory<Hotseat>("hotseat_scale_property");
Nicolo' Mazzucato12131a42022-02-07 20:36:35 +010082
83 public static final int SCALE_INDEX_UNFOLD_ANIMATION = 1;
Alex Chau0c4e11b2022-07-08 18:41:36 +010084 public static final int SCALE_INDEX_WORKSPACE_STATE = 2;
85 public static final int SCALE_INDEX_REVEAL_ANIM = 3;
86 public static final int SCALE_INDEX_WIDGET_TRANSITION = 4;
Nicolo' Mazzucato12131a42022-02-07 20:36:35 +010087
Tony Wickham0f640b62018-05-09 16:00:14 -070088 /** Increase the duration if we prevented the fling, as we are going against a high velocity. */
89 public static int blockedFlingDurationFactor(float velocity) {
90 return (int) Utilities.boundToRange(Math.abs(velocity) / 2, 2f, 6f);
91 }
Sunny Goyal55bdeed2018-09-18 16:17:22 -070092
Sunny Goyaldbcc63e2020-03-06 15:35:55 -080093 public static final IntProperty<LayoutParams> LAYOUT_WIDTH =
94 new IntProperty<LayoutParams>("width") {
Sunny Goyal55bdeed2018-09-18 16:17:22 -070095 @Override
96 public Integer get(LayoutParams lp) {
97 return lp.width;
98 }
99
100 @Override
Sunny Goyaldbcc63e2020-03-06 15:35:55 -0800101 public void setValue(LayoutParams lp, int width) {
Sunny Goyal55bdeed2018-09-18 16:17:22 -0700102 lp.width = width;
103 }
104 };
105
Sunny Goyaldbcc63e2020-03-06 15:35:55 -0800106 public static final IntProperty<LayoutParams> LAYOUT_HEIGHT =
107 new IntProperty<LayoutParams>("height") {
Sunny Goyal55bdeed2018-09-18 16:17:22 -0700108 @Override
109 public Integer get(LayoutParams lp) {
110 return lp.height;
111 }
112
113 @Override
Sunny Goyaldbcc63e2020-03-06 15:35:55 -0800114 public void setValue(LayoutParams lp, int height) {
Sunny Goyal55bdeed2018-09-18 16:17:22 -0700115 lp.height = height;
116 }
117 };
Jon Miranda86f6c442019-01-29 11:14:38 -0800118
Luca Zuccarini8009c1d2022-05-24 16:17:19 +0000119 public static final IntProperty<TextView> TEXT_COLOR =
120 new IntProperty<TextView>("textColor") {
121 @Override
122 public Integer get(TextView view) {
123 return view.getTextColors().getDefaultColor();
124 }
125
126 @Override
127 public void setValue(TextView view, int color) {
128 view.setTextColor(color);
129 }
130 };
131
132 public static final IntProperty<TextView> HINT_TEXT_COLOR =
133 new IntProperty<TextView>("hintTextColor") {
134 @Override
135 public Integer get(TextView view) {
136 return view.getHintTextColors().getDefaultColor();
137 }
138
139 @Override
140 public void setValue(TextView view, int color) {
141 view.setHintTextColor(color);
142 }
143 };
144
Sunny Goyalb80941b2019-06-19 21:30:40 -0700145 public static final FloatProperty<View> VIEW_TRANSLATE_X =
146 View.TRANSLATION_X instanceof FloatProperty ? (FloatProperty) View.TRANSLATION_X
147 : new FloatProperty<View>("translateX") {
148 @Override
149 public void setValue(View view, float v) {
150 view.setTranslationX(v);
151 }
Jon Miranda86f6c442019-01-29 11:14:38 -0800152
Sunny Goyalb80941b2019-06-19 21:30:40 -0700153 @Override
154 public Float get(View view) {
155 return view.getTranslationX();
156 }
157 };
Jon Miranda86f6c442019-01-29 11:14:38 -0800158
Sunny Goyalb80941b2019-06-19 21:30:40 -0700159 public static final FloatProperty<View> VIEW_TRANSLATE_Y =
160 View.TRANSLATION_Y instanceof FloatProperty ? (FloatProperty) View.TRANSLATION_Y
161 : new FloatProperty<View>("translateY") {
162 @Override
163 public void setValue(View view, float v) {
164 view.setTranslationY(v);
165 }
Jon Miranda86f6c442019-01-29 11:14:38 -0800166
Sunny Goyalb80941b2019-06-19 21:30:40 -0700167 @Override
168 public Float get(View view) {
169 return view.getTranslationY();
170 }
171 };
Sunny Goyaldbcc63e2020-03-06 15:35:55 -0800172
173 public static final FloatProperty<View> VIEW_ALPHA =
174 View.ALPHA instanceof FloatProperty ? (FloatProperty) View.ALPHA
175 : new FloatProperty<View>("alpha") {
176 @Override
177 public void setValue(View view, float v) {
178 view.setAlpha(v);
179 }
180
181 @Override
182 public Float get(View view) {
183 return view.getAlpha();
184 }
185 };
Sunny Goyalff9e7d62020-10-21 15:42:05 -0700186
Zak Cohen4d35ac32021-04-23 16:28:12 -0700187 public static final IntProperty<View> VIEW_BACKGROUND_COLOR =
188 new IntProperty<View>("backgroundColor") {
189 @Override
190 public void setValue(View view, int color) {
191 view.setBackgroundColor(color);
192 }
193
194 @Override
195 public Integer get(View view) {
196 if (!(view.getBackground() instanceof ColorDrawable)) {
197 return Color.TRANSPARENT;
198 }
199 return ((ColorDrawable) view.getBackground()).getColor();
200 }
201 };
202
Sunny Goyalff9e7d62020-10-21 15:42:05 -0700203 /**
204 * Utility method to create an {@link AnimatorListener} which executes a callback on animation
205 * cancel.
206 */
207 public static AnimatorListener newCancelListener(Runnable callback) {
208 return new AnimatorListenerAdapter() {
209
210 boolean mDispatched = false;
211
212 @Override
213 public void onAnimationCancel(Animator animation) {
214 if (!mDispatched) {
215 mDispatched = true;
216 callback.run();
217 }
218 }
219 };
220 }
Michael Jurka2ecf9952012-06-18 12:52:28 -0700221}