1 package com.actionbarsherlock.internal.nineoldandroids.view.animation;
3 import java.lang.ref.WeakReference;
4 import java.util.WeakHashMap;
5 import android.graphics.Matrix;
6 import android.graphics.RectF;
7 import android.os.Build;
8 import android.util.FloatMath;
9 import android.view.View;
10 import android.view.animation.Animation;
11 import android.view.animation.Transformation;
13 public final class AnimatorProxy extends Animation {
14 public static final boolean NEEDS_PROXY = Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB;
16 private static final WeakHashMap<View, AnimatorProxy> PROXIES =
17 new WeakHashMap<View, AnimatorProxy>();
19 public static AnimatorProxy wrap(View view) {
20 AnimatorProxy proxy = PROXIES.get(view);
22 proxy = new AnimatorProxy(view);
23 PROXIES.put(view, proxy);
28 private final WeakReference<View> mView;
30 private float mAlpha = 1;
31 private float mScaleX = 1;
32 private float mScaleY = 1;
33 private float mTranslationX;
34 private float mTranslationY;
36 private final RectF mBefore = new RectF();
37 private final RectF mAfter = new RectF();
38 private final Matrix mTempMatrix = new Matrix();
40 private AnimatorProxy(View view) {
41 setDuration(0); //perform transformation immediately
42 setFillAfter(true); //persist transformation beyond duration
43 view.setAnimation(this);
44 mView = new WeakReference<View>(view);
47 public float getAlpha() {
50 public void setAlpha(float alpha) {
51 if (mAlpha != alpha) {
53 View view = mView.get();
59 public float getScaleX() {
62 public void setScaleX(float scaleX) {
63 if (mScaleX != scaleX) {
66 invalidateAfterUpdate();
69 public float getScaleY() {
72 public void setScaleY(float scaleY) {
73 if (mScaleY != scaleY) {
76 invalidateAfterUpdate();
79 public int getScrollX() {
80 View view = mView.get();
84 return view.getScrollX();
86 public void setScrollX(int value) {
87 View view = mView.get();
89 view.scrollTo(value, view.getScrollY());
92 public int getScrollY() {
93 View view = mView.get();
97 return view.getScrollY();
99 public void setScrollY(int value) {
100 View view = mView.get();
102 view.scrollTo(view.getScrollY(), value);
106 public float getTranslationX() {
107 return mTranslationX;
109 public void setTranslationX(float translationX) {
110 if (mTranslationX != translationX) {
112 mTranslationX = translationX;
113 invalidateAfterUpdate();
116 public float getTranslationY() {
117 return mTranslationY;
119 public void setTranslationY(float translationY) {
120 if (mTranslationY != translationY) {
122 mTranslationY = translationY;
123 invalidateAfterUpdate();
127 private void prepareForUpdate() {
128 View view = mView.get();
130 computeRect(mBefore, view);
133 private void invalidateAfterUpdate() {
134 View view = mView.get();
138 View parent = (View)view.getParent();
139 if (parent == null) {
143 view.setAnimation(this);
145 final RectF after = mAfter;
146 computeRect(after, view);
147 after.union(mBefore);
150 (int) FloatMath.floor(after.left),
151 (int) FloatMath.floor(after.top),
152 (int) FloatMath.ceil(after.right),
153 (int) FloatMath.ceil(after.bottom));
156 private void computeRect(final RectF r, View view) {
157 // compute current rectangle according to matrix transformation
158 final float w = view.getWidth();
159 final float h = view.getHeight();
161 // use a rectangle at 0,0 to make sure we don't run into issues with scaling
164 final Matrix m = mTempMatrix;
166 transformMatrix(m, view);
167 mTempMatrix.mapRect(r);
169 r.offset(view.getLeft(), view.getTop());
171 // Straighten coords if rotations flipped them
172 if (r.right < r.left) {
173 final float f = r.right;
177 if (r.bottom < r.top) {
178 final float f = r.top;
184 private void transformMatrix(Matrix m, View view) {
185 final float w = view.getWidth();
186 final float h = view.getHeight();
188 final float sX = mScaleX;
189 final float sY = mScaleY;
190 if ((sX != 1.0f) || (sY != 1.0f)) {
191 final float deltaSX = ((sX * w) - w) / 2f;
192 final float deltaSY = ((sY * h) - h) / 2f;
194 m.postTranslate(-deltaSX, -deltaSY);
196 m.postTranslate(mTranslationX, mTranslationY);
200 protected void applyTransformation(float interpolatedTime, Transformation t) {
201 View view = mView.get();
204 transformMatrix(t.getMatrix(), view);
209 public void reset() {