blob: 803f8d2c3df25f61181589c7442f789ada6ac182 [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;
Jon Miranda2d89ea82017-05-04 11:47:53 -070022import android.graphics.drawable.Drawable;
Sunny Goyalb80941b2019-06-19 21:30:40 -070023import android.util.FloatProperty;
Sunny Goyaldbcc63e2020-03-06 15:35:55 -080024import android.util.IntProperty;
Michael Jurkaf1ad6082013-03-13 12:55:46 +010025import android.view.View;
Sunny Goyal55bdeed2018-09-18 16:17:22 -070026import android.view.ViewGroup.LayoutParams;
Michael Jurka2ecf9952012-06-18 12:52:28 -070027
28public class LauncherAnimUtils {
Sunny Goyalaeb16432017-10-16 11:46:41 -070029 /**
30 * Durations for various state animations. These are not defined in resources to allow
31 * easier access from static classes and enums
32 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -070033 public static final int SPRING_LOADED_EXIT_DELAY = 500;
Sunny Goyalaeb16432017-10-16 11:46:41 -070034
Sunny Goyalff9e7d62020-10-21 15:42:05 -070035 // Progress after which the transition is assumed to be a success
36 public static final float SUCCESS_TRANSITION_PROGRESS = 0.5f;
Tony Wickham0f640b62018-05-09 16:00:14 -070037
Sunny Goyaldbcc63e2020-03-06 15:35:55 -080038 public static final IntProperty<Drawable> DRAWABLE_ALPHA =
39 new IntProperty<Drawable>("drawableAlpha") {
Jon Miranda2d89ea82017-05-04 11:47:53 -070040 @Override
41 public Integer get(Drawable drawable) {
42 return drawable.getAlpha();
43 }
44
45 @Override
Sunny Goyaldbcc63e2020-03-06 15:35:55 -080046 public void setValue(Drawable drawable, int alpha) {
Jon Miranda2d89ea82017-05-04 11:47:53 -070047 drawable.setAlpha(alpha);
48 }
49 };
Sunny Goyalaeb16432017-10-16 11:46:41 -070050
Sunny Goyalb80941b2019-06-19 21:30:40 -070051 public static final FloatProperty<View> SCALE_PROPERTY =
52 new FloatProperty<View>("scale") {
Sunny Goyalaeb16432017-10-16 11:46:41 -070053 @Override
54 public Float get(View view) {
55 return view.getScaleX();
56 }
57
58 @Override
Sunny Goyalb80941b2019-06-19 21:30:40 -070059 public void setValue(View view, float scale) {
Sunny Goyalaeb16432017-10-16 11:46:41 -070060 view.setScaleX(scale);
61 view.setScaleY(scale);
62 }
63 };
Sunny Goyalea529082017-10-31 13:33:03 -070064
Tony Wickham0f640b62018-05-09 16:00:14 -070065 /** 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 Goyal55bdeed2018-09-18 16:17:22 -070069
Sunny Goyaldbcc63e2020-03-06 15:35:55 -080070 public static final IntProperty<LayoutParams> LAYOUT_WIDTH =
71 new IntProperty<LayoutParams>("width") {
Sunny Goyal55bdeed2018-09-18 16:17:22 -070072 @Override
73 public Integer get(LayoutParams lp) {
74 return lp.width;
75 }
76
77 @Override
Sunny Goyaldbcc63e2020-03-06 15:35:55 -080078 public void setValue(LayoutParams lp, int width) {
Sunny Goyal55bdeed2018-09-18 16:17:22 -070079 lp.width = width;
80 }
81 };
82
Sunny Goyaldbcc63e2020-03-06 15:35:55 -080083 public static final IntProperty<LayoutParams> LAYOUT_HEIGHT =
84 new IntProperty<LayoutParams>("height") {
Sunny Goyal55bdeed2018-09-18 16:17:22 -070085 @Override
86 public Integer get(LayoutParams lp) {
87 return lp.height;
88 }
89
90 @Override
Sunny Goyaldbcc63e2020-03-06 15:35:55 -080091 public void setValue(LayoutParams lp, int height) {
Sunny Goyal55bdeed2018-09-18 16:17:22 -070092 lp.height = height;
93 }
94 };
Jon Miranda86f6c442019-01-29 11:14:38 -080095
Sunny Goyalb80941b2019-06-19 21:30:40 -070096 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 Miranda86f6c442019-01-29 11:14:38 -0800103
Sunny Goyalb80941b2019-06-19 21:30:40 -0700104 @Override
105 public Float get(View view) {
106 return view.getTranslationX();
107 }
108 };
Jon Miranda86f6c442019-01-29 11:14:38 -0800109
Sunny Goyalb80941b2019-06-19 21:30:40 -0700110 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 Miranda86f6c442019-01-29 11:14:38 -0800117
Sunny Goyalb80941b2019-06-19 21:30:40 -0700118 @Override
119 public Float get(View view) {
120 return view.getTranslationY();
121 }
122 };
Sunny Goyaldbcc63e2020-03-06 15:35:55 -0800123
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 Goyalff9e7d62020-10-21 15:42:05 -0700137
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 Jurka2ecf9952012-06-18 12:52:28 -0700156}