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