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

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/contextmenu/TabularContextMenuUi.java

Issue 2868403003: added scale animation for context menu (Closed)
Patch Set: y Created 3 years, 7 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 side-by-side diff with in-line comments
Download patch
Index: chrome/android/java/src/org/chromium/chrome/browser/contextmenu/TabularContextMenuUi.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/contextmenu/TabularContextMenuUi.java b/chrome/android/java/src/org/chromium/chrome/browser/contextmenu/TabularContextMenuUi.java
index 09428c07607022ac3b53855c70553f103e64cdfe..70a7426130b5204cf930e08be496299c89a4383f 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/contextmenu/TabularContextMenuUi.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/contextmenu/TabularContextMenuUi.java
@@ -10,19 +10,29 @@ import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
+import android.graphics.Color;
+import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
+import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
+import android.support.v4.view.animation.LinearOutSlowInInterpolator;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
+import android.view.View.OnLayoutChangeListener;
import android.view.ViewGroup;
+import android.view.ViewGroup.LayoutParams;
+import android.view.Window;
+import android.view.animation.Animation;
+import android.view.animation.ScaleAnimation;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
+import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
@@ -31,6 +41,8 @@ import org.chromium.base.ApiCompatibilityUtils;
import org.chromium.base.Callback;
import org.chromium.base.VisibleForTesting;
import org.chromium.chrome.R;
+import org.chromium.chrome.browser.widget.ContextMenuDialog;
+import org.chromium.content.browser.RenderCoordinates;
import java.util.ArrayList;
import java.util.List;
@@ -44,21 +56,27 @@ public class TabularContextMenuUi implements ContextMenuUi, AdapterView.OnItemCl
private int mMenuItemHeight;
private ImageView mHeaderImageView;
private Runnable mOnShareItemClicked;
+ private View mPagerView;
+
+ private float mContextMenuSourceX;
+ private float mContextMenuSourceY;
+ private int mContextMenuYLocationOnScreen;
+ private RenderCoordinates mRenderCoordinates;
+
+ private static final double MAX_WIDTH_PROPORTION = 0.75;
+ private static final int ANIMATION_IN_DURATION = 250;
public TabularContextMenuUi(Runnable onShareItemClicked) {
mOnShareItemClicked = onShareItemClicked;
}
@Override
- public void displayMenu(Activity activity, ContextMenuParams params,
+ public void displayMenu(final Activity activity, ContextMenuParams params,
List<Pair<Integer, List<ContextMenuItem>>> items, Callback<Integer> onItemClicked,
final Runnable onMenuShown, final Runnable onMenuClosed) {
mCallback = onItemClicked;
mDialog = createDialog(activity, params, items);
- mDialog.getWindow().setBackgroundDrawable(ApiCompatibilityUtils.getDrawable(
- activity.getResources(), R.drawable.white_with_rounded_corners));
-
mDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
@@ -73,11 +91,42 @@ public class TabularContextMenuUi implements ContextMenuUi, AdapterView.OnItemCl
}
});
+ Window dialogWindow = mDialog.getWindow();
+ dialogWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
+ dialogWindow.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
+
+ float density = Resources.getSystem().getDisplayMetrics().density;
+ final float touchPointX = params.getX() * density;
+ final float touchPointY = params.getY() * density;
+
+ mPagerView.setVisibility(View.INVISIBLE);
+ mPagerView.addOnLayoutChangeListener(new OnLayoutChangeListener() {
+
+ @Override
+ public void onLayoutChange(View v, int left, int top, int right, int bottom,
+ int oldLeft, int oldTop, int oldRight, int oldBottom) {
+ ViewGroup group = (ViewGroup) v;
+ for (int i = 0; i < group.getChildCount(); i++) {
+ if (group.getChildAt(i).getHeight() == 0
+ && group.getChildAt(i).getVisibility() == View.VISIBLE) {
+ // Return early because not all the views have been measured, so animations
+ // pivots will be off.
+ return;
+ }
+ }
+ initializeAnimationAndRevealView(touchPointX, touchPointY, activity);
+ mPagerView.removeOnLayoutChangeListener(this);
+ ((ContextMenuDialog) mDialog)
+ .setAnimationOutParameters(mPagerView, mContextMenuSourceX,
+ mContextMenuSourceY, mContextMenuYLocationOnScreen);
+ }
+ });
mDialog.show();
}
/**
* Returns the fully complete dialog based off the params and the itemGroups.
+ *
* @param activity Used to inflate the dialog.
* @param params Used to get the header title.
* @param itemGroups If there is more than one group it will create a paged view.
@@ -86,24 +135,42 @@ public class TabularContextMenuUi implements ContextMenuUi, AdapterView.OnItemCl
*/
private Dialog createDialog(Activity activity, ContextMenuParams params,
List<Pair<Integer, List<ContextMenuItem>>> itemGroups) {
- Dialog dialog = new Dialog(activity);
- dialog.setContentView(createPagerView(activity, params, itemGroups));
+ final Dialog dialog = new ContextMenuDialog(activity, R.style.DialogWhenLarge);
+
+ View view = LayoutInflater.from(activity).inflate(R.layout.tabular_context_menu, null);
+
+ Resources resources = activity.getResources();
+ FrameLayout fullContainer = new FrameLayout(activity);
Theresa 2017/05/24 17:53:33 Remove this and the line below since the FrameLayo
Daniel Park 2017/05/24 19:44:39 Done.
+ fullContainer.setBackgroundColor(
+ ApiCompatibilityUtils.getColor(resources, R.color.modal_dialog_scrim_color));
+
+ int contextMenuWidth =
+ (int) Math.min(resources.getDisplayMetrics().widthPixels * MAX_WIDTH_PROPORTION,
+ resources.getDimensionPixelSize(R.dimen.context_menu_max_width));
+
+ mPagerView =
+ initPagerView(activity, params, itemGroups, view.findViewById(R.id.custom_pager));
+ mPagerView.getLayoutParams().width = contextMenuWidth;
+
+ dialog.addContentView(view.findViewById(R.id.context_menu_frame_layout),
Theresa 2017/05/24 17:53:34 view should already be the FrameLayout associated
Daniel Park 2017/05/24 19:44:39 Done.
+ new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
+
return dialog;
}
/**
* Creates a ViewPageAdapter based off the given list of views.
- * @param activity Used to inflate the new ViewPager
+ *
+ * @param activity Used to inflate the new ViewPager.
* @param params Used to get the header text.
* @param itemGroups The list of views to put into the ViewPager. The string is the title of the
- * tab
+ * tab.
Theresa 2017/05/24 17:53:34 Did git cl format do this? Ideally this line would
Daniel Park 2017/05/24 19:44:39 Done.
+ * @param view The pager view to initialize.
* @return Returns a complete tabular context menu view.
*/
@VisibleForTesting
- View createPagerView(Activity activity, ContextMenuParams params,
- List<Pair<Integer, List<ContextMenuItem>>> itemGroups) {
- View view = LayoutInflater.from(activity).inflate(R.layout.tabular_context_menu, null);
-
+ View initPagerView(Activity activity, ContextMenuParams params,
+ List<Pair<Integer, List<ContextMenuItem>>> itemGroups, View view) {
Theresa 2017/05/24 17:53:33 Pass TabularContextMenuViewPager (and cast in the
Daniel Park 2017/05/24 19:44:39 Done.
List<Pair<String, ViewGroup>> viewGroups = new ArrayList<>();
int maxCount = 0;
for (int i = 0; i < itemGroups.size(); i++) {
@@ -118,22 +185,15 @@ public class TabularContextMenuUi implements ContextMenuUi, AdapterView.OnItemCl
createContextMenuPageUi(
activity, params, itemGroup.second, isImageTab, maxCount)));
}
- if (itemGroups.size() == 1) {
- viewGroups.get(0)
- .second.getChildAt(0)
- .findViewById(R.id.context_header_layout)
- .setBackgroundResource(R.color.google_grey_100);
- }
- TabularContextMenuViewPager pager =
- (TabularContextMenuViewPager) view.findViewById(R.id.custom_pager);
+ TabularContextMenuViewPager pager = (TabularContextMenuViewPager) view;
pager.setAdapter(new TabularContextMenuPagerAdapter(viewGroups));
-
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
if (itemGroups.size() <= 1) {
tabLayout.setVisibility(View.GONE);
} else {
- tabLayout.setupWithViewPager((ViewPager) view.findViewById(R.id.custom_pager));
+ tabLayout.setBackgroundResource(R.drawable.grey_with_top_rounded_corners);
+ tabLayout.setupWithViewPager((ViewPager) view);
}
return view;
@@ -142,13 +202,14 @@ public class TabularContextMenuUi implements ContextMenuUi, AdapterView.OnItemCl
/**
* Creates the view of a context menu. Based off the Context Type, it'll adjust the list of
* items and display only the ones that'll be on that specific group.
+ *
* @param activity Used to get the resources of an item.
* @param params used to create the header text.
* @param items A set of Items to display in a context menu. Filtered based off the type.
* @param isImage Whether or not the view should have an image layout or not.
* @param maxCount The maximum amount of {@link ContextMenuItem}s that could exist in this view
- * or any other views calculated in the context menu. Used to estimate the size
- * of the list.
+ * or any other views calculated in the context menu. Used to estimate the size of
+ * the list.
* @return Returns a filled LinearLayout with all the context menu items.
*/
@VisibleForTesting
@@ -245,6 +306,7 @@ public class TabularContextMenuUi implements ContextMenuUi, AdapterView.OnItemCl
* To save time measuring the height, this method gets an item if the height has not been
* previous measured and multiplies it by count of the total amount of items. It is fine if the
* height too small as the ListView will scroll through the other values.
+ *
* @param listView The ListView to measure the surrounding padding.
* @param listAdapter The adapter which contains the items within the list.
* @return Returns the combined height of the padding of the ListView and the approximate height
@@ -263,8 +325,64 @@ public class TabularContextMenuUi implements ContextMenuUi, AdapterView.OnItemCl
}
/**
- * When an thumbnail is retrieved for the header of an image, this will set the header to
- * that particular bitmap.
+ * @param touchPointX The x-coordinate of the touch that triggered the context menu in pixels.
+ * @param touchPointY The y-coordinate of the touch that triggered the context menu in pixels.
+ * @param activity Activity to determine the number of pixels to the left and above of the
+ * window.
Theresa 2017/05/24 17:53:33 nit: indent this to align with "Activity to..."
Daniel Park 2017/05/24 19:44:39 Done.
+ */
+ private void initializeAnimationAndRevealView(
+ final float touchPointX, final float touchPointY, Activity activity) {
+ mPagerView.setVisibility(View.VISIBLE);
+
+ int[] locationOnScreen = new int[2];
+ mPagerView.getLocationOnScreen(locationOnScreen);
+
+ mContextMenuYLocationOnScreen = locationOnScreen[1];
+ mPagerView.startAnimation(initAnimations(
+ touchPointX, touchPointY, locationOnScreen[0], locationOnScreen[1], activity));
+ }
+
+ /**
+ * @param touchPointX The starting x-coordinate of the translation animation.
+ * @param touchPointY The starting y-coordinate of the translation animation.
+ * @param viewLocationX The x-coordinate of the top left corner of the view relative to the
+ * device.
+ * @param viewLocationY The y-coordinate of the top left corner of the view relative to the
+ * device.
+ * @param activity Activity to determine the number of pixels to the left and above of the
+ * window
+ * @return Returns the animation set containing the scale & translation animations with the
+ * pivots set relative to the view.
+ */
+ private Animation initAnimations(final float touchPointX, final float touchPointY,
+ int viewLocationX, int viewLocationY, Activity activity) {
+ Rect rectangle = new Rect();
+ Window window = activity.getWindow();
+ window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
+
+ // yOffset represents how many pixels are taken up by anything above the content window
+ // e.g. toolbar & tab strip.
+ float yOffset = rectangle.top + mRenderCoordinates.getContentOffsetYPix();
+
+ // xOffset represents how many pixels are taken up by anything to the left of the content
+ // window e.g. navigation bar of the Nexus 6P when in landscape mode with usb port to the
+ // left.
+ float xOffset = rectangle.left;
+
+ /**
+ * Touch points & view locations are relative to the content window i.e. 0, 0 represents
+ * the top left corner of the content window, but animation requires coordinates relative
+ * to the context menu. These operations are to transform the content window coordinates
+ * to what they would be if they were relative to the the context menu.
+ */
+ mContextMenuSourceX = touchPointX - viewLocationX + xOffset;
+ mContextMenuSourceY = touchPointY - viewLocationY + yOffset;
+ return getScaleAnimationIn();
+ }
+
+ /**
+ * When an thumbnail is retrieved for the header of an image, this will set the header to that
+ * particular bitmap.
*/
public void onImageThumbnailRetrieved(Bitmap bitmap) {
if (mHeaderImageView != null) {
@@ -277,4 +395,21 @@ public class TabularContextMenuUi implements ContextMenuUi, AdapterView.OnItemCl
mDialog.dismiss();
mCallback.onResult((int) id);
}
+
+ /**
+ * Gives this class access to the content view core to allow access to the total size of the
+ * toolbar and tab strip.
+ */
+ public void setRenderCoordinates(RenderCoordinates renderCoordinates) {
+ mRenderCoordinates = renderCoordinates;
+ }
+
+ private Animation getScaleAnimationIn() {
Theresa 2017/05/24 17:53:33 Ideally ContextMenuDialog would own both animation
Daniel Park 2017/05/24 19:44:39 Done.
+ ScaleAnimation scaleAnimation = new ScaleAnimation(0f, 1f, 0f, 1f, Animation.ABSOLUTE,
+ mContextMenuSourceX, Animation.ABSOLUTE, mContextMenuSourceY);
+
+ scaleAnimation.setDuration(ANIMATION_IN_DURATION);
+ scaleAnimation.setInterpolator(new LinearOutSlowInInterpolator());
+ return scaleAnimation;
+ }
}

Powered by Google App Engine
This is Rietveld 408576698