From ba26e8f2b808b67369d424ec279c440579b94708 Mon Sep 17 00:00:00 2001 From: Saklhc Xu Date: Wed, 11 Dec 2013 14:03:40 +0800 Subject: [PATCH] Supports custom SecondaryMenu's width and Ensure that the same two menu sliding speed --- .../slidingmenu/lib/CustomViewAbove.java | 22 ++++++-- .../slidingmenu/lib/CustomViewBehind.java | 37 +++++++++---- .../slidingmenu/lib/SlidingMenu.java | 54 +++++++++++++++++++ 3 files changed, 98 insertions(+), 15 deletions(-) diff --git a/library/src/com/jeremyfeinstein/slidingmenu/lib/CustomViewAbove.java b/library/src/com/jeremyfeinstein/slidingmenu/lib/CustomViewAbove.java index fa73ef05b..8da1ff9c3 100644 --- a/library/src/com/jeremyfeinstein/slidingmenu/lib/CustomViewAbove.java +++ b/library/src/com/jeremyfeinstein/slidingmenu/lib/CustomViewAbove.java @@ -418,10 +418,24 @@ void smoothScrollTo(int x, int y, int velocity) { velocity = Math.abs(velocity); if (velocity > 0) { duration = 4 * Math.round(1000 * Math.abs(distance / velocity)); - } else { - final float pageDelta = (float) Math.abs(dx) / width; - duration = (int) ((pageDelta + 1) * 100); - duration = MAX_SETTLE_DURATION; + + } else {//Ensure that the same two menu sliding speed + switch (mViewBehind.getMode()) { + case SlidingMenu.LEFT: + case SlidingMenu.RIGHT: + duration = MAX_SETTLE_DURATION; + break; + case SlidingMenu.LEFT_RIGHT: + if (mViewBehind.getBehindWidth() > mViewBehind.getSecondaryBehindWidth()) { + duration = MAX_SETTLE_DURATION * Math.abs(dx) / mViewBehind.getBehindWidth(); + } else { + duration = MAX_SETTLE_DURATION * Math.abs(dx) / mViewBehind.getSecondaryBehindWidth(); + } + break; + default: + duration = MAX_SETTLE_DURATION; + break; + } } duration = Math.min(duration, MAX_SETTLE_DURATION); diff --git a/library/src/com/jeremyfeinstein/slidingmenu/lib/CustomViewBehind.java b/library/src/com/jeremyfeinstein/slidingmenu/lib/CustomViewBehind.java index fa0544d5b..35bbf8c95 100644 --- a/library/src/com/jeremyfeinstein/slidingmenu/lib/CustomViewBehind.java +++ b/library/src/com/jeremyfeinstein/slidingmenu/lib/CustomViewBehind.java @@ -28,6 +28,7 @@ public class CustomViewBehind extends ViewGroup { private View mSecondaryContent; private int mMarginThreshold; private int mWidthOffset; + private int mSecondaryWidthOffset; private CanvasTransformer mTransformer; private boolean mChildrenEnabled; @@ -53,6 +54,11 @@ public void setWidthOffset(int i) { mWidthOffset = i; requestLayout(); } + + public void setSecondaryWidthOffset(int offset) { + mSecondaryWidthOffset = offset; + requestLayout(); + } public void setMarginThreshold(int marginThreshold) { mMarginThreshold = marginThreshold; @@ -66,6 +72,10 @@ public int getBehindWidth() { return mContent.getWidth(); } + public int getSecondaryBehindWidth() { + return mSecondaryContent==null ? 0 : mSecondaryContent.getWidth(); + } + public void setContent(View v) { if (mContent != null) removeView(mContent); @@ -130,7 +140,7 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) { final int height = b - t; mContent.layout(0, 0, width-mWidthOffset, height); if (mSecondaryContent != null) - mSecondaryContent.layout(0, 0, width-mWidthOffset, height); + mSecondaryContent.layout(0, 0, width-mSecondaryWidthOffset, height); } @Override @@ -141,8 +151,11 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { final int contentWidth = getChildMeasureSpec(widthMeasureSpec, 0, width-mWidthOffset); final int contentHeight = getChildMeasureSpec(heightMeasureSpec, 0, height); mContent.measure(contentWidth, contentHeight); - if (mSecondaryContent != null) - mSecondaryContent.measure(contentWidth, contentHeight); + if (mSecondaryContent != null){ + final int contentSecondaryWidth = getChildMeasureSpec( + widthMeasureSpec, 0, width - mSecondaryWidthOffset); + mSecondaryContent.measure(contentSecondaryWidth, contentHeight); + } } private int mMode; @@ -228,8 +241,8 @@ public void scrollBehindTo(View content, int x, int y) { if (x <= content.getLeft()) { scrollTo((int)((x + getBehindWidth())*mScrollScale), y); } else { - scrollTo((int)(getBehindWidth() - getWidth() + - (x-getBehindWidth())*mScrollScale), y); + scrollTo((int)(getSecondaryBehindWidth() - getWidth() + + (x-getSecondaryBehindWidth())*mScrollScale), y); } } if (vis == View.INVISIBLE) @@ -257,7 +270,7 @@ public int getMenuLeft(View content, int page) { case 0: return content.getLeft() - getBehindWidth(); case 2: - return content.getLeft() + getBehindWidth(); + return content.getLeft() + getSecondaryBehindWidth(); } } return content.getLeft(); @@ -275,9 +288,11 @@ public int getAbsLeftBound(View content) { public int getAbsRightBound(View content) { if (mMode == SlidingMenu.LEFT) { return content.getLeft(); - } else if (mMode == SlidingMenu.RIGHT || mMode == SlidingMenu.LEFT_RIGHT) { - return content.getLeft() + getBehindWidth(); - } + } else if (mMode == SlidingMenu.RIGHT) { + return content.getLeft() + getBehindWidth(); + }else if(mMode == SlidingMenu.LEFT_RIGHT){ + return content.getLeft() + getSecondaryBehindWidth(); + } return 0; } @@ -372,11 +387,11 @@ public void drawFade(View content, Canvas canvas, float openPercent) { left = content.getRight(); right = content.getRight() + getBehindWidth(); } else if (mMode == SlidingMenu.LEFT_RIGHT) { - left = content.getLeft() - getBehindWidth(); + left = content.getLeft() - getSecondaryBehindWidth(); right = content.getLeft(); canvas.drawRect(left, 0, right, getHeight(), mFadePaint); left = content.getRight(); - right = content.getRight() + getBehindWidth(); + right = content.getRight() + getSecondaryBehindWidth(); } canvas.drawRect(left, 0, right, getHeight(), mFadePaint); } diff --git a/library/src/com/jeremyfeinstein/slidingmenu/lib/SlidingMenu.java b/library/src/com/jeremyfeinstein/slidingmenu/lib/SlidingMenu.java index 8b88708b9..bfdf28ce2 100644 --- a/library/src/com/jeremyfeinstein/slidingmenu/lib/SlidingMenu.java +++ b/library/src/com/jeremyfeinstein/slidingmenu/lib/SlidingMenu.java @@ -588,6 +588,15 @@ public void setBehindOffset(int i) { mViewBehind.setWidthOffset(i); } + /** + * Sets the behind secondary offset. + * + * @param offset The margin, in pixels, on the right of the screen that the behind secondary view scrolls to. + */ + public void setBehindSecondaryOffset(int offset) { + mViewBehind.setSecondaryWidthOffset(offset); + } + /** * Sets the behind offset. * @@ -599,6 +608,17 @@ public void setBehindOffsetRes(int resID) { setBehindOffset(i); } + /** + * Sets the behind secondary offset. + * + * @param resID The dimension resource id to be set as the behind secondary offset. + * The secondary menu, when open, will leave this width margin on the right of the screen. + */ + public void setBehindSecondaryOffsetRes(int resID) { + int i = (int) getContext().getResources().getDimension(resID); + setBehindSecondaryOffset(i); + } + /** * Sets the above offset. * @@ -641,6 +661,29 @@ public void setBehindWidth(int i) { setBehindOffset(width-i); } + /** + * Sets the behind secondary width. + * + * @param i The width the Secondary Sliding Menu will open to, in pixels + */ + @SuppressWarnings("deprecation") + public void setBehindSecondaryWidth(int i) { + int width; + Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)) + .getDefaultDisplay(); + try { + Class cls = Display.class; + Class[] parameterTypes = {Point.class}; + Point parameter = new Point(); + Method method = cls.getMethod("getSize", parameterTypes); + method.invoke(display, parameter); + width = parameter.x; + } catch (Exception e) { + width = display.getWidth(); + } + setBehindSecondaryOffset(width - i); + } + /** * Sets the behind width. * @@ -652,6 +695,17 @@ public void setBehindWidthRes(int res) { setBehindWidth(i); } + /** + * Sets the behind secondary width. + * + * @param res The dimension resource id to be set as the behind secondary width offset. + * The secondary menu, when open, will open this wide. + */ + public void setBehindSecondaryWidthRes(int res) { + int i = (int) getContext().getResources().getDimension(res); + setBehindSecondaryWidth(i); + } + /** * Gets the behind scroll scale. *