blob: be295f8b3ea73377a272b22f026812f967d9a5cf [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;
Michael Jurkaf1ad6082013-03-13 12:55:46 +010024import android.view.View;
Adam Cohen63f1ec02014-08-12 09:23:13 -070025import android.view.ViewAnimationUtils;
Michael Jurkaf1ad6082013-03-13 12:55:46 +010026import android.view.ViewTreeObserver;
Michael Jurka2ecf9952012-06-18 12:52:28 -070027
28import java.util.HashSet;
Michael Jurka7c70d642013-10-23 15:21:32 +020029import java.util.WeakHashMap;
Michael Jurka2ecf9952012-06-18 12:52:28 -070030
31public class LauncherAnimUtils {
Michael Jurka7c70d642013-10-23 15:21:32 +020032 static WeakHashMap<Animator, Object> sAnimators = new WeakHashMap<Animator, Object>();
Michael Jurkadef8e652012-06-29 15:38:55 -070033 static Animator.AnimatorListener sEndAnimListener = new Animator.AnimatorListener() {
Michael Jurka2ecf9952012-06-18 12:52:28 -070034 public void onAnimationStart(Animator animation) {
Michael Jurka7c70d642013-10-23 15:21:32 +020035 sAnimators.put(animation, null);
Michael Jurka2ecf9952012-06-18 12:52:28 -070036 }
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 Jurkadef8e652012-06-29 15:38:55 -070051 a.addListener(sEndAnimListener);
Michael Jurka2ecf9952012-06-18 12:52:28 -070052 }
53
Michael Jurkaf1ad6082013-03-13 12:55:46 +010054 // 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 Jurkadf96add2013-04-03 16:25:02 -070057 view.getViewTreeObserver().addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
58 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +010059 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -070060 if (mStarted) return;
61 mStarted = true;
Michael Jurkaf1ad6082013-03-13 12:55:46 +010062 // Use this as a signal that the animation was cancelled
63 if (animator.getDuration() == 0) {
64 return;
65 }
66 animator.start();
Michael Jurkadf96add2013-04-03 16:25:02 -070067
68 final ViewTreeObserver.OnDrawListener listener = this;
69 view.post(new Runnable() {
70 public void run() {
71 view.getViewTreeObserver().removeOnDrawListener(listener);
72 }
73 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +010074 }
75 });
76 }
77
Michael Jurka2ecf9952012-06-18 12:52:28 -070078 public static void onDestroyActivity() {
Michael Jurka7c70d642013-10-23 15:21:32 +020079 HashSet<Animator> animators = new HashSet<Animator>(sAnimators.keySet());
Winson Chung15ba53a2012-07-12 11:21:32 -070080 for (Animator a : animators) {
Michael Jurka2ecf9952012-06-18 12:52:28 -070081 if (a.isRunning()) {
82 a.cancel();
Michael Jurka2ecf9952012-06-18 12:52:28 -070083 }
Michael Jurka7c70d642013-10-23 15:21:32 +020084 sAnimators.remove(a);
Michael Jurka2ecf9952012-06-18 12:52:28 -070085 }
86 }
87
88 public static AnimatorSet createAnimatorSet() {
89 AnimatorSet anim = new AnimatorSet();
90 cancelOnDestroyActivity(anim);
91 return anim;
92 }
93
Michael Jurkaf1ad6082013-03-13 12:55:46 +010094 public static ValueAnimator ofFloat(View target, float... values) {
Michael Jurka2ecf9952012-06-18 12:52:28 -070095 ValueAnimator anim = new ValueAnimator();
96 anim.setFloatValues(values);
97 cancelOnDestroyActivity(anim);
98 return anim;
99 }
100
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100101 public static ObjectAnimator ofFloat(View target, String propertyName, float... values) {
Michael Jurka2ecf9952012-06-18 12:52:28 -0700102 ObjectAnimator anim = new ObjectAnimator();
103 anim.setTarget(target);
104 anim.setPropertyName(propertyName);
105 anim.setFloatValues(values);
106 cancelOnDestroyActivity(anim);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100107 new FirstFrameAnimatorHelper(anim, target);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700108 return anim;
109 }
110
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100111 public static ObjectAnimator ofPropertyValuesHolder(View target,
Michael Jurka2ecf9952012-06-18 12:52:28 -0700112 PropertyValuesHolder... values) {
113 ObjectAnimator anim = new ObjectAnimator();
114 anim.setTarget(target);
115 anim.setValues(values);
116 cancelOnDestroyActivity(anim);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100117 new FirstFrameAnimatorHelper(anim, target);
118 return anim;
119 }
120
121 public static ObjectAnimator ofPropertyValuesHolder(Object target,
122 View view, PropertyValuesHolder... values) {
123 ObjectAnimator anim = new ObjectAnimator();
124 anim.setTarget(target);
125 anim.setValues(values);
126 cancelOnDestroyActivity(anim);
127 new FirstFrameAnimatorHelper(anim, view);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700128 return anim;
129 }
Adam Cohen63f1ec02014-08-12 09:23:13 -0700130
131 public static Animator createCircularReveal(View view, int centerX,
132 int centerY, float startRadius, float endRadius) {
133 Animator anim = ViewAnimationUtils.createCircularReveal(view, centerX,
134 centerY, startRadius, endRadius);
135 if (anim instanceof ValueAnimator) {
136 new FirstFrameAnimatorHelper((ValueAnimator) anim, view);
137 }
138 return anim;
139 }
Michael Jurka2ecf9952012-06-18 12:52:28 -0700140}