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; |
Jon Miranda | 2d89ea8 | 2017-05-04 11:47:53 -0700 | [diff] [blame] | 24 | import android.graphics.drawable.Drawable; |
Sunny Goyal | 5d2fc32 | 2015-07-06 22:52:49 -0700 | [diff] [blame] | 25 | import android.util.Property; |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 26 | import android.view.View; |
| 27 | import android.view.ViewTreeObserver; |
Adam Cohen | 1558893 | 2015-05-26 23:03:31 -0700 | [diff] [blame] | 28 | |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 29 | import java.util.HashSet; |
Michael Jurka | 7c70d64 | 2013-10-23 15:21:32 +0200 | [diff] [blame] | 30 | import java.util.WeakHashMap; |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 31 | |
| 32 | public class LauncherAnimUtils { |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 33 | /** |
| 34 | * Durations for various state animations. These are not defined in resources to allow |
| 35 | * easier access from static classes and enums |
| 36 | */ |
| 37 | public static final int ALL_APPS_TRANSITION_MS = 320; |
| 38 | public static final int OVERVIEW_TRANSITION_MS = 250; |
| 39 | public static final int SPRING_LOADED_TRANSITION_MS = 150; |
Sunny Goyal | 4c7f215 | 2017-10-17 17:17:16 -0700 | [diff] [blame] | 40 | public static final int SPRING_LOADED_EXIT_DELAY = 500; |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 41 | |
Tony Wickham | 0f640b6 | 2018-05-09 16:00:14 -0700 | [diff] [blame^] | 42 | // The progress of an animation to all apps must be at least this far along to snap to all apps. |
| 43 | public static final float MIN_PROGRESS_TO_ALL_APPS = 0.5f; |
| 44 | |
Michael Jurka | 7c70d64 | 2013-10-23 15:21:32 +0200 | [diff] [blame] | 45 | static WeakHashMap<Animator, Object> sAnimators = new WeakHashMap<Animator, Object>(); |
Michael Jurka | def8e65 | 2012-06-29 15:38:55 -0700 | [diff] [blame] | 46 | static Animator.AnimatorListener sEndAnimListener = new Animator.AnimatorListener() { |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 47 | public void onAnimationStart(Animator animation) { |
Michael Jurka | 7c70d64 | 2013-10-23 15:21:32 +0200 | [diff] [blame] | 48 | sAnimators.put(animation, null); |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | public void onAnimationRepeat(Animator animation) { |
| 52 | } |
| 53 | |
| 54 | public void onAnimationEnd(Animator animation) { |
| 55 | sAnimators.remove(animation); |
| 56 | } |
| 57 | |
| 58 | public void onAnimationCancel(Animator animation) { |
| 59 | sAnimators.remove(animation); |
| 60 | } |
| 61 | }; |
| 62 | |
| 63 | public static void cancelOnDestroyActivity(Animator a) { |
Michael Jurka | def8e65 | 2012-06-29 15:38:55 -0700 | [diff] [blame] | 64 | a.addListener(sEndAnimListener); |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 67 | // Helper method. Assumes a draw is pending, and that if the animation's duration is 0 |
| 68 | // it should be cancelled |
| 69 | public static void startAnimationAfterNextDraw(final Animator animator, final View view) { |
Michael Jurka | df96add | 2013-04-03 16:25:02 -0700 | [diff] [blame] | 70 | view.getViewTreeObserver().addOnDrawListener(new ViewTreeObserver.OnDrawListener() { |
Tony Wickham | 0bb211a | 2015-10-02 16:22:08 -0700 | [diff] [blame] | 71 | private boolean mStarted = false; |
Michael Jurka | df96add | 2013-04-03 16:25:02 -0700 | [diff] [blame] | 72 | |
Tony Wickham | 0bb211a | 2015-10-02 16:22:08 -0700 | [diff] [blame] | 73 | public void onDraw() { |
| 74 | if (mStarted) return; |
| 75 | mStarted = true; |
| 76 | // Use this as a signal that the animation was cancelled |
| 77 | if (animator.getDuration() == 0) { |
| 78 | return; |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 79 | } |
Tony Wickham | 0bb211a | 2015-10-02 16:22:08 -0700 | [diff] [blame] | 80 | animator.start(); |
| 81 | |
| 82 | final ViewTreeObserver.OnDrawListener listener = this; |
| 83 | view.post(new Runnable() { |
| 84 | public void run() { |
| 85 | view.getViewTreeObserver().removeOnDrawListener(listener); |
| 86 | } |
| 87 | }); |
| 88 | } |
| 89 | }); |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 90 | } |
| 91 | |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 92 | public static void onDestroyActivity() { |
Michael Jurka | 7c70d64 | 2013-10-23 15:21:32 +0200 | [diff] [blame] | 93 | HashSet<Animator> animators = new HashSet<Animator>(sAnimators.keySet()); |
Winson Chung | 15ba53a | 2012-07-12 11:21:32 -0700 | [diff] [blame] | 94 | for (Animator a : animators) { |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 95 | if (a.isRunning()) { |
| 96 | a.cancel(); |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 97 | } |
Michael Jurka | 7c70d64 | 2013-10-23 15:21:32 +0200 | [diff] [blame] | 98 | sAnimators.remove(a); |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
| 102 | public static AnimatorSet createAnimatorSet() { |
| 103 | AnimatorSet anim = new AnimatorSet(); |
| 104 | cancelOnDestroyActivity(anim); |
| 105 | return anim; |
| 106 | } |
| 107 | |
Jon Miranda | cda3bfb | 2017-02-06 15:54:41 -0800 | [diff] [blame] | 108 | public static ValueAnimator ofFloat(float... values) { |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 109 | ValueAnimator anim = new ValueAnimator(); |
| 110 | anim.setFloatValues(values); |
| 111 | cancelOnDestroyActivity(anim); |
| 112 | return anim; |
| 113 | } |
| 114 | |
Sunny Goyal | 5d2fc32 | 2015-07-06 22:52:49 -0700 | [diff] [blame] | 115 | public static ObjectAnimator ofFloat(View target, Property<View, Float> property, |
| 116 | float... values) { |
| 117 | ObjectAnimator anim = ObjectAnimator.ofFloat(target, property, values); |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 118 | cancelOnDestroyActivity(anim); |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 119 | new FirstFrameAnimatorHelper(anim, target); |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 120 | return anim; |
| 121 | } |
| 122 | |
Sunny Goyal | 5d2fc32 | 2015-07-06 22:52:49 -0700 | [diff] [blame] | 123 | public static ObjectAnimator ofViewAlphaAndScale(View target, |
| 124 | float alpha, float scaleX, float scaleY) { |
| 125 | return ofPropertyValuesHolder(target, |
| 126 | PropertyValuesHolder.ofFloat(View.ALPHA, alpha), |
| 127 | PropertyValuesHolder.ofFloat(View.SCALE_X, scaleX), |
| 128 | PropertyValuesHolder.ofFloat(View.SCALE_Y, scaleY)); |
| 129 | } |
| 130 | |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 131 | public static ObjectAnimator ofPropertyValuesHolder(View target, |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 132 | PropertyValuesHolder... values) { |
Sunny Goyal | 5d2fc32 | 2015-07-06 22:52:49 -0700 | [diff] [blame] | 133 | return ofPropertyValuesHolder(target, target, values); |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | public static ObjectAnimator ofPropertyValuesHolder(Object target, |
| 137 | View view, PropertyValuesHolder... values) { |
Sunny Goyal | 5d2fc32 | 2015-07-06 22:52:49 -0700 | [diff] [blame] | 138 | ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(target, values); |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 139 | cancelOnDestroyActivity(anim); |
| 140 | new FirstFrameAnimatorHelper(anim, view); |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 141 | return anim; |
| 142 | } |
Tony Wickham | 9438ed4 | 2017-01-20 09:38:25 -0800 | [diff] [blame] | 143 | |
Jon Miranda | 2d89ea8 | 2017-05-04 11:47:53 -0700 | [diff] [blame] | 144 | public static final Property<Drawable, Integer> DRAWABLE_ALPHA = |
| 145 | new Property<Drawable, Integer>(Integer.TYPE, "drawableAlpha") { |
| 146 | @Override |
| 147 | public Integer get(Drawable drawable) { |
| 148 | return drawable.getAlpha(); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public void set(Drawable drawable, Integer alpha) { |
| 153 | drawable.setAlpha(alpha); |
| 154 | } |
| 155 | }; |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 156 | |
| 157 | public static final Property<View, Float> SCALE_PROPERTY = |
| 158 | new Property<View, Float>(Float.class, "scale") { |
| 159 | @Override |
| 160 | public Float get(View view) { |
| 161 | return view.getScaleX(); |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public void set(View view, Float scale) { |
| 166 | view.setScaleX(scale); |
| 167 | view.setScaleY(scale); |
| 168 | } |
| 169 | }; |
Sunny Goyal | ea52908 | 2017-10-31 13:33:03 -0700 | [diff] [blame] | 170 | |
Tony Wickham | 0f640b6 | 2018-05-09 16:00:14 -0700 | [diff] [blame^] | 171 | /** Increase the duration if we prevented the fling, as we are going against a high velocity. */ |
| 172 | public static int blockedFlingDurationFactor(float velocity) { |
| 173 | return (int) Utilities.boundToRange(Math.abs(velocity) / 2, 2f, 6f); |
| 174 | } |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 175 | } |