blob: 4cafbbfa6649ddc2a3e28b5bd5b10e31c7403aed [file] [log] [blame]
Michael Jurka7407d2a2011-12-12 21:48:38 -08001/*
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 Jurka7407d2a2011-12-12 21:48:38 -080018
19import android.animation.Animator;
20import android.animation.Animator.AnimatorListener;
21import android.animation.TimeInterpolator;
Michael Jurka7407d2a2011-12-12 21:48:38 -080022import android.view.View;
Michael Jurka032e6ba2013-04-22 15:08:42 +020023import android.view.ViewPropertyAnimator;
Michael Jurka7407d2a2011-12-12 21:48:38 -080024
25import java.util.ArrayList;
26import java.util.EnumSet;
27
28public 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,
Michael Jurka7267fa52013-09-26 15:29:57 -070038 INTERPOLATOR,
39 WITH_LAYER
Michael Jurka7407d2a2011-12-12 21:48:38 -080040 }
41 EnumSet<Properties> mPropertiesToSet = EnumSet.noneOf(Properties.class);
42 ViewPropertyAnimator mViewPropertyAnimator;
43 View mTarget;
44
45 float mTranslationX;
46 float mTranslationY;
47 float mScaleX;
48 float mScaleY;
49 float mRotationY;
50 float mAlpha;
51 long mStartDelay;
52 long mDuration;
53 TimeInterpolator mInterpolator;
Michael Jurka159b4cc2012-01-17 03:00:35 -080054 ArrayList<Animator.AnimatorListener> mListeners;
Michael Jurka7407d2a2011-12-12 21:48:38 -080055 boolean mRunning = false;
Michael Jurka39b599e2013-04-08 18:28:15 -070056 FirstFrameAnimatorHelper mFirstFrameHelper;
Michael Jurka7407d2a2011-12-12 21:48:38 -080057
58 public LauncherViewPropertyAnimator(View target) {
59 mTarget = target;
Michael Jurka159b4cc2012-01-17 03:00:35 -080060 mListeners = new ArrayList<Animator.AnimatorListener>();
Michael Jurka7407d2a2011-12-12 21:48:38 -080061 }
62
63 @Override
64 public void addListener(Animator.AnimatorListener listener) {
Michael Jurka159b4cc2012-01-17 03:00:35 -080065 mListeners.add(listener);
Michael Jurka7407d2a2011-12-12 21:48:38 -080066 }
67
68 @Override
69 public void cancel() {
Michael Jurka8a309c22012-01-12 06:06:59 -080070 if (mViewPropertyAnimator != null) {
71 mViewPropertyAnimator.cancel();
72 }
Michael Jurka7407d2a2011-12-12 21:48:38 -080073 }
74
75 @Override
76 public Animator clone() {
77 throw new RuntimeException("Not implemented");
78 }
79
80 @Override
81 public void end() {
82 throw new RuntimeException("Not implemented");
83 }
84
85 @Override
86 public long getDuration() {
Michael Jurka8a309c22012-01-12 06:06:59 -080087 return mDuration;
Michael Jurka7407d2a2011-12-12 21:48:38 -080088 }
89
90 @Override
91 public ArrayList<Animator.AnimatorListener> getListeners() {
Michael Jurka159b4cc2012-01-17 03:00:35 -080092 return mListeners;
Michael Jurka7407d2a2011-12-12 21:48:38 -080093 }
94
95 @Override
96 public long getStartDelay() {
Michael Jurka8a309c22012-01-12 06:06:59 -080097 return mStartDelay;
Michael Jurka7407d2a2011-12-12 21:48:38 -080098 }
99
100 @Override
101 public void onAnimationCancel(Animator animation) {
Michael Jurka159b4cc2012-01-17 03:00:35 -0800102 for (int i = 0; i < mListeners.size(); i++) {
103 Animator.AnimatorListener listener = mListeners.get(i);
104 listener.onAnimationCancel(this);
Michael Jurka7407d2a2011-12-12 21:48:38 -0800105 }
106 mRunning = false;
107 }
108
109 @Override
110 public void onAnimationEnd(Animator animation) {
Michael Jurka159b4cc2012-01-17 03:00:35 -0800111 for (int i = 0; i < mListeners.size(); i++) {
112 Animator.AnimatorListener listener = mListeners.get(i);
113 listener.onAnimationEnd(this);
Michael Jurka7407d2a2011-12-12 21:48:38 -0800114 }
115 mRunning = false;
116 }
117
118 @Override
119 public void onAnimationRepeat(Animator animation) {
Michael Jurka159b4cc2012-01-17 03:00:35 -0800120 for (int i = 0; i < mListeners.size(); i++) {
121 Animator.AnimatorListener listener = mListeners.get(i);
122 listener.onAnimationRepeat(this);
Michael Jurka7407d2a2011-12-12 21:48:38 -0800123 }
124 }
125
126 @Override
127 public void onAnimationStart(Animator animation) {
Michael Jurka39b599e2013-04-08 18:28:15 -0700128 // This is the first time we get a handle to the internal ValueAnimator
129 // used by the ViewPropertyAnimator.
130 mFirstFrameHelper.onAnimationStart(animation);
131
Michael Jurka159b4cc2012-01-17 03:00:35 -0800132 for (int i = 0; i < mListeners.size(); i++) {
133 Animator.AnimatorListener listener = mListeners.get(i);
134 listener.onAnimationStart(this);
Michael Jurka7407d2a2011-12-12 21:48:38 -0800135 }
136 mRunning = true;
137 }
138
139 @Override
140 public boolean isRunning() {
141 return mRunning;
142 }
143
144 @Override
145 public boolean isStarted() {
146 return mViewPropertyAnimator != null;
147 }
148
149 @Override
150 public void removeAllListeners() {
Michael Jurka159b4cc2012-01-17 03:00:35 -0800151 mListeners.clear();
Michael Jurka7407d2a2011-12-12 21:48:38 -0800152 }
153
154 @Override
155 public void removeListener(Animator.AnimatorListener listener) {
Michael Jurka159b4cc2012-01-17 03:00:35 -0800156 mListeners.remove(listener);
Michael Jurka7407d2a2011-12-12 21:48:38 -0800157 }
158
159 @Override
160 public Animator setDuration(long duration) {
161 mPropertiesToSet.add(Properties.DURATION);
162 mDuration = duration;
163 return this;
164 }
165
166 @Override
167 public void setInterpolator(TimeInterpolator value) {
168 mPropertiesToSet.add(Properties.INTERPOLATOR);
169 mInterpolator = value;
170 }
171
172 @Override
173 public void setStartDelay(long startDelay) {
174 mPropertiesToSet.add(Properties.START_DELAY);
175 mStartDelay = startDelay;
176 }
177
178 @Override
179 public void setTarget(Object target) {
180 throw new RuntimeException("Not implemented");
181 }
182
183 @Override
184 public void setupEndValues() {
185
186 }
187
188 @Override
189 public void setupStartValues() {
190 }
191
192 @Override
193 public void start() {
194 mViewPropertyAnimator = mTarget.animate();
Michael Jurka39b599e2013-04-08 18:28:15 -0700195
196 // FirstFrameAnimatorHelper hooks itself up to the updates on the animator,
197 // and then adjusts the play time to keep the first two frames jank-free
198 mFirstFrameHelper = new FirstFrameAnimatorHelper(mViewPropertyAnimator, mTarget);
199
Michael Jurka7407d2a2011-12-12 21:48:38 -0800200 if (mPropertiesToSet.contains(Properties.TRANSLATION_X)) {
201 mViewPropertyAnimator.translationX(mTranslationX);
202 }
203 if (mPropertiesToSet.contains(Properties.TRANSLATION_Y)) {
204 mViewPropertyAnimator.translationY(mTranslationY);
205 }
206 if (mPropertiesToSet.contains(Properties.SCALE_X)) {
207 mViewPropertyAnimator.scaleX(mScaleX);
208 }
209 if (mPropertiesToSet.contains(Properties.ROTATION_Y)) {
210 mViewPropertyAnimator.rotationY(mRotationY);
211 }
212 if (mPropertiesToSet.contains(Properties.SCALE_Y)) {
213 mViewPropertyAnimator.scaleY(mScaleY);
214 }
215 if (mPropertiesToSet.contains(Properties.ALPHA)) {
216 mViewPropertyAnimator.alpha(mAlpha);
217 }
218 if (mPropertiesToSet.contains(Properties.START_DELAY)) {
219 mViewPropertyAnimator.setStartDelay(mStartDelay);
220 }
221 if (mPropertiesToSet.contains(Properties.DURATION)) {
222 mViewPropertyAnimator.setDuration(mDuration);
223 }
224 if (mPropertiesToSet.contains(Properties.INTERPOLATOR)) {
225 mViewPropertyAnimator.setInterpolator(mInterpolator);
226 }
Michael Jurka7267fa52013-09-26 15:29:57 -0700227 if (mPropertiesToSet.contains(Properties.WITH_LAYER)) {
228 mViewPropertyAnimator.withLayer();
229 }
Michael Jurka7407d2a2011-12-12 21:48:38 -0800230 mViewPropertyAnimator.setListener(this);
231 mViewPropertyAnimator.start();
Michael Jurka2ecf9952012-06-18 12:52:28 -0700232 LauncherAnimUtils.cancelOnDestroyActivity(this);
Michael Jurka7407d2a2011-12-12 21:48:38 -0800233 }
234
235 public LauncherViewPropertyAnimator translationX(float value) {
236 mPropertiesToSet.add(Properties.TRANSLATION_X);
237 mTranslationX = value;
238 return this;
239 }
240
241 public LauncherViewPropertyAnimator translationY(float value) {
242 mPropertiesToSet.add(Properties.TRANSLATION_Y);
243 mTranslationY = value;
244 return this;
245 }
246
247 public LauncherViewPropertyAnimator scaleX(float value) {
248 mPropertiesToSet.add(Properties.SCALE_X);
249 mScaleX = value;
250 return this;
251 }
252
253 public LauncherViewPropertyAnimator scaleY(float value) {
254 mPropertiesToSet.add(Properties.SCALE_Y);
255 mScaleY = value;
256 return this;
257 }
258
259 public LauncherViewPropertyAnimator rotationY(float value) {
260 mPropertiesToSet.add(Properties.ROTATION_Y);
261 mRotationY = value;
262 return this;
263 }
264
265 public LauncherViewPropertyAnimator alpha(float value) {
266 mPropertiesToSet.add(Properties.ALPHA);
267 mAlpha = value;
268 return this;
269 }
Michael Jurka7267fa52013-09-26 15:29:57 -0700270
271 public LauncherViewPropertyAnimator withLayer() {
272 mPropertiesToSet.add(Properties.WITH_LAYER);
273 return this;
274 }
Michael Jurka7407d2a2011-12-12 21:48:38 -0800275}