blob: cfb9b570b07f507159930e135869b9c1408f9d38 [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;
Jon Miranda2d89ea82017-05-04 11:47:53 -070024import android.graphics.drawable.Drawable;
Sunny Goyal5d2fc322015-07-06 22:52:49 -070025import android.util.Property;
Michael Jurkaf1ad6082013-03-13 12:55:46 +010026import android.view.View;
27import android.view.ViewTreeObserver;
Adam Cohen15588932015-05-26 23:03:31 -070028
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() {
Tony Wickham0bb211a2015-10-02 16:22:08 -070059 private boolean mStarted = false;
Michael Jurkadf96add2013-04-03 16:25:02 -070060
Tony Wickham0bb211a2015-10-02 16:22:08 -070061 public void onDraw() {
62 if (mStarted) return;
63 mStarted = true;
64 // Use this as a signal that the animation was cancelled
65 if (animator.getDuration() == 0) {
66 return;
Michael Jurkaf1ad6082013-03-13 12:55:46 +010067 }
Tony Wickham0bb211a2015-10-02 16:22:08 -070068 animator.start();
69
70 final ViewTreeObserver.OnDrawListener listener = this;
71 view.post(new Runnable() {
72 public void run() {
73 view.getViewTreeObserver().removeOnDrawListener(listener);
74 }
75 });
76 }
77 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +010078 }
79
Michael Jurka2ecf9952012-06-18 12:52:28 -070080 public static void onDestroyActivity() {
Michael Jurka7c70d642013-10-23 15:21:32 +020081 HashSet<Animator> animators = new HashSet<Animator>(sAnimators.keySet());
Winson Chung15ba53a2012-07-12 11:21:32 -070082 for (Animator a : animators) {
Michael Jurka2ecf9952012-06-18 12:52:28 -070083 if (a.isRunning()) {
84 a.cancel();
Michael Jurka2ecf9952012-06-18 12:52:28 -070085 }
Michael Jurka7c70d642013-10-23 15:21:32 +020086 sAnimators.remove(a);
Michael Jurka2ecf9952012-06-18 12:52:28 -070087 }
88 }
89
90 public static AnimatorSet createAnimatorSet() {
91 AnimatorSet anim = new AnimatorSet();
92 cancelOnDestroyActivity(anim);
93 return anim;
94 }
95
Jon Mirandacda3bfb2017-02-06 15:54:41 -080096 public static ValueAnimator ofFloat(float... values) {
Michael Jurka2ecf9952012-06-18 12:52:28 -070097 ValueAnimator anim = new ValueAnimator();
98 anim.setFloatValues(values);
99 cancelOnDestroyActivity(anim);
100 return anim;
101 }
102
Sunny Goyal5d2fc322015-07-06 22:52:49 -0700103 public static ObjectAnimator ofFloat(View target, Property<View, Float> property,
104 float... values) {
105 ObjectAnimator anim = ObjectAnimator.ofFloat(target, property, values);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700106 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
Sunny Goyal5d2fc322015-07-06 22:52:49 -0700111 public static ObjectAnimator ofViewAlphaAndScale(View target,
112 float alpha, float scaleX, float scaleY) {
113 return ofPropertyValuesHolder(target,
114 PropertyValuesHolder.ofFloat(View.ALPHA, alpha),
115 PropertyValuesHolder.ofFloat(View.SCALE_X, scaleX),
116 PropertyValuesHolder.ofFloat(View.SCALE_Y, scaleY));
117 }
118
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100119 public static ObjectAnimator ofPropertyValuesHolder(View target,
Michael Jurka2ecf9952012-06-18 12:52:28 -0700120 PropertyValuesHolder... values) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -0700121 return ofPropertyValuesHolder(target, target, values);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100122 }
123
124 public static ObjectAnimator ofPropertyValuesHolder(Object target,
125 View view, PropertyValuesHolder... values) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -0700126 ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(target, values);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100127 cancelOnDestroyActivity(anim);
128 new FirstFrameAnimatorHelper(anim, view);
Michael Jurka2ecf9952012-06-18 12:52:28 -0700129 return anim;
130 }
Tony Wickham9438ed42017-01-20 09:38:25 -0800131
Jon Miranda2d89ea82017-05-04 11:47:53 -0700132 public static final Property<Drawable, Integer> DRAWABLE_ALPHA =
133 new Property<Drawable, Integer>(Integer.TYPE, "drawableAlpha") {
134 @Override
135 public Integer get(Drawable drawable) {
136 return drawable.getAlpha();
137 }
138
139 @Override
140 public void set(Drawable drawable, Integer alpha) {
141 drawable.setAlpha(alpha);
142 }
143 };
Michael Jurka2ecf9952012-06-18 12:52:28 -0700144}