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