Bug 1098240 - Use hw layers in tab strip animations (r=mcomella)
authorLucas Rocha <lucasr@lucasr.org>
Tue, 18 Nov 2014 11:44:38 +0000
changeset 240573 1a271fff9c6ad30104864ca7c5b7ce5e1f628687
parent 240438 77a882455945e7852354601085cf1e7252c43a30
child 240574 e66f10fd8921bdc5a79e02922699fcc7c413a26b
push id4311
push userraliiev@mozilla.com
push dateMon, 12 Jan 2015 19:37:41 +0000
treeherdermozilla-beta@150c9fed433b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmcomella
bugs1098240
milestone36.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1098240 - Use hw layers in tab strip animations (r=mcomella)
mobile/android/base/tabs/TabStripView.java
--- a/mobile/android/base/tabs/TabStripView.java
+++ b/mobile/android/base/tabs/TabStripView.java
@@ -13,16 +13,17 @@ import android.graphics.drawable.Drawabl
 import android.util.AttributeSet;
 import android.view.animation.DecelerateInterpolator;
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.ViewTreeObserver;
 import android.view.ViewTreeObserver.OnPreDrawListener;
 
 import com.nineoldandroids.animation.Animator;
+import com.nineoldandroids.animation.Animator.AnimatorListener;
 import com.nineoldandroids.animation.AnimatorSet;
 import com.nineoldandroids.animation.ObjectAnimator;
 
 import java.util.ArrayList;
 import java.util.List;
 
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.Tab;
@@ -34,16 +35,18 @@ public class TabStripView extends TwoWay
 
     private static final int ANIM_TIME_MS = 200;
     private static final DecelerateInterpolator ANIM_INTERPOLATOR =
             new DecelerateInterpolator();
 
     private final TabStripAdapter adapter;
     private final Drawable divider;
 
+    private final TabAnimatorListener animatorListener;
+
     // Filled by calls to ShapeDrawable.getPadding();
     // saved to prevent allocation in draw().
     private final Rect dividerPadding = new Rect();
 
     private boolean isPrivate;
 
     public TabStripView(Context context, AttributeSet attrs) {
         super(context, attrs);
@@ -58,16 +61,18 @@ public class TabStripView extends TwoWay
 
         divider = resources.getDrawable(R.drawable.new_tablet_tab_strip_divider);
         divider.getPadding(dividerPadding);
 
         final int itemMargin =
                 resources.getDimensionPixelSize(R.dimen.new_tablet_tab_strip_item_margin);
         setItemMargin(itemMargin);
 
+        animatorListener = new TabAnimatorListener();
+
         adapter = new TabStripAdapter(context);
         setAdapter(adapter);
     }
 
     private View getViewForTab(Tab tab) {
         final int position = adapter.getPositionForTab(tab);
         return getChildAt(position - getFirstVisiblePosition());
     }
@@ -124,16 +129,17 @@ public class TabStripView extends TwoWay
                             ObjectAnimator.ofFloat(child, "translationX", removedSize, 0);
                     childAnimators.add(animator);
                 }
 
                 final AnimatorSet animatorSet = new AnimatorSet();
                 animatorSet.playTogether(childAnimators);
                 animatorSet.setDuration(ANIM_TIME_MS);
                 animatorSet.setInterpolator(ANIM_INTERPOLATOR);
+                animatorSet.addListener(animatorListener);
                 animatorSet.start();
 
                 return true;
             }
         });
     }
 
     private void animateNewTab(Tab newTab) {
@@ -172,16 +178,17 @@ public class TabStripView extends TwoWay
                     childAnimators.add(
                         ObjectAnimator.ofFloat(child, "translationX", -tabSize, 0));
                 }
 
                 final AnimatorSet animatorSet = new AnimatorSet();
                 animatorSet.playTogether(childAnimators);
                 animatorSet.setDuration(ANIM_TIME_MS);
                 animatorSet.setInterpolator(ANIM_INTERPOLATOR);
+                animatorSet.addListener(animatorListener);
                 animatorSet.start();
 
                 return true;
             }
         });
     }
 
     private void ensurePositionIsVisible(final int position) {
@@ -297,9 +304,38 @@ public class TabStripView extends TwoWay
 
             final int left = child.getLeft() - (itemMargin / 2) - dividerWidth;
             final int right = left + dividerWidth;
 
             divider.setBounds(left, top, right, bottom);
             divider.draw(canvas);
         }
     }
+
+    private class TabAnimatorListener implements AnimatorListener {
+        private void setLayerType(int layerType) {
+            final int childCount = getChildCount();
+            for (int i = 0; i < childCount; i++) {
+                getChildAt(i).setLayerType(layerType, null);
+            }
+        }
+
+        @Override
+        public void onAnimationStart(Animator animation) {
+            setLayerType(View.LAYER_TYPE_HARDWARE);
+        }
+
+        @Override
+        public void onAnimationEnd(Animator animation) {
+            // This method is called even if the animator is canceled.
+            setLayerType(View.LAYER_TYPE_NONE);
+        }
+
+        @Override
+        public void onAnimationRepeat(Animator animation) {
+        }
+
+        @Override
+        public void onAnimationCancel(Animator animation) {
+        }
+
+    }
 }
\ No newline at end of file