blob: 01f72a7ce5152b6d15fa33ad57b1be104332c569 [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;
Michael Jurkaf1ad6082013-03-13 12:55:46 +010024import android.view.View;
25import android.view.ViewTreeObserver;
Michael Jurka2ecf9952012-06-18 12:52:28 -070026
27import java.util.HashSet;
28
29public class LauncherAnimUtils {
30 static HashSet<Animator> sAnimators = new HashSet<Animator>();
Michael Jurkadef8e652012-06-29 15:38:55 -070031 static Animator.AnimatorListener sEndAnimListener = new Animator.AnimatorListener() {
Michael Jurka2ecf9952012-06-18 12:52:28 -070032 public void onAnimationStart(Animator animation) {
33 }
34
35 public void onAnimationRepeat(Animator animation) {
36 }
37
38 public void onAnimationEnd(Animator animation) {
39 sAnimators.remove(animation);
40 }
41
42 public void onAnimationCancel(Animator animation) {
43 sAnimators.remove(animation);
44 }
45 };
46
47 public static void cancelOnDestroyActivity(Animator a) {
48 sAnimators.add(a);
Michael Jurkadef8e652012-06-29 15:38:55 -070049 a.addListener(sEndAnimListener);
Michael Jurka2ecf9952012-06-18 12:52:28 -070050 }
51
Michael Jurkaf1ad6082013-03-13 12:55:46 +010052 // Helper method. Assumes a draw is pending, and that if the animation's duration is 0
53 // it should be cancelled
54 public static void startAnimationAfterNextDraw(final Animator animator, final View view) {
Michael Jurkadf96add2013-04-03 16:25:02 -070055 view.getViewTreeObserver().addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
56 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +010057 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -070058 if (mStarted) return;
59 mStarted = true;
Michael Jurkaf1ad6082013-03-13 12:55:46 +010060 // Use this as a signal that the animation was cancelled
61 if (animator.getDuration() == 0) {
62 return;
63 }
64 animator.start();
Michael Jurkadf96add2013-04-03 16:25:02 -070065
66 final ViewTreeObserver.OnDrawListener listener = this;
67 view.post(new Runnable() {
68 public void run() {
69 view.getViewTreeObserver().removeOnDrawListener(listener);
70 }
71 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +010072 }
73 });
74 }
75
Michael Jurka2ecf9952012-06-18 12:52:28 -070076 public static void onDestroyActivity() {
Winson Chung15ba53a2012-07-12 11:21:32 -070077 HashSet<Animator> animators = new HashSet<Animator>(sAnimators);
78 for (Animator a : animators) {
Michael Jurka2ecf9952012-06-18 12:52:28 -070079 if (a.isRunning()) {
80 a.cancel();
Winson Chungb8b2a5a2012-07-12 17:55:31 -070081 } else {
82 sAnimators.remove(a);
Michael Jurka2ecf9952012-06-18 12:52:28 -070083 }
84 }
85 }
86
87 public static AnimatorSet createAnimatorSet() {
88 AnimatorSet anim = new AnimatorSet();
89 cancelOnDestroyActivity(anim);
90 return anim;
91 }
92
Michael Jurkaf1ad6082013-03-13 12:55:46 +010093 public static ValueAnimator ofFloat(View target, float... values) {
Michael Jurka2ecf9952012-06-18 12:52:28 -070094 ValueAnimator anim = new ValueAnimator();
95 anim.setFloatValues(values);
96 cancelOnDestroyActivity(anim);
97 return anim;
98 }
99
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100100 public static ObjectAnimator ofFloat(View target, String propertyName, float... values) {
Michael Jurka2ecf9952012-06-18 12:52:28 -0700101 ObjectAnimator anim = new ObjectAnimator();
102 anim.setTarget(target);
103 anim.setPropertyName(propertyName);
104 anim.setFloatValues(values);
105 cancelOnDestroyActivity(anim);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100106 new FirstFrameAnimatorHelper(anim, target);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700107 return anim;
108 }
109
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100110 public static ObjectAnimator ofPropertyValuesHolder(View target,
Michael Jurka2ecf9952012-06-18 12:52:28 -0700111 PropertyValuesHolder... values) {
112 ObjectAnimator anim = new ObjectAnimator();
113 anim.setTarget(target);
114 anim.setValues(values);
115 cancelOnDestroyActivity(anim);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100116 new FirstFrameAnimatorHelper(anim, target);
117 return anim;
118 }
119
120 public static ObjectAnimator ofPropertyValuesHolder(Object target,
121 View view, PropertyValuesHolder... values) {
122 ObjectAnimator anim = new ObjectAnimator();
123 anim.setTarget(target);
124 anim.setValues(values);
125 cancelOnDestroyActivity(anim);
126 new FirstFrameAnimatorHelper(anim, view);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700127 return anim;
128 }
129}