blob: 74fde48b4a15aa5bf14edc93a6c9c69fed4d98eb [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();
55 }
56 }
57 }
58
59 public static AnimatorSet createAnimatorSet() {
60 AnimatorSet anim = new AnimatorSet();
61 cancelOnDestroyActivity(anim);
62 return anim;
63 }
64
65 public static ValueAnimator ofFloat(float... values) {
66 ValueAnimator anim = new ValueAnimator();
67 anim.setFloatValues(values);
68 cancelOnDestroyActivity(anim);
69 return anim;
70 }
71
72 public static ObjectAnimator ofFloat(Object target, String propertyName, float... values) {
73 ObjectAnimator anim = new ObjectAnimator();
74 anim.setTarget(target);
75 anim.setPropertyName(propertyName);
76 anim.setFloatValues(values);
77 cancelOnDestroyActivity(anim);
78 return anim;
79 }
80
81 public static ObjectAnimator ofPropertyValuesHolder(Object target,
82 PropertyValuesHolder... values) {
83 ObjectAnimator anim = new ObjectAnimator();
84 anim.setTarget(target);
85 anim.setValues(values);
86 cancelOnDestroyActivity(anim);
87 return anim;
88 }
89}