Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.launcher2; |
| 18 | |
| 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorSet; |
| 21 | import android.animation.ObjectAnimator; |
| 22 | import android.animation.PropertyValuesHolder; |
| 23 | import android.animation.ValueAnimator; |
| 24 | |
| 25 | import java.util.HashSet; |
| 26 | |
| 27 | public class LauncherAnimUtils { |
| 28 | static HashSet<Animator> sAnimators = new HashSet<Animator>(); |
Michael Jurka | def8e65 | 2012-06-29 15:38:55 -0700 | [diff] [blame^] | 29 | static Animator.AnimatorListener sEndAnimListener = new Animator.AnimatorListener() { |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 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); |
Michael Jurka | def8e65 | 2012-06-29 15:38:55 -0700 | [diff] [blame^] | 47 | a.addListener(sEndAnimListener); |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 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 | } |