LauncherAnimUtils.java revision def8e655543384a2837b769d0bc43cdc252cbc53
1/*
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>();
29    static Animator.AnimatorListener sEndAnimListener = new Animator.AnimatorListener() {
30        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);
47        a.addListener(sEndAnimListener);
48    }
49
50    public static void onDestroyActivity() {
51        for (Animator a : sAnimators) {
52            if (a.isRunning()) {
53                a.cancel();
54            }
55        }
56    }
57
58    public static AnimatorSet createAnimatorSet() {
59        AnimatorSet anim = new AnimatorSet();
60        cancelOnDestroyActivity(anim);
61        return anim;
62    }
63
64    public static ValueAnimator ofFloat(float... values) {
65        ValueAnimator anim = new ValueAnimator();
66        anim.setFloatValues(values);
67        cancelOnDestroyActivity(anim);
68        return anim;
69    }
70
71    public static ObjectAnimator ofFloat(Object target, String propertyName, float... values) {
72        ObjectAnimator anim = new ObjectAnimator();
73        anim.setTarget(target);
74        anim.setPropertyName(propertyName);
75        anim.setFloatValues(values);
76        cancelOnDestroyActivity(anim);
77        return anim;
78    }
79
80    public static ObjectAnimator ofPropertyValuesHolder(Object target,
81            PropertyValuesHolder... values) {
82        ObjectAnimator anim = new ObjectAnimator();
83        anim.setTarget(target);
84        anim.setValues(values);
85        cancelOnDestroyActivity(anim);
86        return anim;
87    }
88}
89