blob: 5055d35054d479c50705d906fc8441ec87aa5d24 [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
17package com.android.launcher2;
18
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) {
55 final ViewTreeObserver observer = view.getViewTreeObserver();
56 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
57 public void onDraw() {
58 // Use this as a signal that the animation was cancelled
59 if (animator.getDuration() == 0) {
60 return;
61 }
62 animator.start();
63 view.getViewTreeObserver().removeOnDrawListener(this);
64 }
65 });
66 }
67
Michael Jurka2ecf9952012-06-18 12:52:28 -070068 public static void onDestroyActivity() {
Winson Chung15ba53a2012-07-12 11:21:32 -070069 HashSet<Animator> animators = new HashSet<Animator>(sAnimators);
70 for (Animator a : animators) {
Michael Jurka2ecf9952012-06-18 12:52:28 -070071 if (a.isRunning()) {
72 a.cancel();
Winson Chungb8b2a5a2012-07-12 17:55:31 -070073 } else {
74 sAnimators.remove(a);
Michael Jurka2ecf9952012-06-18 12:52:28 -070075 }
76 }
77 }
78
79 public static AnimatorSet createAnimatorSet() {
80 AnimatorSet anim = new AnimatorSet();
81 cancelOnDestroyActivity(anim);
82 return anim;
83 }
84
Michael Jurkaf1ad6082013-03-13 12:55:46 +010085 public static ValueAnimator ofFloat(View target, float... values) {
Michael Jurka2ecf9952012-06-18 12:52:28 -070086 ValueAnimator anim = new ValueAnimator();
87 anim.setFloatValues(values);
88 cancelOnDestroyActivity(anim);
89 return anim;
90 }
91
Michael Jurkaf1ad6082013-03-13 12:55:46 +010092 public static ObjectAnimator ofFloat(View target, String propertyName, float... values) {
Michael Jurka2ecf9952012-06-18 12:52:28 -070093 ObjectAnimator anim = new ObjectAnimator();
94 anim.setTarget(target);
95 anim.setPropertyName(propertyName);
96 anim.setFloatValues(values);
97 cancelOnDestroyActivity(anim);
Michael Jurkaf1ad6082013-03-13 12:55:46 +010098 new FirstFrameAnimatorHelper(anim, target);
Michael Jurka2ecf9952012-06-18 12:52:28 -070099 return anim;
100 }
101
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100102 public static ObjectAnimator ofPropertyValuesHolder(View target,
Michael Jurka2ecf9952012-06-18 12:52:28 -0700103 PropertyValuesHolder... values) {
104 ObjectAnimator anim = new ObjectAnimator();
105 anim.setTarget(target);
106 anim.setValues(values);
107 cancelOnDestroyActivity(anim);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100108 new FirstFrameAnimatorHelper(anim, target);
109 return anim;
110 }
111
112 public static ObjectAnimator ofPropertyValuesHolder(Object target,
113 View view, PropertyValuesHolder... values) {
114 ObjectAnimator anim = new ObjectAnimator();
115 anim.setTarget(target);
116 anim.setValues(values);
117 cancelOnDestroyActivity(anim);
118 new FirstFrameAnimatorHelper(anim, view);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700119 return anim;
120 }
121}