PageRenderTime 148ms CodeModel.GetById 27ms RepoModel.GetById 19ms app.codeStats 1ms

/ABSherlock/src/com/actionbarsherlock/internal/nineoldandroids/view/animation/AnimatorProxy.java

https://bitbucket.org/ayastrebov/android-actionbarsherlock
Java | 205 lines | 181 code | 21 blank | 3 comment | 36 complexity | 871458e88142b41e223ad1bc15827651 MD5 | raw file
  1. package com.actionbarsherlock.internal.nineoldandroids.view.animation;
  2. import java.lang.ref.WeakReference;
  3. import java.util.WeakHashMap;
  4. import android.graphics.Matrix;
  5. import android.graphics.RectF;
  6. import android.os.Build;
  7. import android.util.FloatMath;
  8. import android.view.View;
  9. import android.view.animation.Animation;
  10. import android.view.animation.Transformation;
  11. public final class AnimatorProxy extends Animation {
  12. public static final boolean NEEDS_PROXY = Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB;
  13. private static final WeakHashMap<View, AnimatorProxy> PROXIES =
  14. new WeakHashMap<View, AnimatorProxy>();
  15. public static AnimatorProxy wrap(View view) {
  16. AnimatorProxy proxy = PROXIES.get(view);
  17. if (proxy == null) {
  18. proxy = new AnimatorProxy(view);
  19. PROXIES.put(view, proxy);
  20. }
  21. return proxy;
  22. }
  23. private final WeakReference<View> mView;
  24. private float mAlpha = 1;
  25. private float mScaleX = 1;
  26. private float mScaleY = 1;
  27. private float mTranslationX;
  28. private float mTranslationY;
  29. private final RectF mBefore = new RectF();
  30. private final RectF mAfter = new RectF();
  31. private final Matrix mTempMatrix = new Matrix();
  32. private AnimatorProxy(View view) {
  33. setDuration(0); //perform transformation immediately
  34. setFillAfter(true); //persist transformation beyond duration
  35. view.setAnimation(this);
  36. mView = new WeakReference<View>(view);
  37. }
  38. public float getAlpha() {
  39. return mAlpha;
  40. }
  41. public void setAlpha(float alpha) {
  42. if (mAlpha != alpha) {
  43. mAlpha = alpha;
  44. View view = mView.get();
  45. if (view != null) {
  46. view.invalidate();
  47. }
  48. }
  49. }
  50. public float getScaleX() {
  51. return mScaleX;
  52. }
  53. public void setScaleX(float scaleX) {
  54. if (mScaleX != scaleX) {
  55. prepareForUpdate();
  56. mScaleX = scaleX;
  57. invalidateAfterUpdate();
  58. }
  59. }
  60. public float getScaleY() {
  61. return mScaleY;
  62. }
  63. public void setScaleY(float scaleY) {
  64. if (mScaleY != scaleY) {
  65. prepareForUpdate();
  66. mScaleY = scaleY;
  67. invalidateAfterUpdate();
  68. }
  69. }
  70. public int getScrollX() {
  71. View view = mView.get();
  72. if (view == null) {
  73. return 0;
  74. }
  75. return view.getScrollX();
  76. }
  77. public void setScrollX(int value) {
  78. View view = mView.get();
  79. if (view != null) {
  80. view.scrollTo(value, view.getScrollY());
  81. }
  82. }
  83. public int getScrollY() {
  84. View view = mView.get();
  85. if (view == null) {
  86. return 0;
  87. }
  88. return view.getScrollY();
  89. }
  90. public void setScrollY(int value) {
  91. View view = mView.get();
  92. if (view != null) {
  93. view.scrollTo(view.getScrollY(), value);
  94. }
  95. }
  96. public float getTranslationX() {
  97. return mTranslationX;
  98. }
  99. public void setTranslationX(float translationX) {
  100. if (mTranslationX != translationX) {
  101. prepareForUpdate();
  102. mTranslationX = translationX;
  103. invalidateAfterUpdate();
  104. }
  105. }
  106. public float getTranslationY() {
  107. return mTranslationY;
  108. }
  109. public void setTranslationY(float translationY) {
  110. if (mTranslationY != translationY) {
  111. prepareForUpdate();
  112. mTranslationY = translationY;
  113. invalidateAfterUpdate();
  114. }
  115. }
  116. private void prepareForUpdate() {
  117. View view = mView.get();
  118. if (view != null) {
  119. computeRect(mBefore, view);
  120. }
  121. }
  122. private void invalidateAfterUpdate() {
  123. View view = mView.get();
  124. if (view == null) {
  125. return;
  126. }
  127. View parent = (View)view.getParent();
  128. if (parent == null) {
  129. return;
  130. }
  131. final RectF after = mAfter;
  132. computeRect(after, view);
  133. after.union(mBefore);
  134. parent.invalidate(
  135. (int) FloatMath.floor(after.left),
  136. (int) FloatMath.floor(after.top),
  137. (int) FloatMath.ceil(after.right),
  138. (int) FloatMath.ceil(after.bottom));
  139. }
  140. private void computeRect(final RectF r, View view) {
  141. // compute current rectangle according to matrix transformation
  142. final float w = view.getWidth();
  143. final float h = view.getHeight();
  144. // use a rectangle at 0,0 to make sure we don't run into issues with scaling
  145. r.set(0, 0, w, h);
  146. final Matrix m = mTempMatrix;
  147. m.reset();
  148. transformMatrix(m, view);
  149. mTempMatrix.mapRect(r);
  150. r.offset(view.getLeft(), view.getTop());
  151. // Straighten coords if rotations flipped them
  152. if (r.right < r.left) {
  153. final float f = r.right;
  154. r.right = r.left;
  155. r.left = f;
  156. }
  157. if (r.bottom < r.top) {
  158. final float f = r.top;
  159. r.top = r.bottom;
  160. r.bottom = f;
  161. }
  162. }
  163. private void transformMatrix(Matrix m, View view) {
  164. final float w = view.getWidth();
  165. final float h = view.getHeight();
  166. final float sX = mScaleX;
  167. final float sY = mScaleY;
  168. if ((sX != 1.0f) || (sY != 1.0f)) {
  169. final float deltaSX = ((sX * w) - w) / 2f;
  170. final float deltaSY = ((sY * h) - h) / 2f;
  171. m.postScale(sX, sY);
  172. m.postTranslate(-deltaSX, -deltaSY);
  173. }
  174. m.postTranslate(mTranslationX, mTranslationY);
  175. }
  176. @Override
  177. protected void applyTransformation(float interpolatedTime, Transformation t) {
  178. View view = mView.get();
  179. if (view != null) {
  180. t.setAlpha(mAlpha);
  181. transformMatrix(t.getMatrix(), view);
  182. }
  183. }
  184. }