blob: 808bf96f9f6ef438aa35dcb1f12dc0d1e7d64ca5 [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;
Alex Chauc83ea5f2022-03-31 14:47:56 +010041 public static final float TABLET_BOTTOM_SHEET_SUCCESS_TRANSITION_PROGRESS = 0.3f;
Tony Wickham0f640b62018-05-09 16:00:14 -070042
Sunny Goyaldbcc63e2020-03-06 15:35:55 -080043 public static final IntProperty<Drawable> DRAWABLE_ALPHA =
44 new IntProperty<Drawable>("drawableAlpha") {
Jon Miranda2d89ea82017-05-04 11:47:53 -070045 @Override
46 public Integer get(Drawable drawable) {
47 return drawable.getAlpha();
48 }
49
50 @Override
Sunny Goyaldbcc63e2020-03-06 15:35:55 -080051 public void setValue(Drawable drawable, int alpha) {
Jon Miranda2d89ea82017-05-04 11:47:53 -070052 drawable.setAlpha(alpha);
53 }
54 };
Sunny Goyalaeb16432017-10-16 11:46:41 -070055
Sunny Goyalb80941b2019-06-19 21:30:40 -070056 public static final FloatProperty<View> SCALE_PROPERTY =
57 new FloatProperty<View>("scale") {
Sunny Goyalaeb16432017-10-16 11:46:41 -070058 @Override
59 public Float get(View view) {
60 return view.getScaleX();
61 }
62
63 @Override
Sunny Goyalb80941b2019-06-19 21:30:40 -070064 public void setValue(View view, float scale) {
Sunny Goyalaeb16432017-10-16 11:46:41 -070065 view.setScaleX(scale);
66 view.setScaleY(scale);
67 }
68 };
Sunny Goyalea529082017-10-31 13:33:03 -070069
Nicolo' Mazzucato12131a42022-02-07 20:36:35 +010070 /**
Nicolo' Mazzucato5765d422022-02-21 18:11:23 +010071 * Property to set the scale of workspace. The value is based on a combination
Nicolo' Mazzucato12131a42022-02-07 20:36:35 +010072 * of all the ones set, to have a smooth experience even in the case of overlapping scaling
73 * animation.
74 */
Shikha Malhotraf78da1b2022-04-11 10:23:18 +000075 public static final MultiScalePropertyFactory<Workspace<?>> WORKSPACE_SCALE_PROPERTY_FACTORY =
76 new MultiScalePropertyFactory<Workspace<?>>("workspace_scale_property");
Nicolo' Mazzucato5765d422022-02-21 18:11:23 +010077
78 /** Property to set the scale of hotseat. */
79 public static final MultiScalePropertyFactory<Hotseat> HOTSEAT_SCALE_PROPERTY_FACTORY =
80 new MultiScalePropertyFactory<Hotseat>("hotseat_scale_property");
Nicolo' Mazzucato12131a42022-02-07 20:36:35 +010081
82 public static final int SCALE_INDEX_UNFOLD_ANIMATION = 1;
83 public static final int SCALE_INDEX_UNLOCK_ANIMATION = 2;
84 public static final int SCALE_INDEX_WORKSPACE_STATE = 3;
85 public static final int SCALE_INDEX_REVEAL_ANIM = 4;
86
Tony Wickham0f640b62018-05-09 16:00:14 -070087 /** Increase the duration if we prevented the fling, as we are going against a high velocity. */
88 public static int blockedFlingDurationFactor(float velocity) {
89 return (int) Utilities.boundToRange(Math.abs(velocity) / 2, 2f, 6f);
90 }
Sunny Goyal55bdeed2018-09-18 16:17:22 -070091
Sunny Goyaldbcc63e2020-03-06 15:35:55 -080092 public static final IntProperty<LayoutParams> LAYOUT_WIDTH =
93 new IntProperty<LayoutParams>("width") {
Sunny Goyal55bdeed2018-09-18 16:17:22 -070094 @Override
95 public Integer get(LayoutParams lp) {
96 return lp.width;
97 }
98
99 @Override
Sunny Goyaldbcc63e2020-03-06 15:35:55 -0800100 public void setValue(LayoutParams lp, int width) {
Sunny Goyal55bdeed2018-09-18 16:17:22 -0700101 lp.width = width;
102 }
103 };
104
Sunny Goyaldbcc63e2020-03-06 15:35:55 -0800105 public static final IntProperty<LayoutParams> LAYOUT_HEIGHT =
106 new IntProperty<LayoutParams>("height") {
Sunny Goyal55bdeed2018-09-18 16:17:22 -0700107 @Override
108 public Integer get(LayoutParams lp) {
109 return lp.height;
110 }
111
112 @Override
Sunny Goyaldbcc63e2020-03-06 15:35:55 -0800113 public void setValue(LayoutParams lp, int height) {
Sunny Goyal55bdeed2018-09-18 16:17:22 -0700114 lp.height = height;
115 }
116 };
Jon Miranda86f6c442019-01-29 11:14:38 -0800117
Sunny Goyalb80941b2019-06-19 21:30:40 -0700118 public static final FloatProperty<View> VIEW_TRANSLATE_X =
119 View.TRANSLATION_X instanceof FloatProperty ? (FloatProperty) View.TRANSLATION_X
120 : new FloatProperty<View>("translateX") {
121 @Override
122 public void setValue(View view, float v) {
123 view.setTranslationX(v);
124 }
Jon Miranda86f6c442019-01-29 11:14:38 -0800125
Sunny Goyalb80941b2019-06-19 21:30:40 -0700126 @Override
127 public Float get(View view) {
128 return view.getTranslationX();
129 }
130 };
Jon Miranda86f6c442019-01-29 11:14:38 -0800131
Sunny Goyalb80941b2019-06-19 21:30:40 -0700132 public static final FloatProperty<View> VIEW_TRANSLATE_Y =
133 View.TRANSLATION_Y instanceof FloatProperty ? (FloatProperty) View.TRANSLATION_Y
134 : new FloatProperty<View>("translateY") {
135 @Override
136 public void setValue(View view, float v) {
137 view.setTranslationY(v);
138 }
Jon Miranda86f6c442019-01-29 11:14:38 -0800139
Sunny Goyalb80941b2019-06-19 21:30:40 -0700140 @Override
141 public Float get(View view) {
142 return view.getTranslationY();
143 }
144 };
Sunny Goyaldbcc63e2020-03-06 15:35:55 -0800145
146 public static final FloatProperty<View> VIEW_ALPHA =
147 View.ALPHA instanceof FloatProperty ? (FloatProperty) View.ALPHA
148 : new FloatProperty<View>("alpha") {
149 @Override
150 public void setValue(View view, float v) {
151 view.setAlpha(v);
152 }
153
154 @Override
155 public Float get(View view) {
156 return view.getAlpha();
157 }
158 };
Sunny Goyalff9e7d62020-10-21 15:42:05 -0700159
Zak Cohen4d35ac32021-04-23 16:28:12 -0700160 public static final IntProperty<View> VIEW_BACKGROUND_COLOR =
161 new IntProperty<View>("backgroundColor") {
162 @Override
163 public void setValue(View view, int color) {
164 view.setBackgroundColor(color);
165 }
166
167 @Override
168 public Integer get(View view) {
169 if (!(view.getBackground() instanceof ColorDrawable)) {
170 return Color.TRANSPARENT;
171 }
172 return ((ColorDrawable) view.getBackground()).getColor();
173 }
174 };
175
Sunny Goyalff9e7d62020-10-21 15:42:05 -0700176 /**
177 * Utility method to create an {@link AnimatorListener} which executes a callback on animation
178 * cancel.
179 */
180 public static AnimatorListener newCancelListener(Runnable callback) {
181 return new AnimatorListenerAdapter() {
182
183 boolean mDispatched = false;
184
185 @Override
186 public void onAnimationCancel(Animator animation) {
187 if (!mDispatched) {
188 mDispatched = true;
189 callback.run();
190 }
191 }
192 };
193 }
Michael Jurka2ecf9952012-06-18 12:52:28 -0700194}