1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/animation/AnimatorProxy.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,355 @@ 1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.7 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +package org.mozilla.gecko.animation; 1.10 + 1.11 +import android.graphics.Matrix; 1.12 +import android.graphics.RectF; 1.13 +import android.os.Build; 1.14 +import android.view.View; 1.15 +import android.view.ViewGroup; 1.16 +import android.view.animation.Animation; 1.17 +import android.view.animation.AnimationUtils; 1.18 +import android.view.animation.Transformation; 1.19 + 1.20 +import java.lang.ref.WeakReference; 1.21 +import java.util.WeakHashMap; 1.22 + 1.23 +class AnimatorProxy { 1.24 + private static final WeakHashMap<View, AnimatorProxy> PROXIES = 1.25 + new WeakHashMap<View, AnimatorProxy>(); 1.26 + 1.27 + private static interface AnimatorProxyImpl { 1.28 + public float getAlpha(); 1.29 + public void setAlpha(float alpha); 1.30 + 1.31 + public float getTranslationX(); 1.32 + public void setTranslationX(float translationX); 1.33 + 1.34 + public float getTranslationY(); 1.35 + public void setTranslationY(float translationY); 1.36 + 1.37 + public View getView(); 1.38 + } 1.39 + 1.40 + private AnimatorProxyImpl mImpl; 1.41 + 1.42 + private AnimatorProxy(AnimatorProxyImpl impl) { 1.43 + mImpl = impl; 1.44 + } 1.45 + 1.46 + public static AnimatorProxy create(View view) { 1.47 + AnimatorProxy proxy = PROXIES.get(view); 1.48 + boolean needsAnimationProxy = (Build.VERSION.SDK_INT < 11); 1.49 + 1.50 + // If the view's animation proxy has been overridden from somewhere else, we need to 1.51 + // create a new AnimatorProxy for the view. 1.52 + if (proxy == null || (needsAnimationProxy && proxy.mImpl != view.getAnimation())) { 1.53 + AnimatorProxyImpl impl = (needsAnimationProxy ? new AnimatorProxyPreHC(view) : 1.54 + new AnimatorProxyPostHC(view)); 1.55 + 1.56 + proxy = new AnimatorProxy(impl); 1.57 + PROXIES.put(view, proxy); 1.58 + } 1.59 + 1.60 + return proxy; 1.61 + } 1.62 + 1.63 + public int getWidth() { 1.64 + View view = mImpl.getView(); 1.65 + if (view != null) 1.66 + return view.getWidth(); 1.67 + 1.68 + return 0; 1.69 + } 1.70 + 1.71 + public void setWidth(int width) { 1.72 + View view = mImpl.getView(); 1.73 + if (view != null) { 1.74 + ViewGroup.LayoutParams lp = view.getLayoutParams(); 1.75 + lp.width = width; 1.76 + view.setLayoutParams(lp); 1.77 + } 1.78 + } 1.79 + 1.80 + public int getHeight() { 1.81 + View view = mImpl.getView(); 1.82 + if (view != null) 1.83 + return view.getHeight(); 1.84 + 1.85 + return 0; 1.86 + } 1.87 + 1.88 + public void setHeight(int height) { 1.89 + View view = mImpl.getView(); 1.90 + if (view != null) { 1.91 + ViewGroup.LayoutParams lp = view.getLayoutParams(); 1.92 + lp.height = height; 1.93 + view.setLayoutParams(lp); 1.94 + } 1.95 + } 1.96 + 1.97 + public int getScrollX() { 1.98 + View view = mImpl.getView(); 1.99 + if (view != null) 1.100 + return view.getScrollX(); 1.101 + 1.102 + return 0; 1.103 + } 1.104 + 1.105 + public int getScrollY() { 1.106 + View view = mImpl.getView(); 1.107 + if (view != null) 1.108 + return view.getScrollY(); 1.109 + 1.110 + return 0; 1.111 + } 1.112 + 1.113 + public void scrollTo(int scrollX, int scrollY) { 1.114 + View view = mImpl.getView(); 1.115 + if (view != null) 1.116 + view.scrollTo(scrollX, scrollY); 1.117 + } 1.118 + 1.119 + public float getAlpha() { 1.120 + return mImpl.getAlpha(); 1.121 + } 1.122 + 1.123 + public void setAlpha(float alpha) { 1.124 + mImpl.setAlpha(alpha); 1.125 + } 1.126 + 1.127 + public float getTranslationX() { 1.128 + return mImpl.getTranslationX(); 1.129 + } 1.130 + 1.131 + public void setTranslationX(float translationX) { 1.132 + mImpl.setTranslationX(translationX); 1.133 + } 1.134 + 1.135 + public float getTranslationY() { 1.136 + return mImpl.getTranslationY(); 1.137 + } 1.138 + 1.139 + public void setTranslationY(float translationY) { 1.140 + mImpl.setTranslationY(translationY); 1.141 + } 1.142 + 1.143 + /* 1.144 + * AnimatorProxyPreHC uses the technique used by the NineOldAndroids described here: 1.145 + * http://jakewharton.com/advanced-pre-honeycomb-animation/ 1.146 + * 1.147 + * Some of this code is based on Jake Wharton's AnimatorProxy released as part of 1.148 + * the NineOldAndroids library under the Apache License 2.0. 1.149 + */ 1.150 + private static class AnimatorProxyPreHC extends Animation implements AnimatorProxyImpl { 1.151 + private WeakReference<View> mViewRef; 1.152 + 1.153 + private final RectF mBefore; 1.154 + private final RectF mAfter; 1.155 + private final Matrix mTempMatrix; 1.156 + 1.157 + private float mAlpha; 1.158 + private float mTranslationX; 1.159 + private float mTranslationY; 1.160 + 1.161 + public AnimatorProxyPreHC(View view) { 1.162 + mBefore = new RectF(); 1.163 + mAfter = new RectF(); 1.164 + mTempMatrix = new Matrix(); 1.165 + 1.166 + mAlpha = 1; 1.167 + mTranslationX = 0; 1.168 + mTranslationY = 0; 1.169 + 1.170 + loadCurrentTransformation(view); 1.171 + 1.172 + setDuration(0); 1.173 + setFillAfter(true); 1.174 + view.setAnimation(this); 1.175 + 1.176 + mViewRef = new WeakReference<View>(view); 1.177 + } 1.178 + 1.179 + private void loadCurrentTransformation(View view) { 1.180 + Animation animation = view.getAnimation(); 1.181 + if (animation == null) 1.182 + return; 1.183 + 1.184 + Transformation transformation = new Transformation(); 1.185 + float[] matrix = new float[9]; 1.186 + 1.187 + animation.getTransformation(AnimationUtils.currentAnimationTimeMillis(), transformation); 1.188 + transformation.getMatrix().getValues(matrix); 1.189 + 1.190 + mAlpha = transformation.getAlpha(); 1.191 + mTranslationX = matrix[Matrix.MTRANS_X]; 1.192 + mTranslationY = matrix[Matrix.MTRANS_Y]; 1.193 + } 1.194 + 1.195 + private void prepareForUpdate() { 1.196 + View view = mViewRef.get(); 1.197 + if (view != null) 1.198 + computeRect(mBefore, view); 1.199 + } 1.200 + 1.201 + private void computeRect(final RectF r, View view) { 1.202 + final float w = view.getWidth(); 1.203 + final float h = view.getHeight(); 1.204 + 1.205 + r.set(0, 0, w, h); 1.206 + 1.207 + final Matrix m = mTempMatrix; 1.208 + m.reset(); 1.209 + transformMatrix(m, view); 1.210 + mTempMatrix.mapRect(r); 1.211 + 1.212 + r.offset(view.getLeft(), view.getTop()); 1.213 + } 1.214 + 1.215 + private void transformMatrix(Matrix m, View view) { 1.216 + m.postTranslate(mTranslationX, mTranslationY); 1.217 + } 1.218 + 1.219 + private void invalidateAfterUpdate() { 1.220 + View view = mViewRef.get(); 1.221 + if (view == null || view.getParent() == null) 1.222 + return; 1.223 + 1.224 + final RectF after = mAfter; 1.225 + computeRect(after, view); 1.226 + after.union(mBefore); 1.227 + 1.228 + ((View)view.getParent()).invalidate( 1.229 + (int) Math.floor(after.left), 1.230 + (int) Math.floor(after.top), 1.231 + (int) Math.ceil(after.right), 1.232 + (int) Math.ceil(after.bottom)); 1.233 + } 1.234 + 1.235 + @Override 1.236 + public float getAlpha() { 1.237 + return mAlpha; 1.238 + } 1.239 + 1.240 + @Override 1.241 + public void setAlpha(float alpha) { 1.242 + if (mAlpha == alpha) 1.243 + return; 1.244 + 1.245 + mAlpha = alpha; 1.246 + 1.247 + View view = mViewRef.get(); 1.248 + if (view != null) 1.249 + view.invalidate(); 1.250 + } 1.251 + 1.252 + @Override 1.253 + public float getTranslationX() { 1.254 + return mTranslationX; 1.255 + } 1.256 + 1.257 + @Override 1.258 + public void setTranslationX(float translationX) { 1.259 + if (mTranslationX == translationX) 1.260 + return; 1.261 + 1.262 + prepareForUpdate(); 1.263 + mTranslationX = translationX; 1.264 + invalidateAfterUpdate(); 1.265 + } 1.266 + 1.267 + @Override 1.268 + public float getTranslationY() { 1.269 + return mTranslationY; 1.270 + } 1.271 + 1.272 + @Override 1.273 + public void setTranslationY(float translationY) { 1.274 + if (mTranslationY == translationY) 1.275 + return; 1.276 + 1.277 + prepareForUpdate(); 1.278 + mTranslationY = translationY; 1.279 + invalidateAfterUpdate(); 1.280 + } 1.281 + 1.282 + @Override 1.283 + public View getView() { 1.284 + return mViewRef.get(); 1.285 + } 1.286 + 1.287 + @Override 1.288 + protected void applyTransformation(float interpolatedTime, Transformation t) { 1.289 + View view = mViewRef.get(); 1.290 + if (view != null) { 1.291 + t.setAlpha(mAlpha); 1.292 + transformMatrix(t.getMatrix(), view); 1.293 + } 1.294 + } 1.295 + } 1.296 + 1.297 + private static class AnimatorProxyPostHC implements AnimatorProxyImpl { 1.298 + private WeakReference<View> mViewRef; 1.299 + 1.300 + public AnimatorProxyPostHC(View view) { 1.301 + mViewRef = new WeakReference<View>(view); 1.302 + } 1.303 + 1.304 + @Override 1.305 + public float getAlpha() { 1.306 + View view = mViewRef.get(); 1.307 + if (view != null) 1.308 + return view.getAlpha(); 1.309 + 1.310 + return 1; 1.311 + } 1.312 + 1.313 + @Override 1.314 + public void setAlpha(float alpha) { 1.315 + View view = mViewRef.get(); 1.316 + if (view != null) 1.317 + view.setAlpha(alpha); 1.318 + } 1.319 + 1.320 + @Override 1.321 + public float getTranslationX() { 1.322 + View view = mViewRef.get(); 1.323 + if (view != null) 1.324 + return view.getTranslationX(); 1.325 + 1.326 + return 0; 1.327 + } 1.328 + 1.329 + @Override 1.330 + public void setTranslationX(float translationX) { 1.331 + View view = mViewRef.get(); 1.332 + if (view != null) 1.333 + view.setTranslationX(translationX); 1.334 + } 1.335 + 1.336 + @Override 1.337 + public float getTranslationY() { 1.338 + View view = mViewRef.get(); 1.339 + if (view != null) 1.340 + return view.getTranslationY(); 1.341 + 1.342 + return 0; 1.343 + } 1.344 + 1.345 + @Override 1.346 + public void setTranslationY(float translationY) { 1.347 + View view = mViewRef.get(); 1.348 + if (view != null) 1.349 + view.setTranslationY(translationY); 1.350 + } 1.351 + 1.352 + @Override 1.353 + public View getView() { 1.354 + return mViewRef.get(); 1.355 + } 1.356 + } 1.357 +} 1.358 +