blob: 9ea277c13af9edbac3457f5aaedd1979530fecf5 [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;
Sunny Goyal5d2fc322015-07-06 22:52:49 -070024import android.util.Property;
Michael Jurkaf1ad6082013-03-13 12:55:46 +010025import android.view.View;
Tony Wickham9438ed42017-01-20 09:38:25 -080026import android.view.ViewGroup;
Michael Jurkaf1ad6082013-03-13 12:55:46 +010027import android.view.ViewTreeObserver;
Tony Wickham9438ed42017-01-20 09:38:25 -080028import android.widget.ViewAnimator;
Adam Cohen15588932015-05-26 23:03:31 -070029
Michael Jurka2ecf9952012-06-18 12:52:28 -070030import java.util.HashSet;
Michael Jurka7c70d642013-10-23 15:21:32 +020031import java.util.WeakHashMap;
Michael Jurka2ecf9952012-06-18 12:52:28 -070032
33public class LauncherAnimUtils {
Michael Jurka7c70d642013-10-23 15:21:32 +020034 static WeakHashMap<Animator, Object> sAnimators = new WeakHashMap<Animator, Object>();
Michael Jurkadef8e652012-06-29 15:38:55 -070035 static Animator.AnimatorListener sEndAnimListener = new Animator.AnimatorListener() {
Michael Jurka2ecf9952012-06-18 12:52:28 -070036 public void onAnimationStart(Animator animation) {
Michael Jurka7c70d642013-10-23 15:21:32 +020037 sAnimators.put(animation, null);
Michael Jurka2ecf9952012-06-18 12:52:28 -070038 }
39
40 public void onAnimationRepeat(Animator animation) {
41 }
42
43 public void onAnimationEnd(Animator animation) {
44 sAnimators.remove(animation);
45 }
46
47 public void onAnimationCancel(Animator animation) {
48 sAnimators.remove(animation);
49 }
50 };
51
52 public static void cancelOnDestroyActivity(Animator a) {
Michael Jurkadef8e652012-06-29 15:38:55 -070053 a.addListener(sEndAnimListener);
Michael Jurka2ecf9952012-06-18 12:52:28 -070054 }
55
Michael Jurkaf1ad6082013-03-13 12:55:46 +010056 // Helper method. Assumes a draw is pending, and that if the animation's duration is 0
57 // it should be cancelled
58 public static void startAnimationAfterNextDraw(final Animator animator, final View view) {
Michael Jurkadf96add2013-04-03 16:25:02 -070059 view.getViewTreeObserver().addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Tony Wickham0bb211a2015-10-02 16:22:08 -070060 private boolean mStarted = false;
Michael Jurkadf96add2013-04-03 16:25:02 -070061
Tony Wickham0bb211a2015-10-02 16:22:08 -070062 public void onDraw() {
63 if (mStarted) return;
64 mStarted = true;
65 // Use this as a signal that the animation was cancelled
66 if (animator.getDuration() == 0) {
67 return;
Michael Jurkaf1ad6082013-03-13 12:55:46 +010068 }
Tony Wickham0bb211a2015-10-02 16:22:08 -070069 animator.start();
70
71 final ViewTreeObserver.OnDrawListener listener = this;
72 view.post(new Runnable() {
73 public void run() {
74 view.getViewTreeObserver().removeOnDrawListener(listener);
75 }
76 });
77 }
78 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +010079 }
80
Michael Jurka2ecf9952012-06-18 12:52:28 -070081 public static void onDestroyActivity() {
Michael Jurka7c70d642013-10-23 15:21:32 +020082 HashSet<Animator> animators = new HashSet<Animator>(sAnimators.keySet());
Winson Chung15ba53a2012-07-12 11:21:32 -070083 for (Animator a : animators) {
Michael Jurka2ecf9952012-06-18 12:52:28 -070084 if (a.isRunning()) {
85 a.cancel();
Michael Jurka2ecf9952012-06-18 12:52:28 -070086 }
Michael Jurka7c70d642013-10-23 15:21:32 +020087 sAnimators.remove(a);
Michael Jurka2ecf9952012-06-18 12:52:28 -070088 }
89 }
90
91 public static AnimatorSet createAnimatorSet() {
92 AnimatorSet anim = new AnimatorSet();
93 cancelOnDestroyActivity(anim);
94 return anim;
95 }
96
Michael Jurkaf1ad6082013-03-13 12:55:46 +010097 public static ValueAnimator ofFloat(View target, float... values) {
Michael Jurka2ecf9952012-06-18 12:52:28 -070098 ValueAnimator anim = new ValueAnimator();
99 anim.setFloatValues(values);
100 cancelOnDestroyActivity(anim);
101 return anim;
102 }
103
Sunny Goyal5d2fc322015-07-06 22:52:49 -0700104 public static ObjectAnimator ofFloat(View target, Property<View, Float> property,
105 float... values) {
106 ObjectAnimator anim = ObjectAnimator.ofFloat(target, property, values);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700107 cancelOnDestroyActivity(anim);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100108 new FirstFrameAnimatorHelper(anim, target);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700109 return anim;
110 }
111
Sunny Goyal5d2fc322015-07-06 22:52:49 -0700112 public static ObjectAnimator ofViewAlphaAndScale(View target,
113 float alpha, float scaleX, float scaleY) {
114 return ofPropertyValuesHolder(target,
115 PropertyValuesHolder.ofFloat(View.ALPHA, alpha),
116 PropertyValuesHolder.ofFloat(View.SCALE_X, scaleX),
117 PropertyValuesHolder.ofFloat(View.SCALE_Y, scaleY));
118 }
119
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100120 public static ObjectAnimator ofPropertyValuesHolder(View target,
Michael Jurka2ecf9952012-06-18 12:52:28 -0700121 PropertyValuesHolder... values) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -0700122 return ofPropertyValuesHolder(target, target, values);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100123 }
124
125 public static ObjectAnimator ofPropertyValuesHolder(Object target,
126 View view, PropertyValuesHolder... values) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -0700127 ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(target, values);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100128 cancelOnDestroyActivity(anim);
129 new FirstFrameAnimatorHelper(anim, view);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700130 return anim;
131 }
Tony Wickham9438ed42017-01-20 09:38:25 -0800132
133 public static ValueAnimator animateViewHeight(final View v, int fromHeight, int toHeight) {
134 ValueAnimator anim = ValueAnimator.ofInt(fromHeight, toHeight);
135 anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
136 @Override
137 public void onAnimationUpdate(ValueAnimator valueAnimator) {
138 int val = (Integer) valueAnimator.getAnimatedValue();
139 ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
140 layoutParams.height = val;
141 v.setLayoutParams(layoutParams);
142 }
143 });
144 return anim;
145 }
Michael Jurka2ecf9952012-06-18 12:52:28 -0700146}