Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 1 | /* |
| 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 Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 18 | |
Sunny Goyal | ff9e7d6 | 2020-10-21 15:42:05 -0700 | [diff] [blame] | 19 | import android.animation.Animator; |
| 20 | import android.animation.Animator.AnimatorListener; |
| 21 | import android.animation.AnimatorListenerAdapter; |
Zak Cohen | 4d35ac3 | 2021-04-23 16:28:12 -0700 | [diff] [blame] | 22 | import android.graphics.Color; |
| 23 | import android.graphics.drawable.ColorDrawable; |
Jon Miranda | 2d89ea8 | 2017-05-04 11:47:53 -0700 | [diff] [blame] | 24 | import android.graphics.drawable.Drawable; |
Sunny Goyal | b80941b | 2019-06-19 21:30:40 -0700 | [diff] [blame] | 25 | import android.util.FloatProperty; |
Sunny Goyal | dbcc63e | 2020-03-06 15:35:55 -0800 | [diff] [blame] | 26 | import android.util.IntProperty; |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 27 | import android.view.View; |
Sunny Goyal | 55bdeed | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 28 | import android.view.ViewGroup.LayoutParams; |
Pat Manning | e68ae41 | 2023-01-29 01:25:27 +0000 | [diff] [blame] | 29 | import android.widget.ImageView; |
Luca Zuccarini | 8009c1d | 2022-05-24 16:17:19 +0000 | [diff] [blame] | 30 | import android.widget.TextView; |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 31 | |
Nicolo' Mazzucato | 12131a4 | 2022-02-07 20:36:35 +0100 | [diff] [blame] | 32 | import com.android.launcher3.util.MultiScalePropertyFactory; |
| 33 | |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 34 | public class LauncherAnimUtils { |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 35 | /** |
| 36 | * Durations for various state animations. These are not defined in resources to allow |
| 37 | * easier access from static classes and enums |
| 38 | */ |
Sunny Goyal | 4c7f215 | 2017-10-17 17:17:16 -0700 | [diff] [blame] | 39 | public static final int SPRING_LOADED_EXIT_DELAY = 500; |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 40 | |
Sunny Goyal | ff9e7d6 | 2020-10-21 15:42:05 -0700 | [diff] [blame] | 41 | // Progress after which the transition is assumed to be a success |
| 42 | public static final float SUCCESS_TRANSITION_PROGRESS = 0.5f; |
Alex Chau | c83ea5f | 2022-03-31 14:47:56 +0100 | [diff] [blame] | 43 | public static final float TABLET_BOTTOM_SHEET_SUCCESS_TRANSITION_PROGRESS = 0.3f; |
Tony Wickham | 0f640b6 | 2018-05-09 16:00:14 -0700 | [diff] [blame] | 44 | |
Sunny Goyal | dbcc63e | 2020-03-06 15:35:55 -0800 | [diff] [blame] | 45 | public static final IntProperty<Drawable> DRAWABLE_ALPHA = |
| 46 | new IntProperty<Drawable>("drawableAlpha") { |
Jon Miranda | 2d89ea8 | 2017-05-04 11:47:53 -0700 | [diff] [blame] | 47 | @Override |
| 48 | public Integer get(Drawable drawable) { |
| 49 | return drawable.getAlpha(); |
| 50 | } |
| 51 | |
| 52 | @Override |
Sunny Goyal | dbcc63e | 2020-03-06 15:35:55 -0800 | [diff] [blame] | 53 | public void setValue(Drawable drawable, int alpha) { |
Jon Miranda | 2d89ea8 | 2017-05-04 11:47:53 -0700 | [diff] [blame] | 54 | drawable.setAlpha(alpha); |
| 55 | } |
| 56 | }; |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 57 | |
Sunny Goyal | b80941b | 2019-06-19 21:30:40 -0700 | [diff] [blame] | 58 | public static final FloatProperty<View> SCALE_PROPERTY = |
| 59 | new FloatProperty<View>("scale") { |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 60 | @Override |
| 61 | public Float get(View view) { |
| 62 | return view.getScaleX(); |
| 63 | } |
| 64 | |
| 65 | @Override |
Sunny Goyal | b80941b | 2019-06-19 21:30:40 -0700 | [diff] [blame] | 66 | public void setValue(View view, float scale) { |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 67 | view.setScaleX(scale); |
| 68 | view.setScaleY(scale); |
| 69 | } |
| 70 | }; |
Sunny Goyal | ea52908 | 2017-10-31 13:33:03 -0700 | [diff] [blame] | 71 | |
Nicolo' Mazzucato | 12131a4 | 2022-02-07 20:36:35 +0100 | [diff] [blame] | 72 | /** |
Nicolo' Mazzucato | 5765d42 | 2022-02-21 18:11:23 +0100 | [diff] [blame] | 73 | * Property to set the scale of workspace. The value is based on a combination |
Nicolo' Mazzucato | 12131a4 | 2022-02-07 20:36:35 +0100 | [diff] [blame] | 74 | * of all the ones set, to have a smooth experience even in the case of overlapping scaling |
| 75 | * animation. |
| 76 | */ |
Shikha Malhotra | f78da1b | 2022-04-11 10:23:18 +0000 | [diff] [blame] | 77 | public static final MultiScalePropertyFactory<Workspace<?>> WORKSPACE_SCALE_PROPERTY_FACTORY = |
| 78 | new MultiScalePropertyFactory<Workspace<?>>("workspace_scale_property"); |
Nicolo' Mazzucato | 5765d42 | 2022-02-21 18:11:23 +0100 | [diff] [blame] | 79 | |
| 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' Mazzucato | 12131a4 | 2022-02-07 20:36:35 +0100 | [diff] [blame] | 83 | |
| 84 | public static final int SCALE_INDEX_UNFOLD_ANIMATION = 1; |
Alex Chau | 0c4e11b | 2022-07-08 18:41:36 +0100 | [diff] [blame] | 85 | 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' Mazzucato | 12131a4 | 2022-02-07 20:36:35 +0100 | [diff] [blame] | 88 | |
Tony Wickham | 0f640b6 | 2018-05-09 16:00:14 -0700 | [diff] [blame] | 89 | /** 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 Goyal | 55bdeed | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 93 | |
Sunny Goyal | dbcc63e | 2020-03-06 15:35:55 -0800 | [diff] [blame] | 94 | public static final IntProperty<LayoutParams> LAYOUT_WIDTH = |
| 95 | new IntProperty<LayoutParams>("width") { |
Sunny Goyal | 55bdeed | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 96 | @Override |
| 97 | public Integer get(LayoutParams lp) { |
| 98 | return lp.width; |
| 99 | } |
| 100 | |
| 101 | @Override |
Sunny Goyal | dbcc63e | 2020-03-06 15:35:55 -0800 | [diff] [blame] | 102 | public void setValue(LayoutParams lp, int width) { |
Sunny Goyal | 55bdeed | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 103 | lp.width = width; |
| 104 | } |
| 105 | }; |
| 106 | |
Sunny Goyal | dbcc63e | 2020-03-06 15:35:55 -0800 | [diff] [blame] | 107 | public static final IntProperty<LayoutParams> LAYOUT_HEIGHT = |
| 108 | new IntProperty<LayoutParams>("height") { |
Sunny Goyal | 55bdeed | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 109 | @Override |
| 110 | public Integer get(LayoutParams lp) { |
| 111 | return lp.height; |
| 112 | } |
| 113 | |
| 114 | @Override |
Sunny Goyal | dbcc63e | 2020-03-06 15:35:55 -0800 | [diff] [blame] | 115 | public void setValue(LayoutParams lp, int height) { |
Sunny Goyal | 55bdeed | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 116 | lp.height = height; |
| 117 | } |
| 118 | }; |
Jon Miranda | 86f6c44 | 2019-01-29 11:14:38 -0800 | [diff] [blame] | 119 | |
Luca Zuccarini | 8009c1d | 2022-05-24 16:17:19 +0000 | [diff] [blame] | 120 | 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 Goyal | b80941b | 2019-06-19 21:30:40 -0700 | [diff] [blame] | 146 | 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 Miranda | 86f6c44 | 2019-01-29 11:14:38 -0800 | [diff] [blame] | 153 | |
Sunny Goyal | b80941b | 2019-06-19 21:30:40 -0700 | [diff] [blame] | 154 | @Override |
| 155 | public Float get(View view) { |
| 156 | return view.getTranslationX(); |
| 157 | } |
| 158 | }; |
Jon Miranda | 86f6c44 | 2019-01-29 11:14:38 -0800 | [diff] [blame] | 159 | |
Sunny Goyal | b80941b | 2019-06-19 21:30:40 -0700 | [diff] [blame] | 160 | 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 Miranda | 86f6c44 | 2019-01-29 11:14:38 -0800 | [diff] [blame] | 167 | |
Sunny Goyal | b80941b | 2019-06-19 21:30:40 -0700 | [diff] [blame] | 168 | @Override |
| 169 | public Float get(View view) { |
| 170 | return view.getTranslationY(); |
| 171 | } |
| 172 | }; |
Sunny Goyal | dbcc63e | 2020-03-06 15:35:55 -0800 | [diff] [blame] | 173 | |
| 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 Goyal | ff9e7d6 | 2020-10-21 15:42:05 -0700 | [diff] [blame] | 187 | |
Zak Cohen | 4d35ac3 | 2021-04-23 16:28:12 -0700 | [diff] [blame] | 188 | 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 Manning | e68ae41 | 2023-01-29 01:25:27 +0000 | [diff] [blame] | 204 | 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 Goyal | ff9e7d6 | 2020-10-21 15:42:05 -0700 | [diff] [blame] | 221 | /** |
| 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 Chau | 950a107 | 2022-09-16 14:53:11 +0800 | [diff] [blame] | 239 | |
| 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 Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 267 | } |