blob: 03ffded7201bc399da5eea925c3c9ea16178f6b4 [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
Tony Wickham0f640b62018-05-09 16:00:14 -070042 // The progress of an animation to all apps must be at least this far along to snap to all apps.
43 public static final float MIN_PROGRESS_TO_ALL_APPS = 0.5f;
44
Michael Jurka7c70d642013-10-23 15:21:32 +020045 static WeakHashMap<Animator, Object> sAnimators = new WeakHashMap<Animator, Object>();
Michael Jurkadef8e652012-06-29 15:38:55 -070046 static Animator.AnimatorListener sEndAnimListener = new Animator.AnimatorListener() {
Michael Jurka2ecf9952012-06-18 12:52:28 -070047 public void onAnimationStart(Animator animation) {
Michael Jurka7c70d642013-10-23 15:21:32 +020048 sAnimators.put(animation, null);
Michael Jurka2ecf9952012-06-18 12:52:28 -070049 }
50
51 public void onAnimationRepeat(Animator animation) {
52 }
53
54 public void onAnimationEnd(Animator animation) {
55 sAnimators.remove(animation);
56 }
57
58 public void onAnimationCancel(Animator animation) {
59 sAnimators.remove(animation);
60 }
61 };
62
63 public static void cancelOnDestroyActivity(Animator a) {
Michael Jurkadef8e652012-06-29 15:38:55 -070064 a.addListener(sEndAnimListener);
Michael Jurka2ecf9952012-06-18 12:52:28 -070065 }
66
Michael Jurkaf1ad6082013-03-13 12:55:46 +010067 // Helper method. Assumes a draw is pending, and that if the animation's duration is 0
68 // it should be cancelled
69 public static void startAnimationAfterNextDraw(final Animator animator, final View view) {
Michael Jurkadf96add2013-04-03 16:25:02 -070070 view.getViewTreeObserver().addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Tony Wickham0bb211a2015-10-02 16:22:08 -070071 private boolean mStarted = false;
Michael Jurkadf96add2013-04-03 16:25:02 -070072
Tony Wickham0bb211a2015-10-02 16:22:08 -070073 public void onDraw() {
74 if (mStarted) return;
75 mStarted = true;
76 // Use this as a signal that the animation was cancelled
77 if (animator.getDuration() == 0) {
78 return;
Michael Jurkaf1ad6082013-03-13 12:55:46 +010079 }
Tony Wickham0bb211a2015-10-02 16:22:08 -070080 animator.start();
81
82 final ViewTreeObserver.OnDrawListener listener = this;
83 view.post(new Runnable() {
84 public void run() {
85 view.getViewTreeObserver().removeOnDrawListener(listener);
86 }
87 });
88 }
89 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +010090 }
91
Michael Jurka2ecf9952012-06-18 12:52:28 -070092 public static void onDestroyActivity() {
Michael Jurka7c70d642013-10-23 15:21:32 +020093 HashSet<Animator> animators = new HashSet<Animator>(sAnimators.keySet());
Winson Chung15ba53a2012-07-12 11:21:32 -070094 for (Animator a : animators) {
Michael Jurka2ecf9952012-06-18 12:52:28 -070095 if (a.isRunning()) {
96 a.cancel();
Michael Jurka2ecf9952012-06-18 12:52:28 -070097 }
Michael Jurka7c70d642013-10-23 15:21:32 +020098 sAnimators.remove(a);
Michael Jurka2ecf9952012-06-18 12:52:28 -070099 }
100 }
101
102 public static AnimatorSet createAnimatorSet() {
103 AnimatorSet anim = new AnimatorSet();
104 cancelOnDestroyActivity(anim);
105 return anim;
106 }
107
Jon Mirandacda3bfb2017-02-06 15:54:41 -0800108 public static ValueAnimator ofFloat(float... values) {
Michael Jurka2ecf9952012-06-18 12:52:28 -0700109 ValueAnimator anim = new ValueAnimator();
110 anim.setFloatValues(values);
111 cancelOnDestroyActivity(anim);
112 return anim;
113 }
114
Sunny Goyal5d2fc322015-07-06 22:52:49 -0700115 public static ObjectAnimator ofFloat(View target, Property<View, Float> property,
116 float... values) {
117 ObjectAnimator anim = ObjectAnimator.ofFloat(target, property, values);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700118 cancelOnDestroyActivity(anim);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100119 new FirstFrameAnimatorHelper(anim, target);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700120 return anim;
121 }
122
Sunny Goyal5d2fc322015-07-06 22:52:49 -0700123 public static ObjectAnimator ofViewAlphaAndScale(View target,
124 float alpha, float scaleX, float scaleY) {
125 return ofPropertyValuesHolder(target,
126 PropertyValuesHolder.ofFloat(View.ALPHA, alpha),
127 PropertyValuesHolder.ofFloat(View.SCALE_X, scaleX),
128 PropertyValuesHolder.ofFloat(View.SCALE_Y, scaleY));
129 }
130
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100131 public static ObjectAnimator ofPropertyValuesHolder(View target,
Michael Jurka2ecf9952012-06-18 12:52:28 -0700132 PropertyValuesHolder... values) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -0700133 return ofPropertyValuesHolder(target, target, values);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100134 }
135
136 public static ObjectAnimator ofPropertyValuesHolder(Object target,
137 View view, PropertyValuesHolder... values) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -0700138 ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(target, values);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100139 cancelOnDestroyActivity(anim);
140 new FirstFrameAnimatorHelper(anim, view);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700141 return anim;
142 }
Tony Wickham9438ed42017-01-20 09:38:25 -0800143
Jon Miranda2d89ea82017-05-04 11:47:53 -0700144 public static final Property<Drawable, Integer> DRAWABLE_ALPHA =
145 new Property<Drawable, Integer>(Integer.TYPE, "drawableAlpha") {
146 @Override
147 public Integer get(Drawable drawable) {
148 return drawable.getAlpha();
149 }
150
151 @Override
152 public void set(Drawable drawable, Integer alpha) {
153 drawable.setAlpha(alpha);
154 }
155 };
Sunny Goyalaeb16432017-10-16 11:46:41 -0700156
157 public static final Property<View, Float> SCALE_PROPERTY =
158 new Property<View, Float>(Float.class, "scale") {
159 @Override
160 public Float get(View view) {
161 return view.getScaleX();
162 }
163
164 @Override
165 public void set(View view, Float scale) {
166 view.setScaleX(scale);
167 view.setScaleY(scale);
168 }
169 };
Sunny Goyalea529082017-10-31 13:33:03 -0700170
Tony Wickham0f640b62018-05-09 16:00:14 -0700171 /** Increase the duration if we prevented the fling, as we are going against a high velocity. */
172 public static int blockedFlingDurationFactor(float velocity) {
173 return (int) Utilities.boundToRange(Math.abs(velocity) / 2, 2f, 6f);
174 }
Michael Jurka2ecf9952012-06-18 12:52:28 -0700175}