blob: 02f05c30e724fd6c62ae217a2fa8ea0229c70722 [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;
40 public static final int SPRING_LOADED_EXIT_SHORT_TIMEOUT = 500;
41 public static final int SPRING_LOADED_EXIT_NEXT_FRAME = 0;
42
Michael Jurka7c70d642013-10-23 15:21:32 +020043 static WeakHashMap<Animator, Object> sAnimators = new WeakHashMap<Animator, Object>();
Michael Jurkadef8e652012-06-29 15:38:55 -070044 static Animator.AnimatorListener sEndAnimListener = new Animator.AnimatorListener() {
Michael Jurka2ecf9952012-06-18 12:52:28 -070045 public void onAnimationStart(Animator animation) {
Michael Jurka7c70d642013-10-23 15:21:32 +020046 sAnimators.put(animation, null);
Michael Jurka2ecf9952012-06-18 12:52:28 -070047 }
48
49 public void onAnimationRepeat(Animator animation) {
50 }
51
52 public void onAnimationEnd(Animator animation) {
53 sAnimators.remove(animation);
54 }
55
56 public void onAnimationCancel(Animator animation) {
57 sAnimators.remove(animation);
58 }
59 };
60
61 public static void cancelOnDestroyActivity(Animator a) {
Michael Jurkadef8e652012-06-29 15:38:55 -070062 a.addListener(sEndAnimListener);
Michael Jurka2ecf9952012-06-18 12:52:28 -070063 }
64
Michael Jurkaf1ad6082013-03-13 12:55:46 +010065 // Helper method. Assumes a draw is pending, and that if the animation's duration is 0
66 // it should be cancelled
67 public static void startAnimationAfterNextDraw(final Animator animator, final View view) {
Michael Jurkadf96add2013-04-03 16:25:02 -070068 view.getViewTreeObserver().addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Tony Wickham0bb211a2015-10-02 16:22:08 -070069 private boolean mStarted = false;
Michael Jurkadf96add2013-04-03 16:25:02 -070070
Tony Wickham0bb211a2015-10-02 16:22:08 -070071 public void onDraw() {
72 if (mStarted) return;
73 mStarted = true;
74 // Use this as a signal that the animation was cancelled
75 if (animator.getDuration() == 0) {
76 return;
Michael Jurkaf1ad6082013-03-13 12:55:46 +010077 }
Tony Wickham0bb211a2015-10-02 16:22:08 -070078 animator.start();
79
80 final ViewTreeObserver.OnDrawListener listener = this;
81 view.post(new Runnable() {
82 public void run() {
83 view.getViewTreeObserver().removeOnDrawListener(listener);
84 }
85 });
86 }
87 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +010088 }
89
Michael Jurka2ecf9952012-06-18 12:52:28 -070090 public static void onDestroyActivity() {
Michael Jurka7c70d642013-10-23 15:21:32 +020091 HashSet<Animator> animators = new HashSet<Animator>(sAnimators.keySet());
Winson Chung15ba53a2012-07-12 11:21:32 -070092 for (Animator a : animators) {
Michael Jurka2ecf9952012-06-18 12:52:28 -070093 if (a.isRunning()) {
94 a.cancel();
Michael Jurka2ecf9952012-06-18 12:52:28 -070095 }
Michael Jurka7c70d642013-10-23 15:21:32 +020096 sAnimators.remove(a);
Michael Jurka2ecf9952012-06-18 12:52:28 -070097 }
98 }
99
100 public static AnimatorSet createAnimatorSet() {
101 AnimatorSet anim = new AnimatorSet();
102 cancelOnDestroyActivity(anim);
103 return anim;
104 }
105
Jon Mirandacda3bfb2017-02-06 15:54:41 -0800106 public static ValueAnimator ofFloat(float... values) {
Michael Jurka2ecf9952012-06-18 12:52:28 -0700107 ValueAnimator anim = new ValueAnimator();
108 anim.setFloatValues(values);
109 cancelOnDestroyActivity(anim);
110 return anim;
111 }
112
Sunny Goyal5d2fc322015-07-06 22:52:49 -0700113 public static ObjectAnimator ofFloat(View target, Property<View, Float> property,
114 float... values) {
115 ObjectAnimator anim = ObjectAnimator.ofFloat(target, property, values);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700116 cancelOnDestroyActivity(anim);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100117 new FirstFrameAnimatorHelper(anim, target);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700118 return anim;
119 }
120
Sunny Goyal5d2fc322015-07-06 22:52:49 -0700121 public static ObjectAnimator ofViewAlphaAndScale(View target,
122 float alpha, float scaleX, float scaleY) {
123 return ofPropertyValuesHolder(target,
124 PropertyValuesHolder.ofFloat(View.ALPHA, alpha),
125 PropertyValuesHolder.ofFloat(View.SCALE_X, scaleX),
126 PropertyValuesHolder.ofFloat(View.SCALE_Y, scaleY));
127 }
128
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100129 public static ObjectAnimator ofPropertyValuesHolder(View target,
Michael Jurka2ecf9952012-06-18 12:52:28 -0700130 PropertyValuesHolder... values) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -0700131 return ofPropertyValuesHolder(target, target, values);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100132 }
133
134 public static ObjectAnimator ofPropertyValuesHolder(Object target,
135 View view, PropertyValuesHolder... values) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -0700136 ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(target, values);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100137 cancelOnDestroyActivity(anim);
138 new FirstFrameAnimatorHelper(anim, view);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700139 return anim;
140 }
Tony Wickham9438ed42017-01-20 09:38:25 -0800141
Jon Miranda2d89ea82017-05-04 11:47:53 -0700142 public static final Property<Drawable, Integer> DRAWABLE_ALPHA =
143 new Property<Drawable, Integer>(Integer.TYPE, "drawableAlpha") {
144 @Override
145 public Integer get(Drawable drawable) {
146 return drawable.getAlpha();
147 }
148
149 @Override
150 public void set(Drawable drawable, Integer alpha) {
151 drawable.setAlpha(alpha);
152 }
153 };
Sunny Goyalaeb16432017-10-16 11:46:41 -0700154
155 public static final Property<View, Float> SCALE_PROPERTY =
156 new Property<View, Float>(Float.class, "scale") {
157 @Override
158 public Float get(View view) {
159 return view.getScaleX();
160 }
161
162 @Override
163 public void set(View view, Float scale) {
164 view.setScaleX(scale);
165 view.setScaleY(scale);
166 }
167 };
Michael Jurka2ecf9952012-06-18 12:52:28 -0700168}