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