blob: 88b4cb4b8a047d2f7b7d7053eb8a3dfa6c384df1 [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
17package com.android.launcher2;
18
19import android.animation.Animator;
20import android.animation.Animator.AnimatorListener;
21import android.animation.TimeInterpolator;
22import android.view.ViewPropertyAnimator;
23import android.view.View;
24
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,
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 Jurka159b4cc2012-01-17 03:00:35 -080053 ArrayList<Animator.AnimatorListener> mListeners;
Michael Jurka7407d2a2011-12-12 21:48:38 -080054 boolean mRunning = false;
55
56 public LauncherViewPropertyAnimator(View target) {
57 mTarget = target;
Michael Jurka159b4cc2012-01-17 03:00:35 -080058 mListeners = new ArrayList<Animator.AnimatorListener>();
Michael Jurka7407d2a2011-12-12 21:48:38 -080059 }
60
61 @Override
62 public void addListener(Animator.AnimatorListener listener) {
Michael Jurka159b4cc2012-01-17 03:00:35 -080063 mListeners.add(listener);
Michael Jurka7407d2a2011-12-12 21:48:38 -080064 }
65
66 @Override
67 public void cancel() {
Michael Jurka8a309c22012-01-12 06:06:59 -080068 if (mViewPropertyAnimator != null) {
69 mViewPropertyAnimator.cancel();
70 }
Michael Jurka7407d2a2011-12-12 21:48:38 -080071 }
72
73 @Override
74 public Animator clone() {
75 throw new RuntimeException("Not implemented");
76 }
77
78 @Override
79 public void end() {
80 throw new RuntimeException("Not implemented");
81 }
82
83 @Override
84 public long getDuration() {
Michael Jurka8a309c22012-01-12 06:06:59 -080085 return mDuration;
Michael Jurka7407d2a2011-12-12 21:48:38 -080086 }
87
88 @Override
89 public ArrayList<Animator.AnimatorListener> getListeners() {
Michael Jurka159b4cc2012-01-17 03:00:35 -080090 return mListeners;
Michael Jurka7407d2a2011-12-12 21:48:38 -080091 }
92
93 @Override
94 public long getStartDelay() {
Michael Jurka8a309c22012-01-12 06:06:59 -080095 return mStartDelay;
Michael Jurka7407d2a2011-12-12 21:48:38 -080096 }
97
98 @Override
99 public void onAnimationCancel(Animator animation) {
Michael Jurka159b4cc2012-01-17 03:00:35 -0800100 for (int i = 0; i < mListeners.size(); i++) {
101 Animator.AnimatorListener listener = mListeners.get(i);
102 listener.onAnimationCancel(this);
Michael Jurka7407d2a2011-12-12 21:48:38 -0800103 }
104 mRunning = false;
105 }
106
107 @Override
108 public void onAnimationEnd(Animator animation) {
Michael Jurka159b4cc2012-01-17 03:00:35 -0800109 for (int i = 0; i < mListeners.size(); i++) {
110 Animator.AnimatorListener listener = mListeners.get(i);
111 listener.onAnimationEnd(this);
Michael Jurka7407d2a2011-12-12 21:48:38 -0800112 }
113 mRunning = false;
114 }
115
116 @Override
117 public void onAnimationRepeat(Animator animation) {
Michael Jurka159b4cc2012-01-17 03:00:35 -0800118 for (int i = 0; i < mListeners.size(); i++) {
119 Animator.AnimatorListener listener = mListeners.get(i);
120 listener.onAnimationRepeat(this);
Michael Jurka7407d2a2011-12-12 21:48:38 -0800121 }
122 }
123
124 @Override
125 public void onAnimationStart(Animator animation) {
Michael Jurka159b4cc2012-01-17 03:00:35 -0800126 for (int i = 0; i < mListeners.size(); i++) {
127 Animator.AnimatorListener listener = mListeners.get(i);
128 listener.onAnimationStart(this);
Michael Jurka7407d2a2011-12-12 21:48:38 -0800129 }
130 mRunning = true;
131 }
132
133 @Override
134 public boolean isRunning() {
135 return mRunning;
136 }
137
138 @Override
139 public boolean isStarted() {
140 return mViewPropertyAnimator != null;
141 }
142
143 @Override
144 public void removeAllListeners() {
Michael Jurka159b4cc2012-01-17 03:00:35 -0800145 mListeners.clear();
Michael Jurka7407d2a2011-12-12 21:48:38 -0800146 }
147
148 @Override
149 public void removeListener(Animator.AnimatorListener listener) {
Michael Jurka159b4cc2012-01-17 03:00:35 -0800150 mListeners.remove(listener);
Michael Jurka7407d2a2011-12-12 21:48:38 -0800151 }
152
153 @Override
154 public Animator setDuration(long duration) {
155 mPropertiesToSet.add(Properties.DURATION);
156 mDuration = duration;
157 return this;
158 }
159
160 @Override
161 public void setInterpolator(TimeInterpolator value) {
162 mPropertiesToSet.add(Properties.INTERPOLATOR);
163 mInterpolator = value;
164 }
165
166 @Override
167 public void setStartDelay(long startDelay) {
168 mPropertiesToSet.add(Properties.START_DELAY);
169 mStartDelay = startDelay;
170 }
171
172 @Override
173 public void setTarget(Object target) {
174 throw new RuntimeException("Not implemented");
175 }
176
177 @Override
178 public void setupEndValues() {
179
180 }
181
182 @Override
183 public void setupStartValues() {
184 }
185
186 @Override
187 public void start() {
188 mViewPropertyAnimator = mTarget.animate();
189 if (mPropertiesToSet.contains(Properties.TRANSLATION_X)) {
190 mViewPropertyAnimator.translationX(mTranslationX);
191 }
192 if (mPropertiesToSet.contains(Properties.TRANSLATION_Y)) {
193 mViewPropertyAnimator.translationY(mTranslationY);
194 }
195 if (mPropertiesToSet.contains(Properties.SCALE_X)) {
196 mViewPropertyAnimator.scaleX(mScaleX);
197 }
198 if (mPropertiesToSet.contains(Properties.ROTATION_Y)) {
199 mViewPropertyAnimator.rotationY(mRotationY);
200 }
201 if (mPropertiesToSet.contains(Properties.SCALE_Y)) {
202 mViewPropertyAnimator.scaleY(mScaleY);
203 }
204 if (mPropertiesToSet.contains(Properties.ALPHA)) {
205 mViewPropertyAnimator.alpha(mAlpha);
206 }
207 if (mPropertiesToSet.contains(Properties.START_DELAY)) {
208 mViewPropertyAnimator.setStartDelay(mStartDelay);
209 }
210 if (mPropertiesToSet.contains(Properties.DURATION)) {
211 mViewPropertyAnimator.setDuration(mDuration);
212 }
213 if (mPropertiesToSet.contains(Properties.INTERPOLATOR)) {
214 mViewPropertyAnimator.setInterpolator(mInterpolator);
215 }
216 mViewPropertyAnimator.setListener(this);
217 mViewPropertyAnimator.start();
218 }
219
220 public LauncherViewPropertyAnimator translationX(float value) {
221 mPropertiesToSet.add(Properties.TRANSLATION_X);
222 mTranslationX = value;
223 return this;
224 }
225
226 public LauncherViewPropertyAnimator translationY(float value) {
227 mPropertiesToSet.add(Properties.TRANSLATION_Y);
228 mTranslationY = value;
229 return this;
230 }
231
232 public LauncherViewPropertyAnimator scaleX(float value) {
233 mPropertiesToSet.add(Properties.SCALE_X);
234 mScaleX = value;
235 return this;
236 }
237
238 public LauncherViewPropertyAnimator scaleY(float value) {
239 mPropertiesToSet.add(Properties.SCALE_Y);
240 mScaleY = value;
241 return this;
242 }
243
244 public LauncherViewPropertyAnimator rotationY(float value) {
245 mPropertiesToSet.add(Properties.ROTATION_Y);
246 mRotationY = value;
247 return this;
248 }
249
250 public LauncherViewPropertyAnimator alpha(float value) {
251 mPropertiesToSet.add(Properties.ALPHA);
252 mAlpha = value;
253 return this;
254 }
255}