blob: 9869fdf7e206c29b4f18f4a2595f4029b7fb5b39 [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
19import android.animation.Animator;
20import android.animation.AnimatorSet;
21import android.animation.ObjectAnimator;
22import android.animation.PropertyValuesHolder;
23import android.animation.ValueAnimator;
Jon Miranda2d89ea82017-05-04 11:47:53 -070024import android.graphics.drawable.Drawable;
Sunny Goyal5d2fc322015-07-06 22:52:49 -070025import android.util.Property;
Michael Jurkaf1ad6082013-03-13 12:55:46 +010026import android.view.View;
27import android.view.ViewTreeObserver;
Adam Cohen15588932015-05-26 23:03:31 -070028
Michael Jurka2ecf9952012-06-18 12:52:28 -070029import java.util.HashSet;
Michael Jurka7c70d642013-10-23 15:21:32 +020030import java.util.WeakHashMap;
Michael Jurka2ecf9952012-06-18 12:52:28 -070031
32public class LauncherAnimUtils {
Sunny Goyalaeb16432017-10-16 11:46:41 -070033 /**
34 * Durations for various state animations. These are not defined in resources to allow
35 * easier access from static classes and enums
36 */
37 public static final int ALL_APPS_TRANSITION_MS = 320;
38 public static final int OVERVIEW_TRANSITION_MS = 250;
39 public static final int SPRING_LOADED_TRANSITION_MS = 150;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070040 public static final int SPRING_LOADED_EXIT_DELAY = 500;
Sunny Goyalaeb16432017-10-16 11:46:41 -070041
Michael Jurka7c70d642013-10-23 15:21:32 +020042 static WeakHashMap<Animator, Object> sAnimators = new WeakHashMap<Animator, Object>();
Michael Jurkadef8e652012-06-29 15:38:55 -070043 static Animator.AnimatorListener sEndAnimListener = new Animator.AnimatorListener() {
Michael Jurka2ecf9952012-06-18 12:52:28 -070044 public void onAnimationStart(Animator animation) {
Michael Jurka7c70d642013-10-23 15:21:32 +020045 sAnimators.put(animation, null);
Michael Jurka2ecf9952012-06-18 12:52:28 -070046 }
47
48 public void onAnimationRepeat(Animator animation) {
49 }
50
51 public void onAnimationEnd(Animator animation) {
52 sAnimators.remove(animation);
53 }
54
55 public void onAnimationCancel(Animator animation) {
56 sAnimators.remove(animation);
57 }
58 };
59
60 public static void cancelOnDestroyActivity(Animator a) {
Michael Jurkadef8e652012-06-29 15:38:55 -070061 a.addListener(sEndAnimListener);
Michael Jurka2ecf9952012-06-18 12:52:28 -070062 }
63
Michael Jurkaf1ad6082013-03-13 12:55:46 +010064 // Helper method. Assumes a draw is pending, and that if the animation's duration is 0
65 // it should be cancelled
66 public static void startAnimationAfterNextDraw(final Animator animator, final View view) {
Michael Jurkadf96add2013-04-03 16:25:02 -070067 view.getViewTreeObserver().addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Tony Wickham0bb211a2015-10-02 16:22:08 -070068 private boolean mStarted = false;
Michael Jurkadf96add2013-04-03 16:25:02 -070069
Tony Wickham0bb211a2015-10-02 16:22:08 -070070 public void onDraw() {
71 if (mStarted) return;
72 mStarted = true;
73 // Use this as a signal that the animation was cancelled
74 if (animator.getDuration() == 0) {
75 return;
Michael Jurkaf1ad6082013-03-13 12:55:46 +010076 }
Tony Wickham0bb211a2015-10-02 16:22:08 -070077 animator.start();
78
79 final ViewTreeObserver.OnDrawListener listener = this;
80 view.post(new Runnable() {
81 public void run() {
82 view.getViewTreeObserver().removeOnDrawListener(listener);
83 }
84 });
85 }
86 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +010087 }
88
Michael Jurka2ecf9952012-06-18 12:52:28 -070089 public static void onDestroyActivity() {
Michael Jurka7c70d642013-10-23 15:21:32 +020090 HashSet<Animator> animators = new HashSet<Animator>(sAnimators.keySet());
Winson Chung15ba53a2012-07-12 11:21:32 -070091 for (Animator a : animators) {
Michael Jurka2ecf9952012-06-18 12:52:28 -070092 if (a.isRunning()) {
93 a.cancel();
Michael Jurka2ecf9952012-06-18 12:52:28 -070094 }
Michael Jurka7c70d642013-10-23 15:21:32 +020095 sAnimators.remove(a);
Michael Jurka2ecf9952012-06-18 12:52:28 -070096 }
97 }
98
99 public static AnimatorSet createAnimatorSet() {
100 AnimatorSet anim = new AnimatorSet();
101 cancelOnDestroyActivity(anim);
102 return anim;
103 }
104
Jon Mirandacda3bfb2017-02-06 15:54:41 -0800105 public static ValueAnimator ofFloat(float... values) {
Michael Jurka2ecf9952012-06-18 12:52:28 -0700106 ValueAnimator anim = new ValueAnimator();
107 anim.setFloatValues(values);
108 cancelOnDestroyActivity(anim);
109 return anim;
110 }
111
Sunny Goyal5d2fc322015-07-06 22:52:49 -0700112 public static ObjectAnimator ofFloat(View target, Property<View, Float> property,
113 float... values) {
114 ObjectAnimator anim = ObjectAnimator.ofFloat(target, property, values);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700115 cancelOnDestroyActivity(anim);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100116 new FirstFrameAnimatorHelper(anim, target);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700117 return anim;
118 }
119
Sunny Goyal5d2fc322015-07-06 22:52:49 -0700120 public static ObjectAnimator ofViewAlphaAndScale(View target,
121 float alpha, float scaleX, float scaleY) {
122 return ofPropertyValuesHolder(target,
123 PropertyValuesHolder.ofFloat(View.ALPHA, alpha),
124 PropertyValuesHolder.ofFloat(View.SCALE_X, scaleX),
125 PropertyValuesHolder.ofFloat(View.SCALE_Y, scaleY));
126 }
127
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100128 public static ObjectAnimator ofPropertyValuesHolder(View target,
Michael Jurka2ecf9952012-06-18 12:52:28 -0700129 PropertyValuesHolder... values) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -0700130 return ofPropertyValuesHolder(target, target, values);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100131 }
132
133 public static ObjectAnimator ofPropertyValuesHolder(Object target,
134 View view, PropertyValuesHolder... values) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -0700135 ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(target, values);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100136 cancelOnDestroyActivity(anim);
137 new FirstFrameAnimatorHelper(anim, view);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700138 return anim;
139 }
Tony Wickham9438ed42017-01-20 09:38:25 -0800140
Jon Miranda2d89ea82017-05-04 11:47:53 -0700141 public static final Property<Drawable, Integer> DRAWABLE_ALPHA =
142 new Property<Drawable, Integer>(Integer.TYPE, "drawableAlpha") {
143 @Override
144 public Integer get(Drawable drawable) {
145 return drawable.getAlpha();
146 }
147
148 @Override
149 public void set(Drawable drawable, Integer alpha) {
150 drawable.setAlpha(alpha);
151 }
152 };
Sunny Goyalaeb16432017-10-16 11:46:41 -0700153
154 public static final Property<View, Float> SCALE_PROPERTY =
155 new Property<View, Float>(Float.class, "scale") {
156 @Override
157 public Float get(View view) {
158 return view.getScaleX();
159 }
160
161 @Override
162 public void set(View view, Float scale) {
163 view.setScaleX(scale);
164 view.setScaleY(scale);
165 }
166 };
Sunny Goyalea529082017-10-31 13:33:03 -0700167
168 public static final Property<View, Float> ELEVATION =
169 new Property<View, Float>(Float.class, "elevation") {
170 @Override
171 public Float get(View view) {
172 return view.getElevation();
173 }
174
175 @Override
176 public void set(View view, Float elevation) {
177 view.setElevation(elevation);
178 }
179 };
Michael Jurka2ecf9952012-06-18 12:52:28 -0700180}