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 | |
| 30 | public class LauncherAnimUtils { |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 31 | /** |
| 32 | * Durations for various state animations. These are not defined in resources to allow |
| 33 | * easier access from static classes and enums |
| 34 | */ |
Sunny Goyal | 4c7f215 | 2017-10-17 17:17:16 -0700 | [diff] [blame] | 35 | public static final int SPRING_LOADED_EXIT_DELAY = 500; |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 36 | |
Sunny Goyal | ff9e7d6 | 2020-10-21 15:42:05 -0700 | [diff] [blame] | 37 | // Progress after which the transition is assumed to be a success |
| 38 | public static final float SUCCESS_TRANSITION_PROGRESS = 0.5f; |
Tony Wickham | 0f640b6 | 2018-05-09 16:00:14 -0700 | [diff] [blame] | 39 | |
Sunny Goyal | dbcc63e | 2020-03-06 15:35:55 -0800 | [diff] [blame] | 40 | public static final IntProperty<Drawable> DRAWABLE_ALPHA = |
| 41 | new IntProperty<Drawable>("drawableAlpha") { |
Jon Miranda | 2d89ea8 | 2017-05-04 11:47:53 -0700 | [diff] [blame] | 42 | @Override |
| 43 | public Integer get(Drawable drawable) { |
| 44 | return drawable.getAlpha(); |
| 45 | } |
| 46 | |
| 47 | @Override |
Sunny Goyal | dbcc63e | 2020-03-06 15:35:55 -0800 | [diff] [blame] | 48 | public void setValue(Drawable drawable, int alpha) { |
Jon Miranda | 2d89ea8 | 2017-05-04 11:47:53 -0700 | [diff] [blame] | 49 | drawable.setAlpha(alpha); |
| 50 | } |
| 51 | }; |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 52 | |
Sunny Goyal | b80941b | 2019-06-19 21:30:40 -0700 | [diff] [blame] | 53 | public static final FloatProperty<View> SCALE_PROPERTY = |
| 54 | new FloatProperty<View>("scale") { |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 55 | @Override |
| 56 | public Float get(View view) { |
| 57 | return view.getScaleX(); |
| 58 | } |
| 59 | |
| 60 | @Override |
Sunny Goyal | b80941b | 2019-06-19 21:30:40 -0700 | [diff] [blame] | 61 | public void setValue(View view, float scale) { |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 62 | view.setScaleX(scale); |
| 63 | view.setScaleY(scale); |
| 64 | } |
| 65 | }; |
Sunny Goyal | ea52908 | 2017-10-31 13:33:03 -0700 | [diff] [blame] | 66 | |
Tony Wickham | 0f640b6 | 2018-05-09 16:00:14 -0700 | [diff] [blame] | 67 | /** Increase the duration if we prevented the fling, as we are going against a high velocity. */ |
| 68 | public static int blockedFlingDurationFactor(float velocity) { |
| 69 | return (int) Utilities.boundToRange(Math.abs(velocity) / 2, 2f, 6f); |
| 70 | } |
Sunny Goyal | 55bdeed | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 71 | |
Sunny Goyal | dbcc63e | 2020-03-06 15:35:55 -0800 | [diff] [blame] | 72 | public static final IntProperty<LayoutParams> LAYOUT_WIDTH = |
| 73 | new IntProperty<LayoutParams>("width") { |
Sunny Goyal | 55bdeed | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 74 | @Override |
| 75 | public Integer get(LayoutParams lp) { |
| 76 | return lp.width; |
| 77 | } |
| 78 | |
| 79 | @Override |
Sunny Goyal | dbcc63e | 2020-03-06 15:35:55 -0800 | [diff] [blame] | 80 | public void setValue(LayoutParams lp, int width) { |
Sunny Goyal | 55bdeed | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 81 | lp.width = width; |
| 82 | } |
| 83 | }; |
| 84 | |
Sunny Goyal | dbcc63e | 2020-03-06 15:35:55 -0800 | [diff] [blame] | 85 | public static final IntProperty<LayoutParams> LAYOUT_HEIGHT = |
| 86 | new IntProperty<LayoutParams>("height") { |
Sunny Goyal | 55bdeed | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 87 | @Override |
| 88 | public Integer get(LayoutParams lp) { |
| 89 | return lp.height; |
| 90 | } |
| 91 | |
| 92 | @Override |
Sunny Goyal | dbcc63e | 2020-03-06 15:35:55 -0800 | [diff] [blame] | 93 | public void setValue(LayoutParams lp, int height) { |
Sunny Goyal | 55bdeed | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 94 | lp.height = height; |
| 95 | } |
| 96 | }; |
Jon Miranda | 86f6c44 | 2019-01-29 11:14:38 -0800 | [diff] [blame] | 97 | |
Sunny Goyal | b80941b | 2019-06-19 21:30:40 -0700 | [diff] [blame] | 98 | public static final FloatProperty<View> VIEW_TRANSLATE_X = |
| 99 | View.TRANSLATION_X instanceof FloatProperty ? (FloatProperty) View.TRANSLATION_X |
| 100 | : new FloatProperty<View>("translateX") { |
| 101 | @Override |
| 102 | public void setValue(View view, float v) { |
| 103 | view.setTranslationX(v); |
| 104 | } |
Jon Miranda | 86f6c44 | 2019-01-29 11:14:38 -0800 | [diff] [blame] | 105 | |
Sunny Goyal | b80941b | 2019-06-19 21:30:40 -0700 | [diff] [blame] | 106 | @Override |
| 107 | public Float get(View view) { |
| 108 | return view.getTranslationX(); |
| 109 | } |
| 110 | }; |
Jon Miranda | 86f6c44 | 2019-01-29 11:14:38 -0800 | [diff] [blame] | 111 | |
Sunny Goyal | b80941b | 2019-06-19 21:30:40 -0700 | [diff] [blame] | 112 | public static final FloatProperty<View> VIEW_TRANSLATE_Y = |
| 113 | View.TRANSLATION_Y instanceof FloatProperty ? (FloatProperty) View.TRANSLATION_Y |
| 114 | : new FloatProperty<View>("translateY") { |
| 115 | @Override |
| 116 | public void setValue(View view, float v) { |
| 117 | view.setTranslationY(v); |
| 118 | } |
Jon Miranda | 86f6c44 | 2019-01-29 11:14:38 -0800 | [diff] [blame] | 119 | |
Sunny Goyal | b80941b | 2019-06-19 21:30:40 -0700 | [diff] [blame] | 120 | @Override |
| 121 | public Float get(View view) { |
| 122 | return view.getTranslationY(); |
| 123 | } |
| 124 | }; |
Sunny Goyal | dbcc63e | 2020-03-06 15:35:55 -0800 | [diff] [blame] | 125 | |
| 126 | public static final FloatProperty<View> VIEW_ALPHA = |
| 127 | View.ALPHA instanceof FloatProperty ? (FloatProperty) View.ALPHA |
| 128 | : new FloatProperty<View>("alpha") { |
| 129 | @Override |
| 130 | public void setValue(View view, float v) { |
| 131 | view.setAlpha(v); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public Float get(View view) { |
| 136 | return view.getAlpha(); |
| 137 | } |
| 138 | }; |
Sunny Goyal | ff9e7d6 | 2020-10-21 15:42:05 -0700 | [diff] [blame] | 139 | |
Zak Cohen | 4d35ac3 | 2021-04-23 16:28:12 -0700 | [diff] [blame] | 140 | public static final IntProperty<View> VIEW_BACKGROUND_COLOR = |
| 141 | new IntProperty<View>("backgroundColor") { |
| 142 | @Override |
| 143 | public void setValue(View view, int color) { |
| 144 | view.setBackgroundColor(color); |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public Integer get(View view) { |
| 149 | if (!(view.getBackground() instanceof ColorDrawable)) { |
| 150 | return Color.TRANSPARENT; |
| 151 | } |
| 152 | return ((ColorDrawable) view.getBackground()).getColor(); |
| 153 | } |
| 154 | }; |
| 155 | |
Sunny Goyal | ff9e7d6 | 2020-10-21 15:42:05 -0700 | [diff] [blame] | 156 | /** |
| 157 | * Utility method to create an {@link AnimatorListener} which executes a callback on animation |
| 158 | * cancel. |
| 159 | */ |
| 160 | public static AnimatorListener newCancelListener(Runnable callback) { |
| 161 | return new AnimatorListenerAdapter() { |
| 162 | |
| 163 | boolean mDispatched = false; |
| 164 | |
| 165 | @Override |
| 166 | public void onAnimationCancel(Animator animation) { |
| 167 | if (!mDispatched) { |
| 168 | mDispatched = true; |
| 169 | callback.run(); |
| 170 | } |
| 171 | } |
| 172 | }; |
| 173 | } |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 174 | } |