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; |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 29 | |
Nicolo' Mazzucato | 12131a4 | 2022-02-07 20:36:35 +0100 | [diff] [blame] | 30 | import com.android.launcher3.util.MultiScalePropertyFactory; |
| 31 | |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 32 | public class LauncherAnimUtils { |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 33 | /** |
| 34 | * Durations for various state animations. These are not defined in resources to allow |
| 35 | * easier access from static classes and enums |
| 36 | */ |
Sunny Goyal | 4c7f215 | 2017-10-17 17:17:16 -0700 | [diff] [blame] | 37 | public static final int SPRING_LOADED_EXIT_DELAY = 500; |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 38 | |
Sunny Goyal | ff9e7d6 | 2020-10-21 15:42:05 -0700 | [diff] [blame] | 39 | // Progress after which the transition is assumed to be a success |
| 40 | public static final float SUCCESS_TRANSITION_PROGRESS = 0.5f; |
Alex Chau | c83ea5f | 2022-03-31 14:47:56 +0100 | [diff] [blame] | 41 | public static final float TABLET_BOTTOM_SHEET_SUCCESS_TRANSITION_PROGRESS = 0.3f; |
Tony Wickham | 0f640b6 | 2018-05-09 16:00:14 -0700 | [diff] [blame] | 42 | |
Sunny Goyal | dbcc63e | 2020-03-06 15:35:55 -0800 | [diff] [blame] | 43 | public static final IntProperty<Drawable> DRAWABLE_ALPHA = |
| 44 | new IntProperty<Drawable>("drawableAlpha") { |
Jon Miranda | 2d89ea8 | 2017-05-04 11:47:53 -0700 | [diff] [blame] | 45 | @Override |
| 46 | public Integer get(Drawable drawable) { |
| 47 | return drawable.getAlpha(); |
| 48 | } |
| 49 | |
| 50 | @Override |
Sunny Goyal | dbcc63e | 2020-03-06 15:35:55 -0800 | [diff] [blame] | 51 | public void setValue(Drawable drawable, int alpha) { |
Jon Miranda | 2d89ea8 | 2017-05-04 11:47:53 -0700 | [diff] [blame] | 52 | drawable.setAlpha(alpha); |
| 53 | } |
| 54 | }; |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 55 | |
Sunny Goyal | b80941b | 2019-06-19 21:30:40 -0700 | [diff] [blame] | 56 | public static final FloatProperty<View> SCALE_PROPERTY = |
| 57 | new FloatProperty<View>("scale") { |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 58 | @Override |
| 59 | public Float get(View view) { |
| 60 | return view.getScaleX(); |
| 61 | } |
| 62 | |
| 63 | @Override |
Sunny Goyal | b80941b | 2019-06-19 21:30:40 -0700 | [diff] [blame] | 64 | public void setValue(View view, float scale) { |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 65 | view.setScaleX(scale); |
| 66 | view.setScaleY(scale); |
| 67 | } |
| 68 | }; |
Sunny Goyal | ea52908 | 2017-10-31 13:33:03 -0700 | [diff] [blame] | 69 | |
Nicolo' Mazzucato | 12131a4 | 2022-02-07 20:36:35 +0100 | [diff] [blame] | 70 | /** |
Nicolo' Mazzucato | 5765d42 | 2022-02-21 18:11:23 +0100 | [diff] [blame] | 71 | * 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] | 72 | * of all the ones set, to have a smooth experience even in the case of overlapping scaling |
| 73 | * animation. |
| 74 | */ |
Shikha Malhotra | f78da1b | 2022-04-11 10:23:18 +0000 | [diff] [blame^] | 75 | public static final MultiScalePropertyFactory<Workspace<?>> WORKSPACE_SCALE_PROPERTY_FACTORY = |
| 76 | new MultiScalePropertyFactory<Workspace<?>>("workspace_scale_property"); |
Nicolo' Mazzucato | 5765d42 | 2022-02-21 18:11:23 +0100 | [diff] [blame] | 77 | |
| 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' Mazzucato | 12131a4 | 2022-02-07 20:36:35 +0100 | [diff] [blame] | 81 | |
| 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 Wickham | 0f640b6 | 2018-05-09 16:00:14 -0700 | [diff] [blame] | 87 | /** 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 Goyal | 55bdeed | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 91 | |
Sunny Goyal | dbcc63e | 2020-03-06 15:35:55 -0800 | [diff] [blame] | 92 | public static final IntProperty<LayoutParams> LAYOUT_WIDTH = |
| 93 | new IntProperty<LayoutParams>("width") { |
Sunny Goyal | 55bdeed | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 94 | @Override |
| 95 | public Integer get(LayoutParams lp) { |
| 96 | return lp.width; |
| 97 | } |
| 98 | |
| 99 | @Override |
Sunny Goyal | dbcc63e | 2020-03-06 15:35:55 -0800 | [diff] [blame] | 100 | public void setValue(LayoutParams lp, int width) { |
Sunny Goyal | 55bdeed | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 101 | lp.width = width; |
| 102 | } |
| 103 | }; |
| 104 | |
Sunny Goyal | dbcc63e | 2020-03-06 15:35:55 -0800 | [diff] [blame] | 105 | public static final IntProperty<LayoutParams> LAYOUT_HEIGHT = |
| 106 | new IntProperty<LayoutParams>("height") { |
Sunny Goyal | 55bdeed | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 107 | @Override |
| 108 | public Integer get(LayoutParams lp) { |
| 109 | return lp.height; |
| 110 | } |
| 111 | |
| 112 | @Override |
Sunny Goyal | dbcc63e | 2020-03-06 15:35:55 -0800 | [diff] [blame] | 113 | public void setValue(LayoutParams lp, int height) { |
Sunny Goyal | 55bdeed | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 114 | lp.height = height; |
| 115 | } |
| 116 | }; |
Jon Miranda | 86f6c44 | 2019-01-29 11:14:38 -0800 | [diff] [blame] | 117 | |
Sunny Goyal | b80941b | 2019-06-19 21:30:40 -0700 | [diff] [blame] | 118 | 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 Miranda | 86f6c44 | 2019-01-29 11:14:38 -0800 | [diff] [blame] | 125 | |
Sunny Goyal | b80941b | 2019-06-19 21:30:40 -0700 | [diff] [blame] | 126 | @Override |
| 127 | public Float get(View view) { |
| 128 | return view.getTranslationX(); |
| 129 | } |
| 130 | }; |
Jon Miranda | 86f6c44 | 2019-01-29 11:14:38 -0800 | [diff] [blame] | 131 | |
Sunny Goyal | b80941b | 2019-06-19 21:30:40 -0700 | [diff] [blame] | 132 | 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 Miranda | 86f6c44 | 2019-01-29 11:14:38 -0800 | [diff] [blame] | 139 | |
Sunny Goyal | b80941b | 2019-06-19 21:30:40 -0700 | [diff] [blame] | 140 | @Override |
| 141 | public Float get(View view) { |
| 142 | return view.getTranslationY(); |
| 143 | } |
| 144 | }; |
Sunny Goyal | dbcc63e | 2020-03-06 15:35:55 -0800 | [diff] [blame] | 145 | |
| 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 Goyal | ff9e7d6 | 2020-10-21 15:42:05 -0700 | [diff] [blame] | 159 | |
Zak Cohen | 4d35ac3 | 2021-04-23 16:28:12 -0700 | [diff] [blame] | 160 | 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 Goyal | ff9e7d6 | 2020-10-21 15:42:05 -0700 | [diff] [blame] | 176 | /** |
| 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 Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 194 | } |