private boolean shouldCloseDrawers(float x, float y) { View activitedDrawer = null; if (mLeftPercent > 0.0f) { activitedDrawer = mLeftDrawer; } else if (mRightPercent > 0.0f) { activitedDrawer = mRightDrawer; } if (activitedDrawer == null) { return false; } int xInt = (int) x; int yInt = (int) y; if (activitedDrawer instanceof DrawerLayoutChild) { DrawerLayoutChild dlc = (DrawerLayoutChild) activitedDrawer; int paddingTop = dlc.getLayoutPaddingTop(); int paddingBottom = dlc.getLayoutPaddingBottom(); if (yInt < paddingTop || yInt >= getHeight() - paddingBottom) { return false; } } return !ViewUtils.isViewUnder(activitedDrawer, xInt, yInt, 0); }
@Override protected void onDraw(Canvas c) { View activitedDrawer = null; if (mLeftDrawer != null && mLeftDrawer.getRight() > 0) { activitedDrawer = mLeftDrawer; } else if (mRightDrawer != null && mRightDrawer.getLeft() < SlidingDrawerLayout.this.getWidth()) { activitedDrawer = mRightDrawer; } if (activitedDrawer == null) { return; } int paddingTop = 0; int paddingBottom = 0; if (activitedDrawer instanceof DrawerLayoutChild) { DrawerLayoutChild dlc = (DrawerLayoutChild) activitedDrawer; paddingTop = dlc.getLayoutPaddingTop(); paddingBottom = dlc.getLayoutPaddingBottom(); } int width = getWidth(); int height = getHeight(); int saved = -1; if (paddingTop != 0 || paddingBottom != 0) { saved = c.save(); c.clipRect(0, paddingTop, width, height - paddingBottom); } // Draw drak background c.drawARGB(MathUtils.lerp(FORM, TO, mPercent), 0, 0, 0); if (activitedDrawer == mLeftDrawer) { if (mShadowLeft != null) { int right = mLeftDrawer.getRight(); final int shadowWidth = mShadowLeft.getIntrinsicWidth(); mShadowLeft.setBounds(right, 0, right + shadowWidth, getHeight()); mShadowLeft.setAlpha((int) (0xff * mLeftPercent)); mShadowLeft.draw(c); } } else if (activitedDrawer == mRightDrawer) { if (mShadowRight != null) { int left = mRightDrawer.getLeft(); final int shadowWidth = mShadowRight.getIntrinsicWidth(); mShadowRight.setBounds(left - shadowWidth, 0, left, getHeight()); mShadowRight.setAlpha((int) (0xff * mRightPercent)); mShadowRight.draw(c); } } if (saved != -1) { c.restoreToCount(saved); } }
@SuppressWarnings("deprecation") @Override protected boolean fitSystemWindows(Rect insets) { mFitPaddingTop = insets.top; mFitPaddingBottom = insets.bottom; insets.top = 0; insets.bottom = 0; for (int i = 0, n = getChildCount(); i < n; i++) { View view = getChildAt(i); if (view instanceof DrawerLayoutChild) { ((DrawerLayoutChild) view).setFitPadding(mFitPaddingTop, mFitPaddingBottom); } } return super.fitSystemWindows(insets); }
@Override protected void onLayout(boolean changed, int l, int t, int r, int b) { mInLayout = true; final int width = r - l; final int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { final View child = getChildAt(i); if (child.getVisibility() == GONE) { continue; } final LayoutParams lp = (LayoutParams) child.getLayoutParams(); int paddingTop = 0; int paddingBottom = mFitPaddingBottom; if (child instanceof DrawerLayoutChild) { DrawerLayoutChild dlc = (DrawerLayoutChild) child; paddingTop = dlc.getLayoutPaddingTop(); paddingBottom = dlc.getLayoutPaddingBottom() + mFitPaddingBottom; } if (child == mContentView) { child.layout( lp.leftMargin, lp.topMargin + paddingTop, lp.leftMargin + child.getMeasuredWidth(), lp.topMargin + paddingTop + child.getMeasuredHeight()); } else if (child == mShadow) { child.layout(0, 0, child.getMeasuredWidth(), child.getMeasuredHeight()); } else { // Drawer, if it wasn't onMeasure would have thrown an exception. final int childWidth = child.getMeasuredWidth(); final int childHeight = child.getMeasuredHeight(); int childLeft; float percent; if (child == mLeftDrawer) { percent = mLeftPercent; childLeft = -childWidth + (int) (childWidth * percent); } else { // Right; onMeasure checked for us. percent = mRightPercent; childLeft = width - (int) (childWidth * percent); } final int vgrav = lp.gravity & Gravity.VERTICAL_GRAVITY_MASK; switch (vgrav) { default: case Gravity.TOP: { child.layout( childLeft, lp.topMargin + paddingTop, childLeft + childWidth, lp.topMargin + paddingTop + childHeight); break; } case Gravity.BOTTOM: { final int height = b - t; child.layout( childLeft, height - lp.bottomMargin - paddingBottom - child.getMeasuredHeight(), childLeft + childWidth, height - lp.bottomMargin - paddingBottom); break; } case Gravity.CENTER_VERTICAL: { final int height = b - t; int childTop = (height - childHeight - paddingTop - paddingBottom - lp.topMargin - lp.bottomMargin) / 2 + paddingTop; // Offset for margins. If things don't fit right because of // bad measurement before, oh well. if (childTop < lp.topMargin + paddingTop) { childTop = lp.topMargin + paddingTop; } else if (childTop + childHeight > height - paddingBottom - lp.bottomMargin) { childTop = height - paddingBottom - lp.bottomMargin - childHeight; } child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight); break; } } final int newVisibility = percent > 0 ? VISIBLE : INVISIBLE; if (child.getVisibility() != newVisibility) { child.setVisibility(newVisibility); } } } mInLayout = false; }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthMode = MeasureSpec.getMode(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); if (widthMode != MeasureSpec.EXACTLY || heightMode != MeasureSpec.EXACTLY) throw new IllegalArgumentException( "SlidingDrawerLayout must be measured with MeasureSpec.EXACTLY."); setMeasuredDimension(widthSize, heightSize); // Gravity value for each drawer we've seen. Only one of each permitted. final int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { final View child = getChildAt(i); if (child.getVisibility() == GONE) { continue; } final LayoutParams lp = (LayoutParams) child.getLayoutParams(); int paddingTop = 0; int paddingBottom = mFitPaddingBottom; if (child instanceof DrawerLayoutChild) { DrawerLayoutChild dlc = (DrawerLayoutChild) child; paddingTop = dlc.getLayoutPaddingTop(); paddingBottom = dlc.getLayoutPaddingBottom() + mFitPaddingBottom; } if (child == mContentView) { // Content views get measured at exactly the layout's size. final int contentWidthSpec = MeasureSpec.makeMeasureSpec( widthSize - lp.leftMargin - lp.rightMargin, MeasureSpec.EXACTLY); final int contentHeightSpec = MeasureSpec.makeMeasureSpec( heightSize - lp.topMargin - lp.bottomMargin - paddingTop - paddingBottom, MeasureSpec.EXACTLY); child.measure(contentWidthSpec, contentHeightSpec); } else if (child == mLeftDrawer || child == mRightDrawer) { final int drawerWidthSpec = getChildMeasureSpec( widthMeasureSpec, lp.leftMargin + lp.rightMargin, Math.min(widthSize - mMinDrawerMargin, lp.width)); final int drawerHeightSpec = getChildMeasureSpec( heightMeasureSpec, lp.topMargin + lp.bottomMargin + paddingTop + paddingBottom, lp.height); child.measure(drawerWidthSpec, drawerHeightSpec); } else if (child == mShadow) { child.measure( MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.EXACTLY)); } else { throw new IllegalStateException("Don't call addView"); } } }