blob: c43172cc56b742a5c664d3c7e936a7986c6cff14 [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 /**
Nicolo' Mazzucato5765d422022-02-21 18:11:23 +010070 * Property to set the scale of workspace. The value is based on a combination
Nicolo' Mazzucato12131a42022-02-07 20:36:35 +010071 * of all the ones set, to have a smooth experience even in the case of overlapping scaling
72 * animation.
73 */
Nicolo' Mazzucato5765d422022-02-21 18:11:23 +010074 public static final MultiScalePropertyFactory<Workspace> WORKSPACE_SCALE_PROPERTY_FACTORY =
75 new MultiScalePropertyFactory<Workspace>("workspace_scale_property");
76
77 /** Property to set the scale of hotseat. */
78 public static final MultiScalePropertyFactory<Hotseat> HOTSEAT_SCALE_PROPERTY_FACTORY =
79 new MultiScalePropertyFactory<Hotseat>("hotseat_scale_property");
Nicolo' Mazzucato12131a42022-02-07 20:36:35 +010080
81 public static final int SCALE_INDEX_UNFOLD_ANIMATION = 1;
82 public static final int SCALE_INDEX_UNLOCK_ANIMATION = 2;
83 public static final int SCALE_INDEX_WORKSPACE_STATE = 3;
84 public static final int SCALE_INDEX_REVEAL_ANIM = 4;
85
Tony Wickham0f640b62018-05-09 16:00:14 -070086 /** Increase the duration if we prevented the fling, as we are going against a high velocity. */
87 public static int blockedFlingDurationFactor(float velocity) {
88 return (int) Utilities.boundToRange(Math.abs(velocity) / 2, 2f, 6f);
89 }
Sunny Goyal55bdeed2018-09-18 16:17:22 -070090
Sunny Goyaldbcc63e2020-03-06 15:35:55 -080091 public static final IntProperty<LayoutParams> LAYOUT_WIDTH =
92 new IntProperty<LayoutParams>("width") {
Sunny Goyal55bdeed2018-09-18 16:17:22 -070093 @Override
94 public Integer get(LayoutParams lp) {
95 return lp.width;
96 }
97
98 @Override
Sunny Goyaldbcc63e2020-03-06 15:35:55 -080099 public void setValue(LayoutParams lp, int width) {
Sunny Goyal55bdeed2018-09-18 16:17:22 -0700100 lp.width = width;
101 }
102 };
103
Sunny Goyaldbcc63e2020-03-06 15:35:55 -0800104 public static final IntProperty<LayoutParams> LAYOUT_HEIGHT =
105 new IntProperty<LayoutParams>("height") {
Sunny Goyal55bdeed2018-09-18 16:17:22 -0700106 @Override
107 public Integer get(LayoutParams lp) {
108 return lp.height;
109 }
110
111 @Override
Sunny Goyaldbcc63e2020-03-06 15:35:55 -0800112 public void setValue(LayoutParams lp, int height) {
Sunny Goyal55bdeed2018-09-18 16:17:22 -0700113 lp.height = height;
114 }
115 };
Jon Miranda86f6c442019-01-29 11:14:38 -0800116
Sunny Goyalb80941b2019-06-19 21:30:40 -0700117 public static final FloatProperty<View> VIEW_TRANSLATE_X =
118 View.TRANSLATION_X instanceof FloatProperty ? (FloatProperty) View.TRANSLATION_X
119 : new FloatProperty<View>("translateX") {
120 @Override
121 public void setValue(View view, float v) {
122 view.setTranslationX(v);
123 }
Jon Miranda86f6c442019-01-29 11:14:38 -0800124
Sunny Goyalb80941b2019-06-19 21:30:40 -0700125 @Override
126 public Float get(View view) {
127 return view.getTranslationX();
128 }
129 };
Jon Miranda86f6c442019-01-29 11:14:38 -0800130
Sunny Goyalb80941b2019-06-19 21:30:40 -0700131 public static final FloatProperty<View> VIEW_TRANSLATE_Y =
132 View.TRANSLATION_Y instanceof FloatProperty ? (FloatProperty) View.TRANSLATION_Y
133 : new FloatProperty<View>("translateY") {
134 @Override
135 public void setValue(View view, float v) {
136 view.setTranslationY(v);
137 }
Jon Miranda86f6c442019-01-29 11:14:38 -0800138
Sunny Goyalb80941b2019-06-19 21:30:40 -0700139 @Override
140 public Float get(View view) {
141 return view.getTranslationY();
142 }
143 };
Sunny Goyaldbcc63e2020-03-06 15:35:55 -0800144
145 public static final FloatProperty<View> VIEW_ALPHA =
146 View.ALPHA instanceof FloatProperty ? (FloatProperty) View.ALPHA
147 : new FloatProperty<View>("alpha") {
148 @Override
149 public void setValue(View view, float v) {
150 view.setAlpha(v);
151 }
152
153 @Override
154 public Float get(View view) {
155 return view.getAlpha();
156 }
157 };
Sunny Goyalff9e7d62020-10-21 15:42:05 -0700158
Zak Cohen4d35ac32021-04-23 16:28:12 -0700159 public static final IntProperty<View> VIEW_BACKGROUND_COLOR =
160 new IntProperty<View>("backgroundColor") {
161 @Override
162 public void setValue(View view, int color) {
163 view.setBackgroundColor(color);
164 }
165
166 @Override
167 public Integer get(View view) {
168 if (!(view.getBackground() instanceof ColorDrawable)) {
169 return Color.TRANSPARENT;
170 }
171 return ((ColorDrawable) view.getBackground()).getColor();
172 }
173 };
174
Sunny Goyalff9e7d62020-10-21 15:42:05 -0700175 /**
176 * Utility method to create an {@link AnimatorListener} which executes a callback on animation
177 * cancel.
178 */
179 public static AnimatorListener newCancelListener(Runnable callback) {
180 return new AnimatorListenerAdapter() {
181
182 boolean mDispatched = false;
183
184 @Override
185 public void onAnimationCancel(Animator animation) {
186 if (!mDispatched) {
187 mDispatched = true;
188 callback.run();
189 }
190 }
191 };
192 }
Michael Jurka2ecf9952012-06-18 12:52:28 -0700193}