blob: 9317c31b6d560cb017ddb5cf620882914e884b3c [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;
24
25import java.util.HashSet;
26
27public class LauncherAnimUtils {
28 static HashSet<Animator> sAnimators = new HashSet<Animator>();
Michael Jurkadef8e652012-06-29 15:38:55 -070029 static Animator.AnimatorListener sEndAnimListener = new Animator.AnimatorListener() {
Michael Jurka2ecf9952012-06-18 12:52:28 -070030 public void onAnimationStart(Animator animation) {
31 }
32
33 public void onAnimationRepeat(Animator animation) {
34 }
35
36 public void onAnimationEnd(Animator animation) {
37 sAnimators.remove(animation);
38 }
39
40 public void onAnimationCancel(Animator animation) {
41 sAnimators.remove(animation);
42 }
43 };
44
45 public static void cancelOnDestroyActivity(Animator a) {
46 sAnimators.add(a);
Michael Jurkadef8e652012-06-29 15:38:55 -070047 a.addListener(sEndAnimListener);
Michael Jurka2ecf9952012-06-18 12:52:28 -070048 }
49
50 public static void onDestroyActivity() {
Winson Chung15ba53a2012-07-12 11:21:32 -070051 HashSet<Animator> animators = new HashSet<Animator>(sAnimators);
52 for (Animator a : animators) {
Michael Jurka2ecf9952012-06-18 12:52:28 -070053 if (a.isRunning()) {
54 a.cancel();
Winson Chungb8b2a5a2012-07-12 17:55:31 -070055 } else {
56 sAnimators.remove(a);
Michael Jurka2ecf9952012-06-18 12:52:28 -070057 }
58 }
59 }
60
61 public static AnimatorSet createAnimatorSet() {
62 AnimatorSet anim = new AnimatorSet();
63 cancelOnDestroyActivity(anim);
64 return anim;
65 }
66
67 public static ValueAnimator ofFloat(float... values) {
68 ValueAnimator anim = new ValueAnimator();
69 anim.setFloatValues(values);
70 cancelOnDestroyActivity(anim);
71 return anim;
72 }
73
74 public static ObjectAnimator ofFloat(Object target, String propertyName, float... values) {
75 ObjectAnimator anim = new ObjectAnimator();
76 anim.setTarget(target);
77 anim.setPropertyName(propertyName);
78 anim.setFloatValues(values);
79 cancelOnDestroyActivity(anim);
80 return anim;
81 }
82
83 public static ObjectAnimator ofPropertyValuesHolder(Object target,
84 PropertyValuesHolder... values) {
85 ObjectAnimator anim = new ObjectAnimator();
86 anim.setTarget(target);
87 anim.setValues(values);
88 cancelOnDestroyActivity(anim);
89 return anim;
90 }
91}