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; |
| 22 | import android.view.ViewPropertyAnimator; |
| 23 | import android.view.View; |
| 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; |
| 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 | mViewPropertyAnimator.cancel(); |
| 71 | } |
| 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() { |
| 85 | return mViewPropertyAnimator.getDuration(); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public ArrayList<Animator.AnimatorListener> getListeners() { |
| 90 | return null; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public long getStartDelay() { |
| 95 | return mViewPropertyAnimator.getStartDelay(); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public void onAnimationCancel(Animator animation) { |
| 100 | if (mListener != null) { |
| 101 | mListener.onAnimationCancel(this); |
| 102 | } |
| 103 | mRunning = false; |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public void onAnimationEnd(Animator animation) { |
| 108 | if (mListener != null) { |
| 109 | mListener.onAnimationEnd(this); |
| 110 | } |
| 111 | mRunning = false; |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public void onAnimationRepeat(Animator animation) { |
| 116 | if (mListener != null) { |
| 117 | mListener.onAnimationRepeat(this); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public void onAnimationStart(Animator animation) { |
| 123 | if (mListener != null) { |
| 124 | mListener.onAnimationStart(this); |
| 125 | } |
| 126 | mRunning = true; |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public boolean isRunning() { |
| 131 | return mRunning; |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public boolean isStarted() { |
| 136 | return mViewPropertyAnimator != null; |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public void removeAllListeners() { |
| 141 | mListener = null; |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public void removeListener(Animator.AnimatorListener listener) { |
| 146 | if (mListener == listener) { |
| 147 | mListener = null; |
| 148 | } else { |
| 149 | throw new RuntimeException("Removing listener that wasn't set"); |
| 150 | } |
| 151 | } |
| 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 | } |