LauncherViewPropertyAnimator.java revision 8a309c257a97d9c4c402fae5f00941e8169ea541
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
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;
53    Animator.AnimatorListener mListener;
54    boolean mRunning = false;
55
56    public LauncherViewPropertyAnimator(View target) {
57        mTarget = target;
58    }
59
60    @Override
61    public void addListener(Animator.AnimatorListener listener) {
62        if (mListener != null) {
63            throw new RuntimeException("Only one listener supported");
64        }
65        mListener = listener;
66    }
67
68    @Override
69    public void cancel() {
70        if (mViewPropertyAnimator != null) {
71            mViewPropertyAnimator.cancel();
72        }
73    }
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() {
87        return mDuration;
88    }
89
90    @Override
91    public ArrayList<Animator.AnimatorListener> getListeners() {
92        return null;
93    }
94
95    @Override
96    public long getStartDelay() {
97        return mStartDelay;
98    }
99
100    @Override
101    public void onAnimationCancel(Animator animation) {
102        if (mListener != null) {
103            mListener.onAnimationCancel(this);
104        }
105        mRunning = false;
106    }
107
108    @Override
109    public void onAnimationEnd(Animator animation) {
110        if (mListener != null) {
111            mListener.onAnimationEnd(this);
112        }
113        mRunning = false;
114    }
115
116    @Override
117    public void onAnimationRepeat(Animator animation) {
118        if (mListener != null) {
119            mListener.onAnimationRepeat(this);
120        }
121    }
122
123    @Override
124    public void onAnimationStart(Animator animation) {
125        if (mListener != null) {
126            mListener.onAnimationStart(this);
127        }
128        mRunning = true;
129    }
130
131    @Override
132    public boolean isRunning() {
133        return mRunning;
134    }
135
136    @Override
137    public boolean isStarted() {
138        return mViewPropertyAnimator != null;
139    }
140
141    @Override
142    public void removeAllListeners() {
143        mListener = null;
144    }
145
146    @Override
147    public void removeListener(Animator.AnimatorListener listener) {
148        if (mListener == listener) {
149            mListener = null;
150        } else {
151            throw new RuntimeException("Removing listener that wasn't set");
152        }
153    }
154
155    @Override
156    public Animator setDuration(long duration) {
157        mPropertiesToSet.add(Properties.DURATION);
158        mDuration = duration;
159        return this;
160    }
161
162    @Override
163    public void setInterpolator(TimeInterpolator value) {
164        mPropertiesToSet.add(Properties.INTERPOLATOR);
165        mInterpolator = value;
166    }
167
168    @Override
169    public void setStartDelay(long startDelay) {
170        mPropertiesToSet.add(Properties.START_DELAY);
171        mStartDelay = startDelay;
172    }
173
174    @Override
175    public void setTarget(Object target) {
176        throw new RuntimeException("Not implemented");
177    }
178
179    @Override
180    public void setupEndValues() {
181
182    }
183
184    @Override
185    public void setupStartValues() {
186    }
187
188    @Override
189    public void start() {
190        mViewPropertyAnimator = mTarget.animate();
191        if (mPropertiesToSet.contains(Properties.TRANSLATION_X)) {
192            mViewPropertyAnimator.translationX(mTranslationX);
193        }
194        if (mPropertiesToSet.contains(Properties.TRANSLATION_Y)) {
195            mViewPropertyAnimator.translationY(mTranslationY);
196        }
197        if (mPropertiesToSet.contains(Properties.SCALE_X)) {
198            mViewPropertyAnimator.scaleX(mScaleX);
199        }
200        if (mPropertiesToSet.contains(Properties.ROTATION_Y)) {
201            mViewPropertyAnimator.rotationY(mRotationY);
202        }
203        if (mPropertiesToSet.contains(Properties.SCALE_Y)) {
204            mViewPropertyAnimator.scaleY(mScaleY);
205        }
206        if (mPropertiesToSet.contains(Properties.ALPHA)) {
207            mViewPropertyAnimator.alpha(mAlpha);
208        }
209        if (mPropertiesToSet.contains(Properties.START_DELAY)) {
210            mViewPropertyAnimator.setStartDelay(mStartDelay);
211        }
212        if (mPropertiesToSet.contains(Properties.DURATION)) {
213            mViewPropertyAnimator.setDuration(mDuration);
214        }
215        if (mPropertiesToSet.contains(Properties.INTERPOLATOR)) {
216            mViewPropertyAnimator.setInterpolator(mInterpolator);
217        }
218        mViewPropertyAnimator.setListener(this);
219        mViewPropertyAnimator.start();
220    }
221
222    public LauncherViewPropertyAnimator translationX(float value) {
223        mPropertiesToSet.add(Properties.TRANSLATION_X);
224        mTranslationX = value;
225        return this;
226    }
227
228    public LauncherViewPropertyAnimator translationY(float value) {
229        mPropertiesToSet.add(Properties.TRANSLATION_Y);
230        mTranslationY = value;
231        return this;
232    }
233
234    public LauncherViewPropertyAnimator scaleX(float value) {
235        mPropertiesToSet.add(Properties.SCALE_X);
236        mScaleX = value;
237        return this;
238    }
239
240    public LauncherViewPropertyAnimator scaleY(float value) {
241        mPropertiesToSet.add(Properties.SCALE_Y);
242        mScaleY = value;
243        return this;
244    }
245
246    public LauncherViewPropertyAnimator rotationY(float value) {
247        mPropertiesToSet.add(Properties.ROTATION_Y);
248        mRotationY = value;
249        return this;
250    }
251
252    public LauncherViewPropertyAnimator alpha(float value) {
253        mPropertiesToSet.add(Properties.ALPHA);
254        mAlpha = value;
255        return this;
256    }
257}
258