blob: fa20f0360bb4b035e2587fac457644f32e212603 [file] [log] [blame]
Michael Jurka2ecf9952012-06-18 12:52:28 -07001/*
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 Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Michael Jurka2ecf9952012-06-18 12:52:28 -070018
19import android.animation.Animator;
20import android.animation.AnimatorSet;
21import android.animation.ObjectAnimator;
22import android.animation.PropertyValuesHolder;
23import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070024import android.annotation.TargetApi;
25import android.os.Build;
Michael Jurkaf1ad6082013-03-13 12:55:46 +010026import android.view.View;
27import android.view.ViewTreeObserver;
Adam Cohen15588932015-05-26 23:03:31 -070028
29import com.android.launcher3.util.UiThreadCircularReveal;
30
Michael Jurka2ecf9952012-06-18 12:52:28 -070031import java.util.HashSet;
Michael Jurka7c70d642013-10-23 15:21:32 +020032import java.util.WeakHashMap;
Michael Jurka2ecf9952012-06-18 12:52:28 -070033
34public class LauncherAnimUtils {
Michael Jurka7c70d642013-10-23 15:21:32 +020035 static WeakHashMap<Animator, Object> sAnimators = new WeakHashMap<Animator, Object>();
Michael Jurkadef8e652012-06-29 15:38:55 -070036 static Animator.AnimatorListener sEndAnimListener = new Animator.AnimatorListener() {
Michael Jurka2ecf9952012-06-18 12:52:28 -070037 public void onAnimationStart(Animator animation) {
Michael Jurka7c70d642013-10-23 15:21:32 +020038 sAnimators.put(animation, null);
Michael Jurka2ecf9952012-06-18 12:52:28 -070039 }
40
41 public void onAnimationRepeat(Animator animation) {
42 }
43
44 public void onAnimationEnd(Animator animation) {
45 sAnimators.remove(animation);
46 }
47
48 public void onAnimationCancel(Animator animation) {
49 sAnimators.remove(animation);
50 }
51 };
52
53 public static void cancelOnDestroyActivity(Animator a) {
Michael Jurkadef8e652012-06-29 15:38:55 -070054 a.addListener(sEndAnimListener);
Michael Jurka2ecf9952012-06-18 12:52:28 -070055 }
56
Michael Jurkaf1ad6082013-03-13 12:55:46 +010057 // Helper method. Assumes a draw is pending, and that if the animation's duration is 0
58 // it should be cancelled
59 public static void startAnimationAfterNextDraw(final Animator animator, final View view) {
Michael Jurkadf96add2013-04-03 16:25:02 -070060 view.getViewTreeObserver().addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Tony Wickham0bb211a2015-10-02 16:22:08 -070061 private boolean mStarted = false;
Michael Jurkadf96add2013-04-03 16:25:02 -070062
Tony Wickham0bb211a2015-10-02 16:22:08 -070063 public void onDraw() {
64 if (mStarted) return;
65 mStarted = true;
66 // Use this as a signal that the animation was cancelled
67 if (animator.getDuration() == 0) {
68 return;
Michael Jurkaf1ad6082013-03-13 12:55:46 +010069 }
Tony Wickham0bb211a2015-10-02 16:22:08 -070070 animator.start();
71
72 final ViewTreeObserver.OnDrawListener listener = this;
73 view.post(new Runnable() {
74 public void run() {
75 view.getViewTreeObserver().removeOnDrawListener(listener);
76 }
77 });
78 }
79 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +010080 }
81
Michael Jurka2ecf9952012-06-18 12:52:28 -070082 public static void onDestroyActivity() {
Michael Jurka7c70d642013-10-23 15:21:32 +020083 HashSet<Animator> animators = new HashSet<Animator>(sAnimators.keySet());
Winson Chung15ba53a2012-07-12 11:21:32 -070084 for (Animator a : animators) {
Michael Jurka2ecf9952012-06-18 12:52:28 -070085 if (a.isRunning()) {
86 a.cancel();
Michael Jurka2ecf9952012-06-18 12:52:28 -070087 }
Michael Jurka7c70d642013-10-23 15:21:32 +020088 sAnimators.remove(a);
Michael Jurka2ecf9952012-06-18 12:52:28 -070089 }
90 }
91
92 public static AnimatorSet createAnimatorSet() {
93 AnimatorSet anim = new AnimatorSet();
94 cancelOnDestroyActivity(anim);
95 return anim;
96 }
97
Michael Jurkaf1ad6082013-03-13 12:55:46 +010098 public static ValueAnimator ofFloat(View target, float... values) {
Michael Jurka2ecf9952012-06-18 12:52:28 -070099 ValueAnimator anim = new ValueAnimator();
100 anim.setFloatValues(values);
101 cancelOnDestroyActivity(anim);
102 return anim;
103 }
104
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100105 public static ObjectAnimator ofFloat(View target, String propertyName, float... values) {
Michael Jurka2ecf9952012-06-18 12:52:28 -0700106 ObjectAnimator anim = new ObjectAnimator();
107 anim.setTarget(target);
108 anim.setPropertyName(propertyName);
109 anim.setFloatValues(values);
110 cancelOnDestroyActivity(anim);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100111 new FirstFrameAnimatorHelper(anim, target);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700112 return anim;
113 }
114
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100115 public static ObjectAnimator ofPropertyValuesHolder(View target,
Michael Jurka2ecf9952012-06-18 12:52:28 -0700116 PropertyValuesHolder... values) {
117 ObjectAnimator anim = new ObjectAnimator();
118 anim.setTarget(target);
119 anim.setValues(values);
120 cancelOnDestroyActivity(anim);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100121 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 Jurka2ecf9952012-06-18 12:52:28 -0700132 return anim;
133 }
Adam Cohen63f1ec02014-08-12 09:23:13 -0700134
Sunny Goyal70660032015-05-14 00:07:08 -0700135 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen15588932015-05-26 23:03:31 -0700136 public static ValueAnimator createCircularReveal(View view, int centerX,
Adam Cohen63f1ec02014-08-12 09:23:13 -0700137 int centerY, float startRadius, float endRadius) {
Adam Cohen15588932015-05-26 23:03:31 -0700138 ValueAnimator anim = UiThreadCircularReveal.createCircularReveal(view, centerX,
Adam Cohen63f1ec02014-08-12 09:23:13 -0700139 centerY, startRadius, endRadius);
Adam Cohen15588932015-05-26 23:03:31 -0700140 new FirstFrameAnimatorHelper(anim, view);
Adam Cohen63f1ec02014-08-12 09:23:13 -0700141 return anim;
142 }
Michael Jurka2ecf9952012-06-18 12:52:28 -0700143}