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 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 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; |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 24 | import android.view.View; |
| 25 | import android.view.ViewTreeObserver; |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 26 | |
| 27 | import java.util.HashSet; |
Michael Jurka | 7c70d64 | 2013-10-23 15:21:32 +0200 | [diff] [blame^] | 28 | import java.util.WeakHashMap; |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 29 | |
| 30 | public class LauncherAnimUtils { |
Michael Jurka | 7c70d64 | 2013-10-23 15:21:32 +0200 | [diff] [blame^] | 31 | static WeakHashMap<Animator, Object> sAnimators = new WeakHashMap<Animator, Object>(); |
Michael Jurka | def8e65 | 2012-06-29 15:38:55 -0700 | [diff] [blame] | 32 | static Animator.AnimatorListener sEndAnimListener = new Animator.AnimatorListener() { |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 33 | public void onAnimationStart(Animator animation) { |
Michael Jurka | 7c70d64 | 2013-10-23 15:21:32 +0200 | [diff] [blame^] | 34 | sAnimators.put(animation, null); |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | public void onAnimationRepeat(Animator animation) { |
| 38 | } |
| 39 | |
| 40 | public void onAnimationEnd(Animator animation) { |
| 41 | sAnimators.remove(animation); |
| 42 | } |
| 43 | |
| 44 | public void onAnimationCancel(Animator animation) { |
| 45 | sAnimators.remove(animation); |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | public static void cancelOnDestroyActivity(Animator a) { |
Michael Jurka | def8e65 | 2012-06-29 15:38:55 -0700 | [diff] [blame] | 50 | a.addListener(sEndAnimListener); |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 53 | // Helper method. Assumes a draw is pending, and that if the animation's duration is 0 |
| 54 | // it should be cancelled |
| 55 | public static void startAnimationAfterNextDraw(final Animator animator, final View view) { |
Michael Jurka | df96add | 2013-04-03 16:25:02 -0700 | [diff] [blame] | 56 | view.getViewTreeObserver().addOnDrawListener(new ViewTreeObserver.OnDrawListener() { |
| 57 | private boolean mStarted = false; |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 58 | public void onDraw() { |
Michael Jurka | df96add | 2013-04-03 16:25:02 -0700 | [diff] [blame] | 59 | if (mStarted) return; |
| 60 | mStarted = true; |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 61 | // Use this as a signal that the animation was cancelled |
| 62 | if (animator.getDuration() == 0) { |
| 63 | return; |
| 64 | } |
| 65 | animator.start(); |
Michael Jurka | df96add | 2013-04-03 16:25:02 -0700 | [diff] [blame] | 66 | |
| 67 | final ViewTreeObserver.OnDrawListener listener = this; |
| 68 | view.post(new Runnable() { |
| 69 | public void run() { |
| 70 | view.getViewTreeObserver().removeOnDrawListener(listener); |
| 71 | } |
| 72 | }); |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 73 | } |
| 74 | }); |
| 75 | } |
| 76 | |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 77 | public static void onDestroyActivity() { |
Michael Jurka | 7c70d64 | 2013-10-23 15:21:32 +0200 | [diff] [blame^] | 78 | HashSet<Animator> animators = new HashSet<Animator>(sAnimators.keySet()); |
Winson Chung | 15ba53a | 2012-07-12 11:21:32 -0700 | [diff] [blame] | 79 | for (Animator a : animators) { |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 80 | if (a.isRunning()) { |
| 81 | a.cancel(); |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 82 | } |
Michael Jurka | 7c70d64 | 2013-10-23 15:21:32 +0200 | [diff] [blame^] | 83 | sAnimators.remove(a); |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| 87 | public static AnimatorSet createAnimatorSet() { |
| 88 | AnimatorSet anim = new AnimatorSet(); |
| 89 | cancelOnDestroyActivity(anim); |
| 90 | return anim; |
| 91 | } |
| 92 | |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 93 | public static ValueAnimator ofFloat(View target, float... values) { |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 94 | ValueAnimator anim = new ValueAnimator(); |
| 95 | anim.setFloatValues(values); |
| 96 | cancelOnDestroyActivity(anim); |
| 97 | return anim; |
| 98 | } |
| 99 | |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 100 | public static ObjectAnimator ofFloat(View target, String propertyName, float... values) { |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 101 | ObjectAnimator anim = new ObjectAnimator(); |
| 102 | anim.setTarget(target); |
| 103 | anim.setPropertyName(propertyName); |
| 104 | anim.setFloatValues(values); |
| 105 | cancelOnDestroyActivity(anim); |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 106 | new FirstFrameAnimatorHelper(anim, target); |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 107 | return anim; |
| 108 | } |
| 109 | |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 110 | public static ObjectAnimator ofPropertyValuesHolder(View target, |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 111 | PropertyValuesHolder... values) { |
| 112 | ObjectAnimator anim = new ObjectAnimator(); |
| 113 | anim.setTarget(target); |
| 114 | anim.setValues(values); |
| 115 | cancelOnDestroyActivity(anim); |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 116 | new FirstFrameAnimatorHelper(anim, target); |
| 117 | return anim; |
| 118 | } |
| 119 | |
| 120 | public static ObjectAnimator ofPropertyValuesHolder(Object target, |
| 121 | View view, PropertyValuesHolder... values) { |
| 122 | ObjectAnimator anim = new ObjectAnimator(); |
| 123 | anim.setTarget(target); |
| 124 | anim.setValues(values); |
| 125 | cancelOnDestroyActivity(anim); |
| 126 | new FirstFrameAnimatorHelper(anim, view); |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 127 | return anim; |
| 128 | } |
| 129 | } |