blob: 430039276ebd464b3de01ffb323d7c71397f0adc [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;
Michael Jurka2ecf9952012-06-18 12:52:28 -070029
Nicolo' Mazzucato12131a42022-02-07 20:36:35 +010030import com.android.launcher3.util.MultiScalePropertyFactory;
31
Michael Jurka2ecf9952012-06-18 12:52:28 -070032public class LauncherAnimUtils {
Sunny Goyalaeb16432017-10-16 11:46:41 -070033 /**
34 * Durations for various state animations. These are not defined in resources to allow
35 * easier access from static classes and enums
36 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -070037 public static final int SPRING_LOADED_EXIT_DELAY = 500;
Sunny Goyalaeb16432017-10-16 11:46:41 -070038
Sunny Goyalff9e7d62020-10-21 15:42:05 -070039 // Progress after which the transition is assumed to be a success
40 public static final float SUCCESS_TRANSITION_PROGRESS = 0.5f;
Tony Wickham0f640b62018-05-09 16:00:14 -070041
Sunny Goyaldbcc63e2020-03-06 15:35:55 -080042 public static final IntProperty<Drawable> DRAWABLE_ALPHA =
43 new IntProperty<Drawable>("drawableAlpha") {
Jon Miranda2d89ea82017-05-04 11:47:53 -070044 @Override
45 public Integer get(Drawable drawable) {
46 return drawable.getAlpha();
47 }
48
49 @Override
Sunny Goyaldbcc63e2020-03-06 15:35:55 -080050 public void setValue(Drawable drawable, int alpha) {
Jon Miranda2d89ea82017-05-04 11:47:53 -070051 drawable.setAlpha(alpha);
52 }
53 };
Sunny Goyalaeb16432017-10-16 11:46:41 -070054
Sunny Goyalb80941b2019-06-19 21:30:40 -070055 public static final FloatProperty<View> SCALE_PROPERTY =
56 new FloatProperty<View>("scale") {
Sunny Goyalaeb16432017-10-16 11:46:41 -070057 @Override
58 public Float get(View view) {
59 return view.getScaleX();
60 }
61
62 @Override
Sunny Goyalb80941b2019-06-19 21:30:40 -070063 public void setValue(View view, float scale) {
Sunny Goyalaeb16432017-10-16 11:46:41 -070064 view.setScaleX(scale);
65 view.setScaleY(scale);
66 }
67 };
Sunny Goyalea529082017-10-31 13:33:03 -070068
Nicolo' Mazzucato12131a42022-02-07 20:36:35 +010069 /**
70 * Property to set the scale of workspace and hotseat. The value is based on a combination
71 * of all the ones set, to have a smooth experience even in the case of overlapping scaling
72 * animation.
73 */
74 public static final MultiScalePropertyFactory<View> SCALE_PROPERTY_FACTORY =
75 new MultiScalePropertyFactory<View>("scale_property") {
76 @Override
77 protected void apply(View view, float scale) {
78 view.setScaleX(scale);
79 view.setScaleY(scale);
80 }
81 };
82
83 public static final int SCALE_INDEX_UNFOLD_ANIMATION = 1;
84 public static final int SCALE_INDEX_UNLOCK_ANIMATION = 2;
85 public static final int SCALE_INDEX_WORKSPACE_STATE = 3;
86 public static final int SCALE_INDEX_REVEAL_ANIM = 4;
87
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
Sunny Goyalb80941b2019-06-19 21:30:40 -0700119 public static final FloatProperty<View> VIEW_TRANSLATE_X =
120 View.TRANSLATION_X instanceof FloatProperty ? (FloatProperty) View.TRANSLATION_X
121 : new FloatProperty<View>("translateX") {
122 @Override
123 public void setValue(View view, float v) {
124 view.setTranslationX(v);
125 }
Jon Miranda86f6c442019-01-29 11:14:38 -0800126
Sunny Goyalb80941b2019-06-19 21:30:40 -0700127 @Override
128 public Float get(View view) {
129 return view.getTranslationX();
130 }
131 };
Jon Miranda86f6c442019-01-29 11:14:38 -0800132
Sunny Goyalb80941b2019-06-19 21:30:40 -0700133 public static final FloatProperty<View> VIEW_TRANSLATE_Y =
134 View.TRANSLATION_Y instanceof FloatProperty ? (FloatProperty) View.TRANSLATION_Y
135 : new FloatProperty<View>("translateY") {
136 @Override
137 public void setValue(View view, float v) {
138 view.setTranslationY(v);
139 }
Jon Miranda86f6c442019-01-29 11:14:38 -0800140
Sunny Goyalb80941b2019-06-19 21:30:40 -0700141 @Override
142 public Float get(View view) {
143 return view.getTranslationY();
144 }
145 };
Sunny Goyaldbcc63e2020-03-06 15:35:55 -0800146
147 public static final FloatProperty<View> VIEW_ALPHA =
148 View.ALPHA instanceof FloatProperty ? (FloatProperty) View.ALPHA
149 : new FloatProperty<View>("alpha") {
150 @Override
151 public void setValue(View view, float v) {
152 view.setAlpha(v);
153 }
154
155 @Override
156 public Float get(View view) {
157 return view.getAlpha();
158 }
159 };
Sunny Goyalff9e7d62020-10-21 15:42:05 -0700160
Zak Cohen4d35ac32021-04-23 16:28:12 -0700161 public static final IntProperty<View> VIEW_BACKGROUND_COLOR =
162 new IntProperty<View>("backgroundColor") {
163 @Override
164 public void setValue(View view, int color) {
165 view.setBackgroundColor(color);
166 }
167
168 @Override
169 public Integer get(View view) {
170 if (!(view.getBackground() instanceof ColorDrawable)) {
171 return Color.TRANSPARENT;
172 }
173 return ((ColorDrawable) view.getBackground()).getColor();
174 }
175 };
176
Sunny Goyalff9e7d62020-10-21 15:42:05 -0700177 /**
178 * Utility method to create an {@link AnimatorListener} which executes a callback on animation
179 * cancel.
180 */
181 public static AnimatorListener newCancelListener(Runnable callback) {
182 return new AnimatorListenerAdapter() {
183
184 boolean mDispatched = false;
185
186 @Override
187 public void onAnimationCancel(Animator animation) {
188 if (!mDispatched) {
189 mDispatched = true;
190 callback.run();
191 }
192 }
193 };
194 }
Michael Jurka2ecf9952012-06-18 12:52:28 -0700195}