blob: 6ff76665c5fd5039a134950b72b00eab7ff9c3e1 [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;
Adam Cohen63f1ec02014-08-12 09:23:13 -070027import android.view.ViewAnimationUtils;
Michael Jurkaf1ad6082013-03-13 12:55:46 +010028import android.view.ViewTreeObserver;
Michael Jurka2ecf9952012-06-18 12:52:28 -070029import java.util.HashSet;
Michael Jurka7c70d642013-10-23 15:21:32 +020030import java.util.WeakHashMap;
Michael Jurka2ecf9952012-06-18 12:52:28 -070031
32public class LauncherAnimUtils {
Michael Jurka7c70d642013-10-23 15:21:32 +020033 static WeakHashMap<Animator, Object> sAnimators = new WeakHashMap<Animator, Object>();
Michael Jurkadef8e652012-06-29 15:38:55 -070034 static Animator.AnimatorListener sEndAnimListener = new Animator.AnimatorListener() {
Michael Jurka2ecf9952012-06-18 12:52:28 -070035 public void onAnimationStart(Animator animation) {
Michael Jurka7c70d642013-10-23 15:21:32 +020036 sAnimators.put(animation, null);
Michael Jurka2ecf9952012-06-18 12:52:28 -070037 }
38
39 public void onAnimationRepeat(Animator animation) {
40 }
41
42 public void onAnimationEnd(Animator animation) {
43 sAnimators.remove(animation);
44 }
45
46 public void onAnimationCancel(Animator animation) {
47 sAnimators.remove(animation);
48 }
49 };
50
51 public static void cancelOnDestroyActivity(Animator a) {
Michael Jurkadef8e652012-06-29 15:38:55 -070052 a.addListener(sEndAnimListener);
Michael Jurka2ecf9952012-06-18 12:52:28 -070053 }
54
Michael Jurkaf1ad6082013-03-13 12:55:46 +010055 // Helper method. Assumes a draw is pending, and that if the animation's duration is 0
56 // it should be cancelled
57 public static void startAnimationAfterNextDraw(final Animator animator, final View view) {
Michael Jurkadf96add2013-04-03 16:25:02 -070058 view.getViewTreeObserver().addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
59 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +010060 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -070061 if (mStarted) return;
62 mStarted = true;
Michael Jurkaf1ad6082013-03-13 12:55:46 +010063 // Use this as a signal that the animation was cancelled
64 if (animator.getDuration() == 0) {
65 return;
66 }
67 animator.start();
Michael Jurkadf96add2013-04-03 16:25:02 -070068
69 final ViewTreeObserver.OnDrawListener listener = this;
70 view.post(new Runnable() {
71 public void run() {
72 view.getViewTreeObserver().removeOnDrawListener(listener);
73 }
74 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +010075 }
76 });
77 }
78
Michael Jurka2ecf9952012-06-18 12:52:28 -070079 public static void onDestroyActivity() {
Michael Jurka7c70d642013-10-23 15:21:32 +020080 HashSet<Animator> animators = new HashSet<Animator>(sAnimators.keySet());
Winson Chung15ba53a2012-07-12 11:21:32 -070081 for (Animator a : animators) {
Michael Jurka2ecf9952012-06-18 12:52:28 -070082 if (a.isRunning()) {
83 a.cancel();
Michael Jurka2ecf9952012-06-18 12:52:28 -070084 }
Michael Jurka7c70d642013-10-23 15:21:32 +020085 sAnimators.remove(a);
Michael Jurka2ecf9952012-06-18 12:52:28 -070086 }
87 }
88
89 public static AnimatorSet createAnimatorSet() {
90 AnimatorSet anim = new AnimatorSet();
91 cancelOnDestroyActivity(anim);
92 return anim;
93 }
94
Michael Jurkaf1ad6082013-03-13 12:55:46 +010095 public static ValueAnimator ofFloat(View target, float... values) {
Michael Jurka2ecf9952012-06-18 12:52:28 -070096 ValueAnimator anim = new ValueAnimator();
97 anim.setFloatValues(values);
98 cancelOnDestroyActivity(anim);
99 return anim;
100 }
101
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100102 public static ObjectAnimator ofFloat(View target, String propertyName, float... values) {
Michael Jurka2ecf9952012-06-18 12:52:28 -0700103 ObjectAnimator anim = new ObjectAnimator();
104 anim.setTarget(target);
105 anim.setPropertyName(propertyName);
106 anim.setFloatValues(values);
107 cancelOnDestroyActivity(anim);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100108 new FirstFrameAnimatorHelper(anim, target);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700109 return anim;
110 }
111
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100112 public static ObjectAnimator ofPropertyValuesHolder(View target,
Michael Jurka2ecf9952012-06-18 12:52:28 -0700113 PropertyValuesHolder... values) {
114 ObjectAnimator anim = new ObjectAnimator();
115 anim.setTarget(target);
116 anim.setValues(values);
117 cancelOnDestroyActivity(anim);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100118 new FirstFrameAnimatorHelper(anim, target);
119 return anim;
120 }
121
122 public static ObjectAnimator ofPropertyValuesHolder(Object target,
123 View view, PropertyValuesHolder... values) {
124 ObjectAnimator anim = new ObjectAnimator();
125 anim.setTarget(target);
126 anim.setValues(values);
127 cancelOnDestroyActivity(anim);
128 new FirstFrameAnimatorHelper(anim, view);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700129 return anim;
130 }
Adam Cohen63f1ec02014-08-12 09:23:13 -0700131
Sunny Goyal70660032015-05-14 00:07:08 -0700132 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen63f1ec02014-08-12 09:23:13 -0700133 public static Animator createCircularReveal(View view, int centerX,
134 int centerY, float startRadius, float endRadius) {
135 Animator anim = ViewAnimationUtils.createCircularReveal(view, centerX,
136 centerY, startRadius, endRadius);
137 if (anim instanceof ValueAnimator) {
138 new FirstFrameAnimatorHelper((ValueAnimator) anim, view);
139 }
140 return anim;
141 }
Michael Jurka2ecf9952012-06-18 12:52:28 -0700142}