blob: b56c0127ffb4bdd94fd48e972bf3481d1c90df26 [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
30public class LauncherAnimUtils {
Sunny Goyalaeb16432017-10-16 11:46:41 -070031 /**
32 * Durations for various state animations. These are not defined in resources to allow
33 * easier access from static classes and enums
34 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -070035 public static final int SPRING_LOADED_EXIT_DELAY = 500;
Sunny Goyalaeb16432017-10-16 11:46:41 -070036
Sunny Goyalff9e7d62020-10-21 15:42:05 -070037 // Progress after which the transition is assumed to be a success
38 public static final float SUCCESS_TRANSITION_PROGRESS = 0.5f;
Tony Wickham0f640b62018-05-09 16:00:14 -070039
Sunny Goyaldbcc63e2020-03-06 15:35:55 -080040 public static final IntProperty<Drawable> DRAWABLE_ALPHA =
41 new IntProperty<Drawable>("drawableAlpha") {
Jon Miranda2d89ea82017-05-04 11:47:53 -070042 @Override
43 public Integer get(Drawable drawable) {
44 return drawable.getAlpha();
45 }
46
47 @Override
Sunny Goyaldbcc63e2020-03-06 15:35:55 -080048 public void setValue(Drawable drawable, int alpha) {
Jon Miranda2d89ea82017-05-04 11:47:53 -070049 drawable.setAlpha(alpha);
50 }
51 };
Sunny Goyalaeb16432017-10-16 11:46:41 -070052
Sunny Goyalb80941b2019-06-19 21:30:40 -070053 public static final FloatProperty<View> SCALE_PROPERTY =
54 new FloatProperty<View>("scale") {
Sunny Goyalaeb16432017-10-16 11:46:41 -070055 @Override
56 public Float get(View view) {
57 return view.getScaleX();
58 }
59
60 @Override
Sunny Goyalb80941b2019-06-19 21:30:40 -070061 public void setValue(View view, float scale) {
Sunny Goyalaeb16432017-10-16 11:46:41 -070062 view.setScaleX(scale);
63 view.setScaleY(scale);
64 }
65 };
Sunny Goyalea529082017-10-31 13:33:03 -070066
Tony Wickham0f640b62018-05-09 16:00:14 -070067 /** 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 Goyal55bdeed2018-09-18 16:17:22 -070071
Sunny Goyaldbcc63e2020-03-06 15:35:55 -080072 public static final IntProperty<LayoutParams> LAYOUT_WIDTH =
73 new IntProperty<LayoutParams>("width") {
Sunny Goyal55bdeed2018-09-18 16:17:22 -070074 @Override
75 public Integer get(LayoutParams lp) {
76 return lp.width;
77 }
78
79 @Override
Sunny Goyaldbcc63e2020-03-06 15:35:55 -080080 public void setValue(LayoutParams lp, int width) {
Sunny Goyal55bdeed2018-09-18 16:17:22 -070081 lp.width = width;
82 }
83 };
84
Sunny Goyaldbcc63e2020-03-06 15:35:55 -080085 public static final IntProperty<LayoutParams> LAYOUT_HEIGHT =
86 new IntProperty<LayoutParams>("height") {
Sunny Goyal55bdeed2018-09-18 16:17:22 -070087 @Override
88 public Integer get(LayoutParams lp) {
89 return lp.height;
90 }
91
92 @Override
Sunny Goyaldbcc63e2020-03-06 15:35:55 -080093 public void setValue(LayoutParams lp, int height) {
Sunny Goyal55bdeed2018-09-18 16:17:22 -070094 lp.height = height;
95 }
96 };
Jon Miranda86f6c442019-01-29 11:14:38 -080097
Sunny Goyalb80941b2019-06-19 21:30:40 -070098 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 Miranda86f6c442019-01-29 11:14:38 -0800105
Sunny Goyalb80941b2019-06-19 21:30:40 -0700106 @Override
107 public Float get(View view) {
108 return view.getTranslationX();
109 }
110 };
Jon Miranda86f6c442019-01-29 11:14:38 -0800111
Sunny Goyalb80941b2019-06-19 21:30:40 -0700112 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 Miranda86f6c442019-01-29 11:14:38 -0800119
Sunny Goyalb80941b2019-06-19 21:30:40 -0700120 @Override
121 public Float get(View view) {
122 return view.getTranslationY();
123 }
124 };
Sunny Goyaldbcc63e2020-03-06 15:35:55 -0800125
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 Goyalff9e7d62020-10-21 15:42:05 -0700139
Zak Cohen4d35ac32021-04-23 16:28:12 -0700140 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 Goyalff9e7d62020-10-21 15:42:05 -0700156 /**
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 Jurka2ecf9952012-06-18 12:52:28 -0700174}