Michael Jurka | 7407d2a | 2011-12-12 21:48:38 -0800 | [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 | |
| 17 | package com.android.launcher2; |
| 18 | |
| 19 | import android.animation.Animator; |
| 20 | import android.animation.Animator.AnimatorListener; |
| 21 | import android.animation.TimeInterpolator; |
Michael Jurka | 7407d2a | 2011-12-12 21:48:38 -0800 | [diff] [blame] | 22 | import android.view.View; |
Michael Jurka | 032e6ba | 2013-04-22 15:08:42 +0200 | [diff] [blame^] | 23 | import android.view.ViewPropertyAnimator; |
Michael Jurka | 7407d2a | 2011-12-12 21:48:38 -0800 | [diff] [blame] | 24 | |
| 25 | import java.util.ArrayList; |
| 26 | import java.util.EnumSet; |
| 27 | |
| 28 | public class LauncherViewPropertyAnimator extends Animator implements AnimatorListener { |
| 29 | enum Properties { |
| 30 | TRANSLATION_X, |
| 31 | TRANSLATION_Y, |
| 32 | SCALE_X, |
| 33 | SCALE_Y, |
| 34 | ROTATION_Y, |
| 35 | ALPHA, |
| 36 | START_DELAY, |
| 37 | DURATION, |
| 38 | INTERPOLATOR |
| 39 | } |
| 40 | EnumSet<Properties> mPropertiesToSet = EnumSet.noneOf(Properties.class); |
| 41 | ViewPropertyAnimator mViewPropertyAnimator; |
| 42 | View mTarget; |
| 43 | |
| 44 | float mTranslationX; |
| 45 | float mTranslationY; |
| 46 | float mScaleX; |
| 47 | float mScaleY; |
| 48 | float mRotationY; |
| 49 | float mAlpha; |
| 50 | long mStartDelay; |
| 51 | long mDuration; |
| 52 | TimeInterpolator mInterpolator; |
Michael Jurka | 159b4cc | 2012-01-17 03:00:35 -0800 | [diff] [blame] | 53 | ArrayList<Animator.AnimatorListener> mListeners; |
Michael Jurka | 7407d2a | 2011-12-12 21:48:38 -0800 | [diff] [blame] | 54 | boolean mRunning = false; |
Michael Jurka | 39b599e | 2013-04-08 18:28:15 -0700 | [diff] [blame] | 55 | FirstFrameAnimatorHelper mFirstFrameHelper; |
Michael Jurka | 7407d2a | 2011-12-12 21:48:38 -0800 | [diff] [blame] | 56 | |
| 57 | public LauncherViewPropertyAnimator(View target) { |
| 58 | mTarget = target; |
Michael Jurka | 159b4cc | 2012-01-17 03:00:35 -0800 | [diff] [blame] | 59 | mListeners = new ArrayList<Animator.AnimatorListener>(); |
Michael Jurka | 7407d2a | 2011-12-12 21:48:38 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public void addListener(Animator.AnimatorListener listener) { |
Michael Jurka | 159b4cc | 2012-01-17 03:00:35 -0800 | [diff] [blame] | 64 | mListeners.add(listener); |
Michael Jurka | 7407d2a | 2011-12-12 21:48:38 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public void cancel() { |
Michael Jurka | 8a309c2 | 2012-01-12 06:06:59 -0800 | [diff] [blame] | 69 | if (mViewPropertyAnimator != null) { |
| 70 | mViewPropertyAnimator.cancel(); |
| 71 | } |
Michael Jurka | 7407d2a | 2011-12-12 21:48:38 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public Animator clone() { |
| 76 | throw new RuntimeException("Not implemented"); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public void end() { |
| 81 | throw new RuntimeException("Not implemented"); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public long getDuration() { |
Michael Jurka | 8a309c2 | 2012-01-12 06:06:59 -0800 | [diff] [blame] | 86 | return mDuration; |
Michael Jurka | 7407d2a | 2011-12-12 21:48:38 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public ArrayList<Animator.AnimatorListener> getListeners() { |
Michael Jurka | 159b4cc | 2012-01-17 03:00:35 -0800 | [diff] [blame] | 91 | return mListeners; |
Michael Jurka | 7407d2a | 2011-12-12 21:48:38 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public long getStartDelay() { |
Michael Jurka | 8a309c2 | 2012-01-12 06:06:59 -0800 | [diff] [blame] | 96 | return mStartDelay; |
Michael Jurka | 7407d2a | 2011-12-12 21:48:38 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public void onAnimationCancel(Animator animation) { |
Michael Jurka | 159b4cc | 2012-01-17 03:00:35 -0800 | [diff] [blame] | 101 | for (int i = 0; i < mListeners.size(); i++) { |
| 102 | Animator.AnimatorListener listener = mListeners.get(i); |
| 103 | listener.onAnimationCancel(this); |
Michael Jurka | 7407d2a | 2011-12-12 21:48:38 -0800 | [diff] [blame] | 104 | } |
| 105 | mRunning = false; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public void onAnimationEnd(Animator animation) { |
Michael Jurka | 159b4cc | 2012-01-17 03:00:35 -0800 | [diff] [blame] | 110 | for (int i = 0; i < mListeners.size(); i++) { |
| 111 | Animator.AnimatorListener listener = mListeners.get(i); |
| 112 | listener.onAnimationEnd(this); |
Michael Jurka | 7407d2a | 2011-12-12 21:48:38 -0800 | [diff] [blame] | 113 | } |
| 114 | mRunning = false; |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void onAnimationRepeat(Animator animation) { |
Michael Jurka | 159b4cc | 2012-01-17 03:00:35 -0800 | [diff] [blame] | 119 | for (int i = 0; i < mListeners.size(); i++) { |
| 120 | Animator.AnimatorListener listener = mListeners.get(i); |
| 121 | listener.onAnimationRepeat(this); |
Michael Jurka | 7407d2a | 2011-12-12 21:48:38 -0800 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void onAnimationStart(Animator animation) { |
Michael Jurka | 39b599e | 2013-04-08 18:28:15 -0700 | [diff] [blame] | 127 | // This is the first time we get a handle to the internal ValueAnimator |
| 128 | // used by the ViewPropertyAnimator. |
| 129 | mFirstFrameHelper.onAnimationStart(animation); |
| 130 | |
Michael Jurka | 159b4cc | 2012-01-17 03:00:35 -0800 | [diff] [blame] | 131 | for (int i = 0; i < mListeners.size(); i++) { |
| 132 | Animator.AnimatorListener listener = mListeners.get(i); |
| 133 | listener.onAnimationStart(this); |
Michael Jurka | 7407d2a | 2011-12-12 21:48:38 -0800 | [diff] [blame] | 134 | } |
| 135 | mRunning = true; |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public boolean isRunning() { |
| 140 | return mRunning; |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public boolean isStarted() { |
| 145 | return mViewPropertyAnimator != null; |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public void removeAllListeners() { |
Michael Jurka | 159b4cc | 2012-01-17 03:00:35 -0800 | [diff] [blame] | 150 | mListeners.clear(); |
Michael Jurka | 7407d2a | 2011-12-12 21:48:38 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public void removeListener(Animator.AnimatorListener listener) { |
Michael Jurka | 159b4cc | 2012-01-17 03:00:35 -0800 | [diff] [blame] | 155 | mListeners.remove(listener); |
Michael Jurka | 7407d2a | 2011-12-12 21:48:38 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public Animator setDuration(long duration) { |
| 160 | mPropertiesToSet.add(Properties.DURATION); |
| 161 | mDuration = duration; |
| 162 | return this; |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public void setInterpolator(TimeInterpolator value) { |
| 167 | mPropertiesToSet.add(Properties.INTERPOLATOR); |
| 168 | mInterpolator = value; |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public void setStartDelay(long startDelay) { |
| 173 | mPropertiesToSet.add(Properties.START_DELAY); |
| 174 | mStartDelay = startDelay; |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public void setTarget(Object target) { |
| 179 | throw new RuntimeException("Not implemented"); |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public void setupEndValues() { |
| 184 | |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public void setupStartValues() { |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public void start() { |
| 193 | mViewPropertyAnimator = mTarget.animate(); |
Michael Jurka | 39b599e | 2013-04-08 18:28:15 -0700 | [diff] [blame] | 194 | |
| 195 | // FirstFrameAnimatorHelper hooks itself up to the updates on the animator, |
| 196 | // and then adjusts the play time to keep the first two frames jank-free |
| 197 | mFirstFrameHelper = new FirstFrameAnimatorHelper(mViewPropertyAnimator, mTarget); |
| 198 | |
Michael Jurka | 7407d2a | 2011-12-12 21:48:38 -0800 | [diff] [blame] | 199 | if (mPropertiesToSet.contains(Properties.TRANSLATION_X)) { |
| 200 | mViewPropertyAnimator.translationX(mTranslationX); |
| 201 | } |
| 202 | if (mPropertiesToSet.contains(Properties.TRANSLATION_Y)) { |
| 203 | mViewPropertyAnimator.translationY(mTranslationY); |
| 204 | } |
| 205 | if (mPropertiesToSet.contains(Properties.SCALE_X)) { |
| 206 | mViewPropertyAnimator.scaleX(mScaleX); |
| 207 | } |
| 208 | if (mPropertiesToSet.contains(Properties.ROTATION_Y)) { |
| 209 | mViewPropertyAnimator.rotationY(mRotationY); |
| 210 | } |
| 211 | if (mPropertiesToSet.contains(Properties.SCALE_Y)) { |
| 212 | mViewPropertyAnimator.scaleY(mScaleY); |
| 213 | } |
| 214 | if (mPropertiesToSet.contains(Properties.ALPHA)) { |
| 215 | mViewPropertyAnimator.alpha(mAlpha); |
| 216 | } |
| 217 | if (mPropertiesToSet.contains(Properties.START_DELAY)) { |
| 218 | mViewPropertyAnimator.setStartDelay(mStartDelay); |
| 219 | } |
| 220 | if (mPropertiesToSet.contains(Properties.DURATION)) { |
| 221 | mViewPropertyAnimator.setDuration(mDuration); |
| 222 | } |
| 223 | if (mPropertiesToSet.contains(Properties.INTERPOLATOR)) { |
| 224 | mViewPropertyAnimator.setInterpolator(mInterpolator); |
| 225 | } |
| 226 | mViewPropertyAnimator.setListener(this); |
| 227 | mViewPropertyAnimator.start(); |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 228 | LauncherAnimUtils.cancelOnDestroyActivity(this); |
Michael Jurka | 7407d2a | 2011-12-12 21:48:38 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | public LauncherViewPropertyAnimator translationX(float value) { |
| 232 | mPropertiesToSet.add(Properties.TRANSLATION_X); |
| 233 | mTranslationX = value; |
| 234 | return this; |
| 235 | } |
| 236 | |
| 237 | public LauncherViewPropertyAnimator translationY(float value) { |
| 238 | mPropertiesToSet.add(Properties.TRANSLATION_Y); |
| 239 | mTranslationY = value; |
| 240 | return this; |
| 241 | } |
| 242 | |
| 243 | public LauncherViewPropertyAnimator scaleX(float value) { |
| 244 | mPropertiesToSet.add(Properties.SCALE_X); |
| 245 | mScaleX = value; |
| 246 | return this; |
| 247 | } |
| 248 | |
| 249 | public LauncherViewPropertyAnimator scaleY(float value) { |
| 250 | mPropertiesToSet.add(Properties.SCALE_Y); |
| 251 | mScaleY = value; |
| 252 | return this; |
| 253 | } |
| 254 | |
| 255 | public LauncherViewPropertyAnimator rotationY(float value) { |
| 256 | mPropertiesToSet.add(Properties.ROTATION_Y); |
| 257 | mRotationY = value; |
| 258 | return this; |
| 259 | } |
| 260 | |
| 261 | public LauncherViewPropertyAnimator alpha(float value) { |
| 262 | mPropertiesToSet.add(Properties.ALPHA); |
| 263 | mAlpha = value; |
| 264 | return this; |
| 265 | } |
| 266 | } |