Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(246)

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/contextmenu/TabularContextMenuViewPager.java

Issue 2960743002: Adding animations for view pager transitions (Closed)
Patch Set: Requests layout only when transitioning to a smaller size Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/android/java/res/layout/tabular_context_menu.xml ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.chrome.browser.contextmenu; 5 package org.chromium.chrome.browser.contextmenu;
6 6
7 import android.animation.Animator;
8 import android.animation.AnimatorListenerAdapter;
9 import android.animation.ValueAnimator;
10 import android.animation.ValueAnimator.AnimatorUpdateListener;
7 import android.content.Context; 11 import android.content.Context;
12 import android.graphics.Canvas;
13 import android.graphics.Rect;
14 import android.graphics.drawable.Drawable;
8 import android.support.v4.view.ViewPager; 15 import android.support.v4.view.ViewPager;
16 import android.support.v4.view.animation.LinearOutSlowInInterpolator;
9 import android.util.AttributeSet; 17 import android.util.AttributeSet;
10 import android.view.View; 18 import android.view.View;
11 19
20 import org.chromium.base.ApiCompatibilityUtils;
12 import org.chromium.chrome.R; 21 import org.chromium.chrome.R;
22 import org.chromium.chrome.browser.util.MathUtils;
13 23
14 /** 24 /**
15 * When there is more than one view for the context menu to display, it wraps th e display in a view 25 * When there is more than one view for the context menu to display, it wraps th e display in a view
16 * pager. 26 * pager.
17 */ 27 */
18 public class TabularContextMenuViewPager extends ViewPager { 28 public class TabularContextMenuViewPager extends ViewPager {
29 private static final int ANIMATION_DURATION_MS = 250;
30
31 private ValueAnimator mAnimator;
32 private int mOldHeight;
33 private int mCanvasWidth;
34 private int mClipHeight;
35
36 private int mDifferenceInHeight = 0;
37 private int mPreviousChildIndex = 1;
19 private final int mContextMenuMinimumPaddingPx = 38 private final int mContextMenuMinimumPaddingPx =
20 getResources().getDimensionPixelSize(R.dimen.context_menu_min_paddin g); 39 getResources().getDimensionPixelSize(R.dimen.context_menu_min_paddin g);
40 private Canvas mCanvas;
41 private final Drawable mDrawable = ApiCompatibilityUtils.getDrawable(
42 getResources(), R.drawable.white_with_rounded_corners);
21 43
22 public TabularContextMenuViewPager(Context context) { 44 public TabularContextMenuViewPager(Context context) {
23 super(context); 45 super(context);
46 mDrawable.mutate();
24 } 47 }
25 48
26 public TabularContextMenuViewPager(Context context, AttributeSet attrs) { 49 public TabularContextMenuViewPager(Context context, AttributeSet attrs) {
27 super(context, attrs); 50 super(context, attrs);
51 mDrawable.mutate();
28 } 52 }
29 53
30 /** 54 /**
31 * Used to show the full ViewPager dialog. Without this the dialog would hav e no height or 55 * Used to show the full ViewPager dialog. Without this the dialog would hav e no height or
32 * width. 56 * width.
33 */ 57 */
34 @Override 58 @Override
35 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 59 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
60 int tabHeight = 0;
36 int menuHeight = 0; 61 int menuHeight = 0;
37 int tabHeight = 0;
38
39 // getCurrentItem() does not take into account the tab layout unlike get ChildCount().
40 int currentItemsIndex = getCurrentItem() + 1;
41 62
42 // The width of the context menu is defined so that it leaves space betw een itself and the 63 // The width of the context menu is defined so that it leaves space betw een itself and the
43 // screen's edges. It is also bounded to a max size to prevent the menu from stretching 64 // screen's edges. It is also bounded to a max size to prevent the menu from stretching
44 // across a large display (e.g. a tablet screen). 65 // across a large display (e.g. a tablet screen).
45 int deviceWidthPx = getResources().getDisplayMetrics().widthPixels; 66 int appWindowWidthPx = getResources().getDisplayMetrics().widthPixels;
46 int contextMenuWidth = Math.min(deviceWidthPx - 2 * mContextMenuMinimumP addingPx, 67 int contextMenuWidth = Math.min(appWindowWidthPx - 2 * mContextMenuMinim umPaddingPx,
47 getResources().getDimensionPixelSize(R.dimen.context_menu_max_wi dth)); 68 getResources().getDimensionPixelSize(R.dimen.context_menu_max_wi dth));
48 69
49 widthMeasureSpec = MeasureSpec.makeMeasureSpec(contextMenuWidth, Measure Spec.EXACTLY); 70 widthMeasureSpec = MeasureSpec.makeMeasureSpec(contextMenuWidth, Measure Spec.EXACTLY);
50 71
51 // The height of the context menu is calculated as the sum of: 72 // getCurrentItem() does not take into account the tab layout unlike get ChildCount().
52 // 1. The tab bar's height, which is only visible when the context menu requires it 73 int currentChildIndex = getCurrentItem() + 1;
53 // (i.e. an ImageLink is clicked)
54 // 2. The height of the View being displayed for the current tab.
55 for (int i = 0; i < getChildCount(); i++) {
56 View child = getChildAt(i);
57 74
58 child.measure( 75 // Handles the case where this is called while the pager is scrolling be tween views.
59 widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec .UNSPECIFIED)); 76 if (getScrollX() != 0 && getScrollX() != mCanvasWidth) {
60 int measuredHeight = child.getMeasuredHeight(); 77 heightMeasureSpec = MeasureSpec.makeMeasureSpec(mOldHeight, MeasureS pec.EXACTLY);
78 } else {
79 // The height of the context menu is calculated as the sum of:
80 // 1. The tab bar's height, which is only visible when the context m enu requires it
81 // (i.e. an ImageLink is clicked)
82 // 2. The height of the View being displayed for the current tab.
83 for (int i = 0; i < getChildCount(); i++) {
84 View child = getChildAt(i);
85 child.measure(
86 widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, Measure Spec.UNSPECIFIED));
87 int measuredHeight = child.getMeasuredHeight();
61 88
62 // The ViewPager also considers the tab layout one of its children, and needs to be 89 // The ViewPager also considers the tab layout one of its childr en, and needs to be
63 // treated separately from getting the largest height. 90 // treated separately from getting the largest height.
64 if (child.getId() == R.id.tab_layout && child.getVisibility() != GON E) { 91 if (child.getId() == R.id.tab_layout && child.getVisibility() != GONE) {
65 tabHeight = measuredHeight; 92 tabHeight = measuredHeight;
66 } else if (i == currentItemsIndex) { 93 } else if (i == currentChildIndex) {
67 menuHeight = child.getMeasuredHeight(); 94 menuHeight = child.getMeasuredHeight();
68 break; 95 break;
96 }
97 }
98 int fullHeight = menuHeight + tabHeight;
99 int appWindowHeightPx = getResources().getDisplayMetrics().heightPix els;
100 fullHeight = Math.min(fullHeight, appWindowHeightPx - 2 * mContextMe nuMinimumPaddingPx);
101 mDifferenceInHeight = fullHeight - mOldHeight;
102
103 if (currentChildIndex == mPreviousChildIndex) {
104 // Handles the snapping and re-clipping of the view when its hei ght changes
105 // (i.e. an image finished loading or the link became fully visi ble).
106 mClipHeight = fullHeight;
107 if (menuHeight != 0) mOldHeight = fullHeight;
108 heightMeasureSpec = MeasureSpec.makeMeasureSpec(fullHeight, Meas ureSpec.EXACTLY);
109 // Clip logic is not called on orientation changes, so it must b e set here.
110 setClipDimensionsForResize(contextMenuWidth);
111 } else {
112 // Handles the case where the view pager has completely scrolled to a different
113 // child.
114 initAnimator();
115 heightMeasureSpec = MeasureSpec.makeMeasureSpec(
116 Math.max(mOldHeight, fullHeight), MeasureSpec.EXACTLY);
117 mAnimator.start();
69 } 118 }
70 } 119 }
120 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
121 mPreviousChildIndex = currentChildIndex;
122 }
71 123
72 // Cap the height of the context menu so that it fits on the screen with out touching the 124 public void setClipDimensionsForResize(int width) {
73 // screen's edges. 125 if (mCanvas != null) mCanvas.clipRect(new Rect(0, 0, width, mClipHeight) );
Theresa 2017/07/19 15:23:20 Is this needed now that you're setting the clipRec
Daniel Park 2017/07/19 20:26:44 Removed.
74 int fullHeight = menuHeight + tabHeight; 126 }
75 int deviceHeightPx = getResources().getDisplayMetrics().heightPixels;
76 fullHeight = Math.min(fullHeight, deviceHeightPx - 2 * mContextMenuMinim umPaddingPx);
77 127
78 heightMeasureSpec = MeasureSpec.makeMeasureSpec(fullHeight, MeasureSpec. EXACTLY); 128 private void initAnimator() {
79 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 129 if (mAnimator != null) return;
130 mAnimator = ValueAnimator.ofFloat(0f, 1f);
131 mAnimator.setDuration(ANIMATION_DURATION_MS);
132 mAnimator.setInterpolator(new LinearOutSlowInInterpolator());
133 mAnimator.addUpdateListener(new AnimatorUpdateListener() {
134
135 @Override
136 public void onAnimationUpdate(ValueAnimator animation) {
137 float animatedValue = (float) animation.getAnimatedValue();
138 // If the animation duration is long enough (e.g. 5x, 10x), anim atedValue will
139 // undesirably be set to 1f more than once.
140 if (mDifferenceInHeight < 0) {
141 setTranslationY(animatedValue * -mDifferenceInHeight / 2);
142 } else {
143 setTranslationY((1 - animatedValue) * mDifferenceInHeight / 2);
144 }
145 mClipHeight = mOldHeight + (int) (mDifferenceInHeight * animated Value);
146 invalidate();
147 if (MathUtils.areFloatsEqual(animatedValue, 1f)) {
148 return;
149 }
150 }
151 });
152 mAnimator.addListener(new AnimatorListenerAdapter() {
153 @Override
154 public void onAnimationEnd(Animator animation) {
155 mOldHeight = mClipHeight;
156 setTranslationY(0);
157 if (mDifferenceInHeight < 0) requestLayout();
158 }
159 });
160 }
161
162 @Override
163 public void onDraw(Canvas canvas) {
164 mCanvasWidth = canvas.getWidth();
165 int backgroundOffsetX = getScrollX();
166 mDrawable.setBounds(
167 backgroundOffsetX, 0, canvas.getWidth() + backgroundOffsetX, mCl ipHeight);
168 mDrawable.draw(canvas);
169 boolean clipped = false;
170 if (mClipHeight != 0) {
171 canvas.save();
172 canvas.clipRect(0, 0, mCanvasWidth, mClipHeight);
173 clipped = true;
174 }
175 super.onDraw(canvas);
176 if (clipped) {
177 canvas.restore();
178 }
179 mCanvas = canvas;
80 } 180 }
81 } 181 }
OLDNEW
« no previous file with comments | « chrome/android/java/res/layout/tabular_context_menu.xml ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698