Example usage for android.view Gravity VERTICAL_GRAVITY_MASK

List of usage examples for android.view Gravity VERTICAL_GRAVITY_MASK

Introduction

In this page you can find the example usage for android.view Gravity VERTICAL_GRAVITY_MASK.

Prototype

int VERTICAL_GRAVITY_MASK

To view the source code for android.view Gravity VERTICAL_GRAVITY_MASK.

Click Source Link

Document

Binary mask to get the vertical gravity of a gravity.

Usage

From source file:android.support.v7ox.widget.Toolbar.java

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    final boolean isRtl = ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL;
    final int width = getWidth();
    final int height = getHeight();
    final int paddingLeft = getPaddingLeft();
    final int paddingRight = getPaddingRight();
    final int paddingTop = getPaddingTop();
    final int paddingBottom = getPaddingBottom();
    int left = paddingLeft;
    int right = width - paddingRight;

    final int[] collapsingMargins = mTempMargins;
    collapsingMargins[0] = collapsingMargins[1] = 0;

    // Align views within the minimum toolbar height, if set.
    final int alignmentHeight = ViewCompat.getMinimumHeight(this);

    if (shouldLayout(mNavButtonView)) {
        if (isRtl) {
            right = layoutChildRight(mNavButtonView, right, collapsingMargins, alignmentHeight);
        } else {/* w  ww. j a v a 2s. com*/
            left = layoutChildLeft(mNavButtonView, left, collapsingMargins, alignmentHeight);
        }
    }

    if (shouldLayout(mCollapseButtonView)) {
        if (isRtl) {
            right = layoutChildRight(mCollapseButtonView, right, collapsingMargins, alignmentHeight);
        } else {
            left = layoutChildLeft(mCollapseButtonView, left, collapsingMargins, alignmentHeight);
        }
    }

    if (shouldLayout(mMenuView)) {
        if (isRtl) {
            left = layoutChildLeft(mMenuView, left, collapsingMargins, alignmentHeight);
        } else {
            right = layoutChildRight(mMenuView, right, collapsingMargins, alignmentHeight);
        }
    }

    collapsingMargins[0] = Math.max(0, getContentInsetLeft() - left);
    collapsingMargins[1] = Math.max(0, getContentInsetRight() - (width - paddingRight - right));
    left = Math.max(left, getContentInsetLeft());
    right = Math.min(right, width - paddingRight - getContentInsetRight());

    if (shouldLayout(mExpandedActionView)) {
        if (isRtl) {
            right = layoutChildRight(mExpandedActionView, right, collapsingMargins, alignmentHeight);
        } else {
            left = layoutChildLeft(mExpandedActionView, left, collapsingMargins, alignmentHeight);
        }
    }

    if (shouldLayout(mLogoView)) {
        if (isRtl) {
            right = layoutChildRight(mLogoView, right, collapsingMargins, alignmentHeight);
        } else {
            left = layoutChildLeft(mLogoView, left, collapsingMargins, alignmentHeight);
        }
    }

    final boolean layoutTitle = shouldLayout(mTitleTextView);
    final boolean layoutSubtitle = shouldLayout(mSubtitleTextView);
    int titleHeight = 0;
    if (layoutTitle) {
        final LayoutParams lp = (LayoutParams) mTitleTextView.getLayoutParams();
        titleHeight += lp.topMargin + mTitleTextView.getMeasuredHeight() + lp.bottomMargin;
    }
    if (layoutSubtitle) {
        final LayoutParams lp = (LayoutParams) mSubtitleTextView.getLayoutParams();
        titleHeight += lp.topMargin + mSubtitleTextView.getMeasuredHeight() + lp.bottomMargin;
    }

    if (layoutTitle || layoutSubtitle) {
        int titleTop;
        final View topChild = layoutTitle ? mTitleTextView : mSubtitleTextView;
        final View bottomChild = layoutSubtitle ? mSubtitleTextView : mTitleTextView;
        final LayoutParams toplp = (LayoutParams) topChild.getLayoutParams();
        final LayoutParams bottomlp = (LayoutParams) bottomChild.getLayoutParams();
        final boolean titleHasWidth = layoutTitle && mTitleTextView.getMeasuredWidth() > 0
                || layoutSubtitle && mSubtitleTextView.getMeasuredWidth() > 0;

        switch (mGravity & Gravity.VERTICAL_GRAVITY_MASK) {
        case Gravity.TOP:
            titleTop = getPaddingTop() + toplp.topMargin + mTitleMarginTop;
            break;
        default:
        case Gravity.CENTER_VERTICAL:
            final int space = height - paddingTop - paddingBottom;
            int spaceAbove = (space - titleHeight) / 2;
            if (spaceAbove < toplp.topMargin + mTitleMarginTop) {
                spaceAbove = toplp.topMargin + mTitleMarginTop;
            } else {
                final int spaceBelow = height - paddingBottom - titleHeight - spaceAbove - paddingTop;
                if (spaceBelow < toplp.bottomMargin + mTitleMarginBottom) {
                    spaceAbove = Math.max(0,
                            spaceAbove - (bottomlp.bottomMargin + mTitleMarginBottom - spaceBelow));
                }
            }
            titleTop = paddingTop + spaceAbove;
            break;
        case Gravity.BOTTOM:
            titleTop = height - paddingBottom - bottomlp.bottomMargin - mTitleMarginBottom - titleHeight;
            break;
        }
        if (isRtl) {
            final int rd = (titleHasWidth ? mTitleMarginStart : 0) - collapsingMargins[1];
            right -= Math.max(0, rd);
            collapsingMargins[1] = Math.max(0, -rd);
            int titleRight = right;
            int subtitleRight = right;

            if (layoutTitle) {
                final LayoutParams lp = (LayoutParams) mTitleTextView.getLayoutParams();
                final int titleLeft = titleRight - mTitleTextView.getMeasuredWidth();
                final int titleBottom = titleTop + mTitleTextView.getMeasuredHeight();
                mTitleTextView.layout(titleLeft, titleTop, titleRight, titleBottom);
                titleRight = titleLeft - mTitleMarginEnd;
                titleTop = titleBottom + lp.bottomMargin;
            }
            if (layoutSubtitle) {
                final LayoutParams lp = (LayoutParams) mSubtitleTextView.getLayoutParams();
                titleTop += lp.topMargin;
                final int subtitleLeft = subtitleRight - mSubtitleTextView.getMeasuredWidth();
                final int subtitleBottom = titleTop + mSubtitleTextView.getMeasuredHeight();
                mSubtitleTextView.layout(subtitleLeft, titleTop, subtitleRight, subtitleBottom);
                subtitleRight = subtitleRight - mTitleMarginEnd;
                titleTop = subtitleBottom + lp.bottomMargin;
            }
            if (titleHasWidth) {
                right = Math.min(titleRight, subtitleRight);
            }
        } else {
            final int ld = (titleHasWidth ? mTitleMarginStart : 0) - collapsingMargins[0];
            left += Math.max(0, ld);
            collapsingMargins[0] = Math.max(0, -ld);
            int titleLeft = left;
            int subtitleLeft = left;

            if (layoutTitle) {
                final LayoutParams lp = (LayoutParams) mTitleTextView.getLayoutParams();
                final int titleRight = titleLeft + mTitleTextView.getMeasuredWidth();
                final int titleBottom = titleTop + mTitleTextView.getMeasuredHeight();
                mTitleTextView.layout(titleLeft, titleTop, titleRight, titleBottom);
                titleLeft = titleRight + mTitleMarginEnd;
                titleTop = titleBottom + lp.bottomMargin;
            }
            if (layoutSubtitle) {
                final LayoutParams lp = (LayoutParams) mSubtitleTextView.getLayoutParams();
                titleTop += lp.topMargin;
                final int subtitleRight = subtitleLeft + mSubtitleTextView.getMeasuredWidth();
                final int subtitleBottom = titleTop + mSubtitleTextView.getMeasuredHeight();
                mSubtitleTextView.layout(subtitleLeft, titleTop, subtitleRight, subtitleBottom);
                subtitleLeft = subtitleRight + mTitleMarginEnd;
                titleTop = subtitleBottom + lp.bottomMargin;
            }
            if (titleHasWidth) {
                left = Math.max(titleLeft, subtitleLeft);
            }
        }
    }

    // Get all remaining children sorted for layout. This is all prepared
    // such that absolute layout direction can be used below.

    addCustomViewsWithGravity(mTempViews, Gravity.LEFT);
    final int leftViewsCount = mTempViews.size();
    for (int i = 0; i < leftViewsCount; i++) {
        left = layoutChildLeft(mTempViews.get(i), left, collapsingMargins, alignmentHeight);
    }

    addCustomViewsWithGravity(mTempViews, Gravity.RIGHT);
    final int rightViewsCount = mTempViews.size();
    for (int i = 0; i < rightViewsCount; i++) {
        right = layoutChildRight(mTempViews.get(i), right, collapsingMargins, alignmentHeight);
    }

    // Centered views try to center with respect to the whole bar, but views pinned
    // to the left or right can push the mass of centered views to one side or the other.
    addCustomViewsWithGravity(mTempViews, Gravity.CENTER_HORIZONTAL);
    final int centerViewsWidth = getViewListMeasuredWidth(mTempViews, collapsingMargins);
    final int parentCenter = paddingLeft + (width - paddingLeft - paddingRight) / 2;
    final int halfCenterViewsWidth = centerViewsWidth / 2;
    int centerLeft = parentCenter - halfCenterViewsWidth;
    final int centerRight = centerLeft + centerViewsWidth;
    if (centerLeft < left) {
        centerLeft = left;
    } else if (centerRight > right) {
        centerLeft -= centerRight - right;
    }

    final int centerViewsCount = mTempViews.size();
    for (int i = 0; i < centerViewsCount; i++) {
        centerLeft = layoutChildLeft(mTempViews.get(i), centerLeft, collapsingMargins, alignmentHeight);
    }

    mTempViews.clear();
}

From source file:cn.androidy.materialdesignsample.ryanharterviewpager.ViewPager.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // For simple implementation, our internal size is always 0.
    // We depend on the container to specify the layout size of
    // our view.  We can't really know what it is since we will be
    // adding and removing different arbitrary views and do not
    // want the layout to change as this happens.
    setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));

    final int measuredWidth = getMeasuredWidth();
    final int measuredHeight = getMeasuredHeight();
    final int maxGutterSize = (isOrientationHorizontal() ? measuredWidth : measuredHeight) / 10;
    mGutterSize = Math.min(maxGutterSize, mDefaultGutterSize);

    // Children are just made to fill our space.
    int childWidthSize = measuredWidth - getPaddingLeft() - getPaddingRight();
    int childHeightSize = measuredHeight - getPaddingTop() - getPaddingBottom();

    /*//from   w  w  w.ja  va2s.  com
     * Make sure all children have been properly measured. Decor views first.
     * Right now we cheat and make this less complicated by assuming decor
     * views won't intersect. We will pin to edges based on gravity.
     */
    int size = getChildCount();
    for (int i = 0; i < size; ++i) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (lp != null && lp.isDecor) {
                final int hgrav = lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
                final int vgrav = lp.gravity & Gravity.VERTICAL_GRAVITY_MASK;
                int widthMode = MeasureSpec.AT_MOST;
                int heightMode = MeasureSpec.AT_MOST;
                boolean consumeVertical = vgrav == Gravity.TOP || vgrav == Gravity.BOTTOM;
                boolean consumeHorizontal = hgrav == Gravity.LEFT || hgrav == Gravity.RIGHT;

                if (consumeVertical) {
                    widthMode = MeasureSpec.EXACTLY;
                } else if (consumeHorizontal) {
                    heightMode = MeasureSpec.EXACTLY;
                }

                int widthSize = childWidthSize;
                int heightSize = childHeightSize;
                if (lp.width != LayoutParams.WRAP_CONTENT) {
                    widthMode = MeasureSpec.EXACTLY;
                    if (lp.width != LayoutParams.MATCH_PARENT) {
                        widthSize = lp.width;
                    }
                }
                if (lp.height != LayoutParams.WRAP_CONTENT) {
                    heightMode = MeasureSpec.EXACTLY;
                    if (lp.height != LayoutParams.MATCH_PARENT) {
                        heightSize = lp.height;
                    }
                }
                final int widthSpec = MeasureSpec.makeMeasureSpec(widthSize, widthMode);
                final int heightSpec = MeasureSpec.makeMeasureSpec(heightSize, heightMode);
                child.measure(widthSpec, heightSpec);

                if (consumeVertical) {
                    childHeightSize -= child.getMeasuredHeight();
                } else if (consumeHorizontal) {
                    childWidthSize -= child.getMeasuredWidth();
                }
            }
        }
    }

    mChildWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY);
    mChildHeightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeightSize, MeasureSpec.EXACTLY);

    // Make sure we have created all fragments that we need to have shown.
    mInLayout = true;
    populate();
    mInLayout = false;

    // Page views next.
    size = getChildCount();
    for (int i = 0; i < size; ++i) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            if (DEBUG)
                Log.v(TAG, "Measuring #" + i + " " + child + ": " + mChildWidthMeasureSpec);

            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (lp == null || !lp.isDecor) {
                if (isOrientationHorizontal()) {
                    final int widthSpec = MeasureSpec.makeMeasureSpec((int) (childWidthSize * lp.sizeFactor),
                            MeasureSpec.EXACTLY);
                    child.measure(widthSpec, mChildHeightMeasureSpec);
                } else {
                    final int heightSpec = MeasureSpec.makeMeasureSpec((int) (childHeightSize * lp.sizeFactor),
                            MeasureSpec.EXACTLY);
                    child.measure(mChildWidthMeasureSpec, heightSpec);
                }
            }
        }
    }
}

From source file:ir.ac.ut.widget.ViewPager.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // For simple implementation, our internal size is always 0.
    // We depend on the container to specify the layout size of
    // our view.  We can't really know what it is since we will be
    // adding and removing different arbitrary views and do not
    // want the layout to change as this happens.
    setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));

    final int measuredWidth = getMeasuredWidth();
    final int maxGutterSize = measuredWidth / 10;
    mGutterSize = Math.min(maxGutterSize, mDefaultGutterSize);

    // Children are just made to fill our space.
    int childWidthSize = measuredWidth - getPaddingLeft() - getPaddingRight();
    int childHeightSize = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();

    /*//from  www  . j  av  a2 s . c om
     * Make sure all children have been properly measured. Decor views first.
     * Right now we cheat and make this less complicated by assuming decor
     * views won't intersect. We will pin to edges based on gravity.
     */
    int size = getChildCount();
    for (int i = 0; i < size; ++i) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (lp != null && lp.isDecor) {
                final int hgrav = lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
                final int vgrav = lp.gravity & Gravity.VERTICAL_GRAVITY_MASK;
                int widthMode = MeasureSpec.AT_MOST;
                int heightMode = MeasureSpec.AT_MOST;
                boolean consumeVertical = vgrav == Gravity.TOP || vgrav == Gravity.BOTTOM;
                boolean consumeHorizontal = hgrav == Gravity.LEFT || hgrav == Gravity.RIGHT;

                if (consumeVertical) {
                    widthMode = MeasureSpec.EXACTLY;
                } else if (consumeHorizontal) {
                    heightMode = MeasureSpec.EXACTLY;
                }

                int widthSize = childWidthSize;
                int heightSize = childHeightSize;
                if (lp.width != LayoutParams.WRAP_CONTENT) {
                    widthMode = MeasureSpec.EXACTLY;
                    if (lp.width != LayoutParams.FILL_PARENT) {
                        widthSize = lp.width;
                    }
                }
                if (lp.height != LayoutParams.WRAP_CONTENT) {
                    heightMode = MeasureSpec.EXACTLY;
                    if (lp.height != LayoutParams.FILL_PARENT) {
                        heightSize = lp.height;
                    }
                }
                final int widthSpec = MeasureSpec.makeMeasureSpec(widthSize, widthMode);
                final int heightSpec = MeasureSpec.makeMeasureSpec(heightSize, heightMode);
                child.measure(widthSpec, heightSpec);

                if (consumeVertical) {
                    childHeightSize -= child.getMeasuredHeight();
                } else if (consumeHorizontal) {
                    childWidthSize -= child.getMeasuredWidth();
                }
            }
        }
    }

    mChildWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY);
    mChildHeightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeightSize, MeasureSpec.EXACTLY);

    // Make sure we have created all fragments that we need to have shown.
    mInLayout = true;
    populate();
    mInLayout = false;

    // Page views next.
    size = getChildCount();
    for (int i = 0; i < size; ++i) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            if (DEBUG) {
                Log.v(TAG, "Measuring #" + i + " " + child + ": " + mChildWidthMeasureSpec);
            }

            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (lp == null || !lp.isDecor) {
                final int widthSpec = MeasureSpec.makeMeasureSpec((int) (childWidthSize * lp.widthFactor),
                        MeasureSpec.EXACTLY);
                child.measure(widthSpec, mChildHeightMeasureSpec);
            }
        }
    }
}

From source file:com.dian.diabetes.widget.VerticalViewPager.java

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    final int count = getChildCount();
    int width = r - l;
    int height = b - t;
    int paddingLeft = getPaddingLeft();
    int paddingTop = getPaddingTop();
    int paddingRight = getPaddingRight();
    int paddingBottom = getPaddingBottom();
    final int scrollY = getScrollY();

    int decorCount = 0;

    // First pass - decor views. We need to do this in two passes so that
    // we have the proper offsets for non-decor views later.
    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            int childLeft = 0;
            int childTop = 0;
            if (lp.isDecor) {
                final int hgrav = lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
                final int vgrav = lp.gravity & Gravity.VERTICAL_GRAVITY_MASK;
                switch (hgrav) {
                default:
                    childLeft = paddingLeft;
                    break;
                case Gravity.LEFT:
                    childLeft = paddingLeft;
                    paddingLeft += child.getMeasuredWidth();
                    break;
                case Gravity.CENTER_HORIZONTAL:
                    childLeft = Math.max((width - child.getMeasuredWidth()) / 2, paddingLeft);
                    break;
                case Gravity.RIGHT:
                    childLeft = width - paddingRight - child.getMeasuredWidth();
                    paddingRight += child.getMeasuredWidth();
                    break;
                }/*from   ww w.j  a  v a2 s .  c o m*/
                switch (vgrav) {
                default:
                    childTop = paddingTop;
                    break;
                case Gravity.TOP:
                    childTop = paddingTop;
                    paddingTop += child.getMeasuredHeight();
                    break;
                case Gravity.CENTER_VERTICAL:
                    childTop = Math.max((height - child.getMeasuredHeight()) / 2, paddingTop);
                    break;
                case Gravity.BOTTOM:
                    childTop = height - paddingBottom - child.getMeasuredHeight();
                    paddingBottom += child.getMeasuredHeight();
                    break;
                }
                childTop += scrollY;
                child.layout(childLeft, childTop, childLeft + child.getMeasuredWidth(),
                        childTop + child.getMeasuredHeight());
                decorCount++;
            }
        }
    }

    final int childHeight = height - paddingTop - paddingBottom;
    // Page views. Do this once we have the right padding offsets from above.
    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            ItemInfo ii;
            if (!lp.isDecor && (ii = infoForChild(child)) != null) {
                int toff = (int) (childHeight * ii.offset);
                int childLeft = paddingLeft;
                int childTop = paddingTop + toff;
                if (lp.needsMeasure) {
                    // This was added during layout and needs measurement.
                    // Do it now that we know what we're working with.
                    lp.needsMeasure = false;
                    final int widthSpec = MeasureSpec
                            .makeMeasureSpec((int) (width - paddingLeft - paddingRight), MeasureSpec.EXACTLY);
                    final int heightSpec = MeasureSpec.makeMeasureSpec((int) (childHeight * lp.heightFactor),
                            MeasureSpec.EXACTLY);
                    child.measure(widthSpec, heightSpec);
                }
                if (DEBUG)
                    Log.v(TAG, "Positioning #" + i + " " + child + " f=" + ii.object + ":" + childLeft + ","
                            + childTop + " " + child.getMeasuredWidth() + "x" + child.getMeasuredHeight());
                child.layout(childLeft, childTop, childLeft + child.getMeasuredWidth(),
                        childTop + child.getMeasuredHeight());
            }
        }
    }
    mLeftPageBounds = paddingLeft;
    mRightPageBounds = width - paddingRight;
    mDecorChildCount = decorCount;

    if (mFirstLayout) {
        scrollToItem(mCurItem, false, 0, false);
    }
    mFirstLayout = false;
}

From source file:com.viewpagerindicator.MyDirectionalViewPager.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // For simple implementation, our internal size is always 0.
    // We depend on the container to specify the layout size of
    // our view.  We can't really know what it is since we will be
    // adding and removing different arbitrary views and do not
    // want the layout to change as this happens.
    setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));

    final int measuredWidth = getMeasuredWidth();
    final int maxGutterSize = measuredWidth / 10;
    mGutterSize = Math.min(maxGutterSize, mDefaultGutterSize);

    // Children are just made to fill our space.
    int childWidthSize = measuredWidth - getPaddingLeft() - getPaddingRight();
    int childHeightSize = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();

    /*//from  w w w .j ava2  s  .  c o m
     * Make sure all children have been properly measured. Decor views first.
     * Right now we cheat and make this less complicated by assuming decor
     * views won't intersect. We will pin to edges based on gravity.
     */
    int size = getChildCount();
    for (int i = 0; i < size; ++i) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (lp != null && lp.isDecor) {
                final int hgrav = lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
                final int vgrav = lp.gravity & Gravity.VERTICAL_GRAVITY_MASK;
                int widthMode = MeasureSpec.AT_MOST;
                int heightMode = MeasureSpec.AT_MOST;
                boolean consumeVertical = vgrav == Gravity.TOP || vgrav == Gravity.BOTTOM;
                boolean consumeHorizontal = hgrav == Gravity.LEFT || hgrav == Gravity.RIGHT;

                if (consumeVertical) {
                    widthMode = MeasureSpec.EXACTLY;
                } else if (consumeHorizontal) {
                    heightMode = MeasureSpec.EXACTLY;
                }

                int widthSize = childWidthSize;
                int heightSize = childHeightSize;
                if (lp.width != LayoutParams.WRAP_CONTENT) {
                    widthMode = MeasureSpec.EXACTLY;
                    if (lp.width != LayoutParams.MATCH_PARENT) {
                        widthSize = lp.width;
                    }
                }
                if (lp.height != LayoutParams.WRAP_CONTENT) {
                    heightMode = MeasureSpec.EXACTLY;
                    if (lp.height != LayoutParams.MATCH_PARENT) {
                        heightSize = lp.height;
                    }
                }
                final int widthSpec = MeasureSpec.makeMeasureSpec(widthSize, widthMode);
                final int heightSpec = MeasureSpec.makeMeasureSpec(heightSize, heightMode);
                child.measure(widthSpec, heightSpec);

                if (consumeVertical) {
                    childHeightSize -= child.getMeasuredHeight();
                } else if (consumeHorizontal) {
                    childWidthSize -= child.getMeasuredWidth();
                }
            }
        }
    }

    mChildWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY);
    mChildHeightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeightSize, MeasureSpec.EXACTLY);

    // Make sure we have created all fragments that we need to have shown.
    mInLayout = true;
    populate();
    mInLayout = false;

    int width = widthMeasureSpec;
    int temp = 0;
    // Page views next.
    size = getChildCount();
    for (int i = 0; i < size; ++i) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            if (DEBUG)
                Log.v(TAG, "Measuring #" + i + " " + child + ": " + mChildWidthMeasureSpec);

            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (lp == null || !lp.isDecor) {
                final int widthSpec = MeasureSpec.makeMeasureSpec((int) (childWidthSize * lp.widthFactor),
                        MeasureSpec.EXACTLY);
                child.measure(widthSpec, mChildHeightMeasureSpec);
                temp = Math.max(widthSpec, temp);
            }
        }
    }
    //      int specMode = MeasureSpec.getMode(widthMeasureSpec);
    //        if(specMode == MeasureSpec.AT_MOST){
    //           width = Math.min(width, temp);
    //        }
    //        setMeasuredDimension(width,heightMeasureSpec);
}

From source file:android.support.custom.view.VerticalViewPager.java

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    if (DEBUG)/*from   ww w .  jav a  2 s . c  o m*/
        Log.d(TAG, "onLayout");

    mInLayout = true;
    populate();
    mInLayout = false;

    final int count = getChildCount();
    int width = r - l;
    int height = b - t;
    int paddingLeft = getPaddingLeft();
    int paddingTop = getPaddingTop();
    int paddingRight = getPaddingRight();
    int paddingBottom = getPaddingBottom();
    final int scrollY = getScrollY();

    int decorCount = 0;

    // First pass - decor views. We need to do this in two passes so that
    // we have the proper offsets for non-decor views later.
    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            int childLeft = 0;
            int childTop = 0;
            if (lp.isDecor) {
                final int hgrav = lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
                final int vgrav = lp.gravity & Gravity.VERTICAL_GRAVITY_MASK;
                switch (hgrav) {
                default:
                    childLeft = paddingLeft;
                    break;
                case Gravity.LEFT:
                    childLeft = paddingLeft;
                    paddingLeft += child.getMeasuredWidth();
                    break;
                case Gravity.CENTER_HORIZONTAL:
                    childLeft = Math.max((width - child.getMeasuredWidth()) / 2, paddingLeft);
                    break;
                case Gravity.RIGHT:
                    childLeft = width - paddingRight - child.getMeasuredWidth();
                    paddingRight += child.getMeasuredWidth();
                    break;
                }
                switch (vgrav) {
                default:
                    childTop = paddingTop;
                    break;
                case Gravity.TOP:
                    childTop = paddingTop;
                    paddingTop += child.getMeasuredHeight();
                    break;
                case Gravity.CENTER_VERTICAL:
                    childTop = Math.max((height - child.getMeasuredHeight()) / 2, paddingTop);
                    break;
                case Gravity.BOTTOM:
                    childTop = height - paddingBottom - child.getMeasuredHeight();
                    paddingBottom += child.getMeasuredHeight();
                    break;
                }
                childTop += scrollY;
                child.layout(childLeft, childTop, childLeft + child.getMeasuredWidth(),
                        childTop + child.getMeasuredHeight());
                decorCount++;
            }
        }
    }

    // Page views. Do this once we have the right padding offsets from above.
    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            ItemInfo ii;
            if (!lp.isDecor && (ii = infoForChild(child)) != null) {
                int loff = (int) (height * ii.offset);

                int childLeft = paddingLeft;

                int childTop = paddingTop = loff;

                if (lp.needsMeasure) {
                    // This was added during layout and needs measurement.
                    // Do it now that we know what we're working with.
                    lp.needsMeasure = false;
                    final int widthSpec = MeasureSpec.makeMeasureSpec(
                            (int) ((width - paddingLeft - paddingRight) * lp.widthFactor), MeasureSpec.EXACTLY);
                    final int heightSpec = MeasureSpec
                            .makeMeasureSpec((int) (height - paddingTop - paddingBottom), MeasureSpec.EXACTLY);
                    child.measure(widthSpec, heightSpec);
                }
                if (DEBUG)
                    Log.v(TAG, "Positioning #" + i + " " + child + " f=" + ii.object + ":" + childLeft + ","
                            + childTop + " " + child.getMeasuredWidth() + "x" + child.getMeasuredHeight());
                child.layout(childLeft, childTop, childLeft + child.getMeasuredWidth(),
                        childTop + child.getMeasuredHeight());
            }
        }
    }
    mLeftPageBounds = paddingLeft;
    mRightPageBounds = width - paddingRight;
    mDecorChildCount = decorCount;
    mFirstLayout = false;
}

From source file:com.baiiu.autoloopviewpager.loopvp.LoopViewPager.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // For simple implementation, or internal size is always 0.
    // We depend on the container to specify the layout size of
    // our view. We can't really know what it is since we will be
    // adding and removing different arbitrary views and do not
    // want the layout to change as this happens.
    setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));

    final int measuredWidth = getMeasuredWidth();
    final int maxGutterSize = measuredWidth / 10;
    mGutterSize = Math.min(maxGutterSize, mDefaultGutterSize);

    // Children are just made to fill our space.
    int childWidthSize = measuredWidth - getPaddingLeft() - getPaddingRight();
    int childHeightSize = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();

    /*/*ww w . jav a 2 s.  c om*/
     * Make sure all children have been properly measured. Decor views
     * first. Right now we cheat and make this less complicated by assuming
     * decor views won't intersect. We will pin to edges based on gravity.
     */
    int size = getChildCount();
    for (int i = 0; i < size; ++i) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (lp != null && lp.isDecor) {
                final int hgrav = lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
                final int vgrav = lp.gravity & Gravity.VERTICAL_GRAVITY_MASK;
                int widthMode = MeasureSpec.AT_MOST;
                int heightMode = MeasureSpec.AT_MOST;
                boolean consumeVertical = vgrav == Gravity.TOP || vgrav == Gravity.BOTTOM;
                boolean consumeHorizontal = hgrav == Gravity.LEFT || hgrav == Gravity.RIGHT;

                if (consumeVertical) {
                    widthMode = MeasureSpec.EXACTLY;
                } else if (consumeHorizontal) {
                    heightMode = MeasureSpec.EXACTLY;
                }

                int widthSize = childWidthSize;
                int heightSize = childHeightSize;
                if (lp.width != LayoutParams.WRAP_CONTENT) {
                    widthMode = MeasureSpec.EXACTLY;
                    if (lp.width != LayoutParams.FILL_PARENT) {
                        widthSize = lp.width;
                    }
                }
                if (lp.height != LayoutParams.WRAP_CONTENT) {
                    heightMode = MeasureSpec.EXACTLY;
                    if (lp.height != LayoutParams.FILL_PARENT) {
                        heightSize = lp.height;
                    }
                }
                final int widthSpec = MeasureSpec.makeMeasureSpec(widthSize, widthMode);
                final int heightSpec = MeasureSpec.makeMeasureSpec(heightSize, heightMode);
                child.measure(widthSpec, heightSpec);

                if (consumeVertical) {
                    childHeightSize -= child.getMeasuredHeight();
                } else if (consumeHorizontal) {
                    childWidthSize -= child.getMeasuredWidth();
                }
            }
        }
    }

    mChildWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY);
    mChildHeightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeightSize, MeasureSpec.EXACTLY);

    // Make sure we have created all fragments that we need to have shown.
    mInLayout = true;
    populate();
    mInLayout = false;

    // Page views next.
    size = getChildCount();
    for (int i = 0; i < size; ++i) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            if (DEBUG)
                Log.v(TAG, "Measuring #" + i + " " + child + ": " + mChildWidthMeasureSpec);

            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (lp == null || !lp.isDecor) {
                final int widthSpec = MeasureSpec.makeMeasureSpec((int) (childWidthSize * lp.widthFactor),
                        MeasureSpec.EXACTLY);
                child.measure(widthSpec, mChildHeightMeasureSpec);
            }
        }
    }
}

From source file:com.guide.ViewPager.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // For simple implementation, our internal size is always 0.
    // We depend on the container to specify the layout size of
    // our view. We can't really know what it is since we will be
    // adding and removing different arbitrary views and do not
    // want the layout to change as this happens.
    setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));

    final int measuredWidth = getMeasuredWidth();
    final int measuredHeight = getMeasuredHeight();
    final int maxGutterSize = (isOrientationHorizontal() ? measuredWidth : measuredHeight) / 10;
    mGutterSize = Math.min(maxGutterSize, mDefaultGutterSize);

    // Children are just made to fill our space.
    int childWidthSize = measuredWidth - getPaddingLeft() - getPaddingRight();
    int childHeightSize = measuredHeight - getPaddingTop() - getPaddingBottom();

    /*//  ww w.  j  ava  2s.  c  o  m
     * Make sure all children have been properly measured. Decor views
     * first. Right now we cheat and make this less complicated by assuming
     * decor views won't intersect. We will pin to edges based on gravity.
     */
    int size = getChildCount();
    for (int i = 0; i < size; ++i) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (lp != null && lp.isDecor) {
                final int hgrav = lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
                final int vgrav = lp.gravity & Gravity.VERTICAL_GRAVITY_MASK;
                int widthMode = MeasureSpec.AT_MOST;
                int heightMode = MeasureSpec.AT_MOST;
                boolean consumeVertical = vgrav == Gravity.TOP || vgrav == Gravity.BOTTOM;
                boolean consumeHorizontal = hgrav == Gravity.LEFT || hgrav == Gravity.RIGHT;

                if (consumeVertical) {
                    widthMode = MeasureSpec.EXACTLY;
                } else if (consumeHorizontal) {
                    heightMode = MeasureSpec.EXACTLY;
                }

                int widthSize = childWidthSize;
                int heightSize = childHeightSize;
                if (lp.width != LayoutParams.WRAP_CONTENT) {
                    widthMode = MeasureSpec.EXACTLY;
                    if (lp.width != LayoutParams.MATCH_PARENT) {
                        widthSize = lp.width;
                    }
                }
                if (lp.height != LayoutParams.WRAP_CONTENT) {
                    heightMode = MeasureSpec.EXACTLY;
                    if (lp.height != LayoutParams.MATCH_PARENT) {
                        heightSize = lp.height;
                    }
                }
                final int widthSpec = MeasureSpec.makeMeasureSpec(widthSize, widthMode);
                final int heightSpec = MeasureSpec.makeMeasureSpec(heightSize, heightMode);
                child.measure(widthSpec, heightSpec);

                if (consumeVertical) {
                    childHeightSize -= child.getMeasuredHeight();
                } else if (consumeHorizontal) {
                    childWidthSize -= child.getMeasuredWidth();
                }
            }
        }
    }

    mChildWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY);
    mChildHeightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeightSize, MeasureSpec.EXACTLY);

    // Make sure we have created all fragments that we need to have shown.
    mInLayout = true;
    populate();
    mInLayout = false;

    // Page views next.
    size = getChildCount();
    for (int i = 0; i < size; ++i) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            if (DEBUG)
                Log.v(TAG, "Measuring #" + i + " " + child + ": " + mChildWidthMeasureSpec);

            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (lp == null || !lp.isDecor) {
                if (isOrientationHorizontal()) {
                    final int widthSpec = MeasureSpec.makeMeasureSpec((int) (childWidthSize * lp.sizeFactor),
                            MeasureSpec.EXACTLY);
                    child.measure(widthSpec, mChildHeightMeasureSpec);
                } else {
                    final int heightSpec = MeasureSpec.makeMeasureSpec((int) (childHeightSize * lp.sizeFactor),
                            MeasureSpec.EXACTLY);
                    child.measure(mChildWidthMeasureSpec, heightSpec);
                }
            }
        }
    }
}

From source file:com.zyk.drawerlayout.widget.DrawerLayout.java

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    mInLayout = true;/*from  w w  w  . java  2  s .com*/
    final int width = r - l;
    final int height = b - 1;
    final int wi = width / 4;
    final int he = height / 4;
    mLeftHeader = Math.min(mLeftHeader, wi);
    mTopHeader = Math.min(mTopHeader, he);
    mRightHeader = Math.min(mRightHeader, wi);
    mBottomHeader = Math.min(mBottomHeader, he);
    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();

        if (isContentView(child)) {
            child.layout(lp.leftMargin, lp.topMargin, lp.leftMargin + child.getMeasuredWidth(),
                    lp.topMargin + 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 = 0;
            int childTop = 0;

            final float newOffset;
            if (checkDrawerViewAbsoluteGravity(child, Gravity.LEFT)) {
                childTop = lp.topMargin;
                if (mLeftHeader - childWidth >= child.getLeft()) {
                    childLeft = mLeftHeader - childWidth;
                    newOffset = 0;
                } else {
                    childLeft = -childWidth + (int) (childWidth * lp.onScreen);
                    newOffset = (float) (childWidth + childLeft) / childWidth;
                }
            } else if (checkDrawerViewAbsoluteGravity(child, Gravity.TOP)) {
                childLeft = lp.leftMargin;
                if (mTopHeader - childHeight >= child.getTop()) {
                    childTop = mTopHeader - childHeight;
                    newOffset = 0;
                } else {
                    childTop = -childHeight + (int) (childHeight * lp.onScreen);
                    newOffset = (float) (childTop + childHeight) / childHeight;
                }
            } else if (checkDrawerViewAbsoluteGravity(child, Gravity.RIGHT)) { // Right; onMeasure checked for us.
                childTop = lp.topMargin;
                if (width - mRightHeader >= child.getLeft()) {
                    childLeft = width - mRightHeader;
                    newOffset = 0;
                } else {
                    childLeft = width - (int) (childWidth * lp.onScreen);
                    newOffset = (float) (width - childLeft) / childWidth;
                }
            } else {
                childLeft = lp.leftMargin;
                if (height - mBottomHeader >= child.getTop()) {
                    childTop = height - mBottomHeader;
                    newOffset = 0;
                } else {
                    childTop = height - (int) (childHeight * lp.onScreen);
                    newOffset = (float) (height - childTop) / childHeight;
                }
            }

            final boolean changeOffset = newOffset != lp.onScreen;

            final int vgrav = lp.gravity & Gravity.VERTICAL_GRAVITY_MASK;
            //FIXME 
            if (vgrav == Gravity.CENTER_VERTICAL) {
                final int h = b - t;
                childTop = (h - childHeight) / 2;

                if (childTop < lp.topMargin) {
                    childTop = lp.topMargin;
                } else if (childTop + childHeight > h - lp.bottomMargin) {
                    childTop = h - lp.bottomMargin - childHeight;
                }
            }
            child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);

            if (changeOffset) {
                setDrawerViewOffset(child, newOffset);
            }

            //                final int newVisibility = lp.onScreen > 0 ? VISIBLE : INVISIBLE;
            //                if (child.getVisibility() != newVisibility) {
            //                    child.setVisibility(newVisibility);
            //                }
        }
    }
    mInLayout = false;
    mFirstLayout = false;
}

From source file:com.tjych.swip.vertical.DirectionalViewPager.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // For simple implementation, our internal size is always 0.
    // We depend on the container to specify the layout size of
    // our view.  We can't really know what it is since we will be
    // adding and removing different arbitrary views and do not
    // want the layout to change as this happens.
    setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));

    final int measuredWidth = getMeasuredWidth();
    final int measuredHeight = getMeasuredHeight();

    final int maxGutterSize = mOrientation == HORIZONTAL ? measuredWidth / 10 : measuredHeight / 10;
    mGutterSize = Math.min(maxGutterSize, mDefaultGutterSize);

    // Children are just made to fill our space.
    int childWidthSize = measuredWidth - getPaddingLeft() - getPaddingRight();
    int childHeightSize = measuredHeight - getPaddingTop() - getPaddingBottom();

    /*//from  w  w w  .j  a va  2 s.c  om
     * Make sure all children have been properly measured. Decor views first.
     * Right now we cheat and make this less complicated by assuming decor
     * views won't intersect. We will pin to edges based on gravity.
     */
    int size = getChildCount();
    for (int i = 0; i < size; ++i) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (lp != null && lp.isDecor) {
                final int hgrav = lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
                final int vgrav = lp.gravity & Gravity.VERTICAL_GRAVITY_MASK;
                int widthMode = MeasureSpec.AT_MOST;
                int heightMode = MeasureSpec.AT_MOST;
                boolean consumeVertical = vgrav == Gravity.TOP || vgrav == Gravity.BOTTOM;
                boolean consumeHorizontal = hgrav == Gravity.LEFT || hgrav == Gravity.RIGHT;

                if (consumeVertical) {
                    widthMode = MeasureSpec.EXACTLY;
                } else if (consumeHorizontal) {
                    heightMode = MeasureSpec.EXACTLY;
                }

                int widthSize = childWidthSize;
                int heightSize = childHeightSize;
                if (lp.width != LayoutParams.WRAP_CONTENT) {
                    widthMode = MeasureSpec.EXACTLY;
                    if (lp.width != LayoutParams.FILL_PARENT) {
                        widthSize = lp.width;
                    }
                }
                if (lp.height != LayoutParams.WRAP_CONTENT) {
                    heightMode = MeasureSpec.EXACTLY;
                    if (lp.height != LayoutParams.FILL_PARENT) {
                        heightSize = lp.height;
                    }
                }
                final int widthSpec = MeasureSpec.makeMeasureSpec(widthSize, widthMode);
                final int heightSpec = MeasureSpec.makeMeasureSpec(heightSize, heightMode);
                child.measure(widthSpec, heightSpec);

                if (consumeVertical) {
                    childHeightSize -= child.getMeasuredHeight();
                } else if (consumeHorizontal) {
                    childWidthSize -= child.getMeasuredWidth();
                }
            }
        }
    }

    mChildWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY);
    mChildHeightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeightSize, MeasureSpec.EXACTLY);

    // Make sure we have created all fragments that we need to have shown.
    mInLayout = true;
    populate();
    mInLayout = false;

    // Page views next.
    size = getChildCount();
    for (int i = 0; i < size; ++i) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            if (DEBUG)
                Log.v(TAG, "Measuring #" + i + " " + child + ": " + mChildWidthMeasureSpec);

            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (lp == null || !lp.isDecor) {
                int widthSpec;
                int heightSpec;

                if (mOrientation == HORIZONTAL) {
                    widthSpec = MeasureSpec.makeMeasureSpec((int) (childWidthSize * lp.dimensionFactor),
                            MeasureSpec.EXACTLY);
                    heightSpec = mChildHeightMeasureSpec;
                } else {
                    widthSpec = mChildWidthMeasureSpec;
                    heightSpec = MeasureSpec.makeMeasureSpec((int) (childHeightSize * lp.dimensionFactor),
                            MeasureSpec.EXACTLY);
                }

                child.measure(widthSpec, heightSpec);
            }
        }
    }
}