Example usage for android.view View getMeasuredHeight

List of usage examples for android.view View getMeasuredHeight

Introduction

In this page you can find the example usage for android.view View getMeasuredHeight.

Prototype

public final int getMeasuredHeight() 

Source Link

Document

Like #getMeasuredHeightAndState() , but only returns the raw height component (that is the result is masked by #MEASURED_SIZE_MASK ).

Usage

From source file:cc.flydev.launcher.Page.java

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    if (!mIsDataReady || getChildCount() == 0) {
        return;/*from   www .j a va2  s  .c  o  m*/
    }

    if (DEBUG)
        Log.d(TAG, "PagedView.onLayout()");
    final int childCount = getChildCount();

    int screenWidth = getViewportWidth();

    int offsetX = getViewportOffsetX();
    int offsetY = getViewportOffsetY();

    // Update the viewport offsets
    mViewport.offset(offsetX, offsetY);

    final boolean isRtl = isLayoutRtl();

    final int startIndex = isRtl ? childCount - 1 : 0;
    final int endIndex = isRtl ? -1 : childCount;
    final int delta = isRtl ? -1 : 1;

    int verticalPadding = getPaddingTop() + getPaddingBottom();

    int childLeft = mFirstChildLeft = offsetX + (screenWidth - getChildWidth(startIndex)) / 2;
    if (mPageScrolls == null || getChildCount() != mChildCountOnLastLayout) {
        mPageScrolls = new int[getChildCount()];
    }

    for (int i = startIndex; i != endIndex; i += delta) {
        final View child = getPageAt(i);
        if (child.getVisibility() != View.GONE) {
            LayoutParams lp = (LayoutParams) child.getLayoutParams();
            int childTop;
            if (lp.isFullScreenPage) {
                childTop = offsetY;
            } else {
                childTop = offsetY + getPaddingTop() + mInsets.top;
                if (mCenterPagesVertically) {
                    childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding
                            - child.getMeasuredHeight()) / 2;
                }
            }

            final int childWidth = child.getMeasuredWidth();
            final int childHeight = child.getMeasuredHeight();

            if (DEBUG)
                Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
            child.layout(childLeft, childTop, childLeft + child.getMeasuredWidth(), childTop + childHeight);

            // We assume the left and right padding are equal, and hence center the pages
            // horizontally
            int scrollOffset = (getViewportWidth() - childWidth) / 2;
            mPageScrolls[i] = childLeft - scrollOffset - offsetX;

            if (i != endIndex - delta) {
                childLeft += childWidth + scrollOffset;
                int nextScrollOffset = (getViewportWidth() - getChildWidth(i + delta)) / 2;
                childLeft += nextScrollOffset;
            }
        }
    }

    if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
        setHorizontalScrollBarEnabled(false);
        updateCurrentPageScroll();
        setHorizontalScrollBarEnabled(true);
        mFirstLayout = false;
    }

    if (childCount > 0) {
        final int index = isLayoutRtl() ? 0 : childCount - 1;
        mMaxScrollX = getScrollForPage(index);
    } else {
        mMaxScrollX = 0;
    }

    if (mScroller.isFinished() && mChildCountOnLastLayout != getChildCount() && !mDeferringForDelete) {
        if (mRestorePage != INVALID_RESTORE_PAGE) {
            setCurrentPage(mRestorePage);
            mRestorePage = INVALID_RESTORE_PAGE;
        } else {
            setCurrentPage(getNextPage());
        }
    }
    mChildCountOnLastLayout = getChildCount();

    if (isReordering(true)) {
        updateDragViewTranslationDuringDrag();
    }
}

From source file:android.support.v7.widget.ListPopupWindow.java

/**
 * <p>Builds the popup window's content and returns the height the popup
 * should have. Returns -1 when the content already exists.</p>
 *
 * @return the content's height or -1 if content already exists
 *///from   w  ww. j a  v  a2 s.c  o  m
private int buildDropDown() {
    ViewGroup dropDownView;
    int otherHeights = 0;

    if (mDropDownList == null) {
        Context context = mContext;

        /**
         * This Runnable exists for the sole purpose of checking if the view layout has got
         * completed and if so call showDropDown to display the drop down. This is used to show
         * the drop down as soon as possible after user opens up the search dialog, without
         * waiting for the normal UI pipeline to do it's job which is slower than this method.
         */
        mShowDropDownRunnable = new Runnable() {
            public void run() {
                // View layout should be all done before displaying the drop down.
                View view = getAnchorView();
                if (view != null && view.getWindowToken() != null) {
                    show();
                }
            }
        };

        mDropDownList = new DropDownListView(context, !mModal);
        if (mDropDownListHighlight != null) {
            mDropDownList.setSelector(mDropDownListHighlight);
        }
        mDropDownList.setAdapter(mAdapter);
        mDropDownList.setOnItemClickListener(mItemClickListener);
        mDropDownList.setFocusable(true);
        mDropDownList.setFocusableInTouchMode(true);
        mDropDownList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                if (position != -1) {
                    DropDownListView dropDownList = mDropDownList;

                    if (dropDownList != null) {
                        dropDownList.mListSelectionHidden = false;
                    }
                }
            }

            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        mDropDownList.setOnScrollListener(mScrollListener);

        if (mItemSelectedListener != null) {
            mDropDownList.setOnItemSelectedListener(mItemSelectedListener);
        }

        dropDownView = mDropDownList;

        View hintView = mPromptView;
        if (hintView != null) {
            // if a hint has been specified, we accomodate more space for it and
            // add a text view in the drop down menu, at the bottom of the list
            LinearLayout hintContainer = new LinearLayout(context);
            hintContainer.setOrientation(LinearLayout.VERTICAL);

            LinearLayout.LayoutParams hintParams = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, 0, 1.0f);

            switch (mPromptPosition) {
            case POSITION_PROMPT_BELOW:
                hintContainer.addView(dropDownView, hintParams);
                hintContainer.addView(hintView);
                break;

            case POSITION_PROMPT_ABOVE:
                hintContainer.addView(hintView);
                hintContainer.addView(dropDownView, hintParams);
                break;

            default:
                Log.e(TAG, "Invalid hint position " + mPromptPosition);
                break;
            }

            // measure the hint's height to find how much more vertical space
            // we need to add to the drop down's height
            int widthSpec = MeasureSpec.makeMeasureSpec(mDropDownWidth, MeasureSpec.AT_MOST);
            int heightSpec = MeasureSpec.UNSPECIFIED;
            hintView.measure(widthSpec, heightSpec);

            hintParams = (LinearLayout.LayoutParams) hintView.getLayoutParams();
            otherHeights = hintView.getMeasuredHeight() + hintParams.topMargin + hintParams.bottomMargin;

            dropDownView = hintContainer;
        }

        mPopup.setContentView(dropDownView);
    } else {
        dropDownView = (ViewGroup) mPopup.getContentView();
        final View view = mPromptView;
        if (view != null) {
            LinearLayout.LayoutParams hintParams = (LinearLayout.LayoutParams) view.getLayoutParams();
            otherHeights = view.getMeasuredHeight() + hintParams.topMargin + hintParams.bottomMargin;
        }
    }

    // getMaxAvailableHeight() subtracts the padding, so we put it back
    // to get the available height for the whole window
    int padding = 0;
    Drawable background = mPopup.getBackground();
    if (background != null) {
        background.getPadding(mTempRect);
        padding = mTempRect.top + mTempRect.bottom;

        // If we don't have an explicit vertical offset, determine one from the window
        // background so that content will line up.
        if (!mDropDownVerticalOffsetSet) {
            mDropDownVerticalOffset = -mTempRect.top;
        }
    } else {
        mTempRect.setEmpty();
    }

    // Max height available on the screen for a popup.
    boolean ignoreBottomDecorations = mPopup.getInputMethodMode() == PopupWindow.INPUT_METHOD_NOT_NEEDED;
    final int maxHeight = mPopup.getMaxAvailableHeight(getAnchorView(),
            mDropDownVerticalOffset /*, ignoreBottomDecorations*/);

    if (mDropDownAlwaysVisible || mDropDownHeight == ViewGroup.LayoutParams.MATCH_PARENT) {
        return maxHeight + padding;
    }

    final int childWidthSpec;
    switch (mDropDownWidth) {
    case ViewGroup.LayoutParams.WRAP_CONTENT:
        childWidthSpec = MeasureSpec.makeMeasureSpec(
                mContext.getResources().getDisplayMetrics().widthPixels - (mTempRect.left + mTempRect.right),
                MeasureSpec.AT_MOST);
        break;
    case ViewGroup.LayoutParams.MATCH_PARENT:
        childWidthSpec = MeasureSpec.makeMeasureSpec(
                mContext.getResources().getDisplayMetrics().widthPixels - (mTempRect.left + mTempRect.right),
                MeasureSpec.EXACTLY);
        break;
    default:
        childWidthSpec = MeasureSpec.makeMeasureSpec(mDropDownWidth, MeasureSpec.EXACTLY);
        break;
    }

    final int listContent = mDropDownList.measureHeightOfChildrenCompat(childWidthSpec, 0,
            DropDownListView.NO_POSITION, maxHeight - otherHeights, -1);
    // add padding only if the list has items in it, that way we don't show
    // the popup if it is not needed
    if (listContent > 0)
        otherHeights += padding;

    return listContent + otherHeights;
}

From source file:cn.bingoogolapple.swipebacklayout.BGASwipeBackLayout.java

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    final boolean isLayoutRtl = isLayoutRtlSupport();
    if (isLayoutRtl) {
        mDragHelper.setEdgeTrackingEnabled(ViewDragHelper.EDGE_RIGHT);
    } else {/*from  ww  w . j a  va  2 s.  co  m*/
        mDragHelper.setEdgeTrackingEnabled(ViewDragHelper.EDGE_LEFT);
    }
    final int width = r - l;
    final int paddingStart = isLayoutRtl ? getPaddingRight() : getPaddingLeft();
    final int paddingEnd = isLayoutRtl ? getPaddingLeft() : getPaddingRight();
    final int paddingTop = getPaddingTop();

    final int childCount = getChildCount();
    int xStart = paddingStart;
    int nextXStart = xStart;

    if (mFirstLayout) {
        mSlideOffset = mCanSlide && mPreservedOpenState ? 1.f : 0.f;
    }

    for (int i = 0; i < childCount; i++) {
        final View child = getChildAt(i);

        if (child.getVisibility() == GONE) {
            continue;
        }

        final LayoutParams lp = (LayoutParams) child.getLayoutParams();

        final int childWidth = child.getMeasuredWidth();
        int offset = 0;

        if (lp.slideable) {
            final int margin = lp.leftMargin + lp.rightMargin;
            final int range = Math.min(nextXStart, width - paddingEnd - mOverhangSize) - xStart - margin;
            mSlideRange = range;
            final int lpMargin = isLayoutRtl ? lp.rightMargin : lp.leftMargin;
            lp.dimWhenOffset = xStart + lpMargin + range + childWidth / 2 > width - paddingEnd;
            final int pos = (int) (range * mSlideOffset);
            xStart += pos + lpMargin;
            mSlideOffset = (float) pos / mSlideRange;
        } else if (mCanSlide && mParallaxBy != 0) {
            offset = (int) ((1 - mSlideOffset) * mParallaxBy);
            xStart = nextXStart;
        } else {
            xStart = nextXStart;
        }

        final int childRight;
        final int childLeft;
        if (isLayoutRtl) {
            childRight = width - xStart + offset;
            childLeft = childRight - childWidth;
        } else {
            childLeft = xStart - offset;
            childRight = childLeft + childWidth;
        }

        final int childTop = paddingTop;
        final int childBottom = childTop + child.getMeasuredHeight();
        child.layout(childLeft, paddingTop, childRight, childBottom);

        nextXStart += child.getWidth();
    }

    if (mFirstLayout) {
        if (mCanSlide) {
            if (mParallaxBy != 0) {
                parallaxOtherViews(mSlideOffset);
            }
            if (((LayoutParams) mSlideableView.getLayoutParams()).dimWhenOffset) {
                dimChildView(mSlideableView, mSlideOffset, mSliderFadeColor);
            }
        } else {
            // Reset the dim level of all children; it's irrelevant when nothing moves.
            for (int i = 0; i < childCount; i++) {
                dimChildView(getChildAt(i), 0, mSliderFadeColor);
            }
        }
        updateObscuredViewsVisibility(mSlideableView);
    }

    mFirstLayout = false;
}

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

/**
 * This method will be invoked when the current page is scrolled, either as part
 * of a programmatically initiated smooth scroll or a user initiated touch scroll.
 * If you override this method you must call through to the superclass implementation
 * (e.g. super.onPageScrolled(position, offset, offsetPixels)) before onPageScrolled
 * returns.//from ww  w  . ja va2 s . c o  m
 *
 * @param position Position index of the first page currently being displayed.
 *                 Page position+1 will be visible if positionOffset is nonzero.
 * @param offset Value from [0, 1) indicating the offset from the page at position.
 * @param offsetPixels Value in pixels indicating the offset from position.
 */
protected void onPageScrolled(int position, float offset, int offsetPixels) {
    // Offset any decor views if needed - keep them on-screen at all times.
    if (mDecorChildCount > 0) {
        final int scrollY = getScrollY();
        int paddingTop = getPaddingTop();
        int paddingBottom = getPaddingBottom();
        final int height = getHeight();
        final int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = getChildAt(i);
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (!lp.isDecor)
                continue;

            final int vgrav = lp.gravity & Gravity.VERTICAL_GRAVITY_MASK;
            int childTop = 0;
            switch (height) {
            default:
                childTop = paddingTop;
                break;
            case Gravity.TOP:
                childTop = paddingTop;
                paddingTop += child.getHeight();
                break;
            case Gravity.CENTER_HORIZONTAL:
                childTop = Math.max((height - child.getMeasuredHeight()) / 2, paddingTop);
                break;
            case Gravity.RIGHT:
                childTop = height - paddingBottom - child.getMeasuredHeight();
                paddingBottom += child.getMeasuredHeight();
                break;
            }
            childTop += scrollY;

            final int childOffset = childTop - child.getTop();
            if (childOffset != 0) {
                child.offsetTopAndBottom(childOffset);
            }
        }
    }

    if (mOnPageChangeListener != null) {
        mOnPageChangeListener.onPageScrolled(position, offset, offsetPixels);
    }
    if (mInternalPageChangeListener != null) {
        mInternalPageChangeListener.onPageScrolled(position, offset, offsetPixels);
    }

    if (mPageTransformer != null) {
        final int scrollY = getScrollY();
        final int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = getChildAt(i);
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();

            if (lp.isDecor)
                continue;

            final float transformPos = (float) (child.getTop() - scrollY) / getClientHeight();
            mPageTransformer.transformPage(child, transformPos);
        }
    }

    mCalledSuper = true;
}

From source file:de.vanita5.twittnuker.util.Utils.java

public static String getMapStaticImageUri(final double lat, final double lng, final View v) {
    if (v == null)
        return null;
    final int wSpec = MeasureSpec.makeMeasureSpec(v.getWidth(), MeasureSpec.UNSPECIFIED);
    final int hSpec = MeasureSpec.makeMeasureSpec(v.getHeight(), MeasureSpec.UNSPECIFIED);
    v.measure(wSpec, hSpec);/* w ww. j av  a2  s.  c  om*/
    return getMapStaticImageUri(lat, lng, 12, v.getMeasuredWidth(), v.getMeasuredHeight(),
            v.getResources().getConfiguration().locale);
}

From source file:com.cliff.material.widget.ListPopupWindow.java

/**
 * <p>Builds the popup window's content and returns the height the popup
 * should have. Returns -1 when the content already exists.</p>
 *
 * @return the content's height or -1 if content already exists
 *//*w  w  w . ja v a  2  s.c  o m*/
private int buildDropDown() {
    int otherHeights = 0;

    if (mDropDownList == null) {
        ViewGroup dropDownView;
        Context context = mContext;

        /**
         * This Runnable exists for the sole purpose of checking if the view layout has got
         * completed and if so call showDropDown to display the drop down. This is used to show
         * the drop down as soon as possible after user opens up the search dialog, without
         * waiting for the normal UI pipeline to do it's job which is slower than this method.
         */
        mShowDropDownRunnable = new Runnable() {
            public void run() {
                // View layout should be all done before displaying the drop down.
                View view = getAnchorView();
                if (view != null && view.getWindowToken() != null) {
                    show();
                }
            }
        };

        mDropDownList = new DropDownListView(context, !mModal);
        if (mDropDownListHighlight != null) {
            mDropDownList.setSelector(mDropDownListHighlight);
        }
        mDropDownList.setAdapter(mAdapter);
        mDropDownList.setOnItemClickListener(mItemClickListener);
        mDropDownList.setFocusable(true);
        mDropDownList.setFocusableInTouchMode(true);
        mDropDownList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                if (position != -1) {
                    DropDownListView dropDownList = mDropDownList;

                    if (dropDownList != null) {
                        dropDownList.mListSelectionHidden = false;
                    }
                }
            }

            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        mDropDownList.setOnScrollListener(mScrollListener);

        if (mItemSelectedListener != null) {
            mDropDownList.setOnItemSelectedListener(mItemSelectedListener);
        }

        dropDownView = mDropDownList;

        View hintView = mPromptView;
        if (hintView != null) {
            // if a hint has been specified, we accomodate more space for it and
            // add a text view in the drop down menu, at the bottom of the list
            LinearLayout hintContainer = new LinearLayout(context);
            hintContainer.setOrientation(LinearLayout.VERTICAL);

            LinearLayout.LayoutParams hintParams = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, 0, 1.0f);

            switch (mPromptPosition) {
            case POSITION_PROMPT_BELOW:
                hintContainer.addView(dropDownView, hintParams);
                hintContainer.addView(hintView);
                break;

            case POSITION_PROMPT_ABOVE:
                hintContainer.addView(hintView);
                hintContainer.addView(dropDownView, hintParams);
                break;

            default:
                Log.e(TAG, "Invalid hint position " + mPromptPosition);
                break;
            }

            // measure the hint's height to find how much more vertical space
            // we need to add to the drop down's height
            int widthSpec = MeasureSpec.makeMeasureSpec(mDropDownWidth, MeasureSpec.AT_MOST);
            int heightSpec = MeasureSpec.UNSPECIFIED;
            hintView.measure(widthSpec, heightSpec);

            hintParams = (LinearLayout.LayoutParams) hintView.getLayoutParams();
            otherHeights = hintView.getMeasuredHeight() + hintParams.topMargin + hintParams.bottomMargin;

            dropDownView = hintContainer;
        }

        mPopup.setContentView(dropDownView);

    } else {
        final View view = mPromptView;
        if (view != null) {
            LinearLayout.LayoutParams hintParams = (LinearLayout.LayoutParams) view.getLayoutParams();
            otherHeights = view.getMeasuredHeight() + hintParams.topMargin + hintParams.bottomMargin;
        }
    }

    // getMaxAvailableHeight() subtracts the padding, so we put it back
    // to get the available height for the whole window
    int padding = 0;
    Drawable background = mPopup.getBackground();
    if (background != null) {
        background.getPadding(mTempRect);
        padding = mTempRect.top + mTempRect.bottom;

        // If we don't have an explicit vertical offset, determine one from the window
        // background so that content will line up.
        if (!mDropDownVerticalOffsetSet) {
            mDropDownVerticalOffset = -mTempRect.top;
        }
    } else {
        mTempRect.setEmpty();
    }

    // Max height available on the screen for a popup.
    boolean ignoreBottomDecorations = mPopup.getInputMethodMode() == PopupWindow.INPUT_METHOD_NOT_NEEDED;
    final int maxHeight = mPopup.getMaxAvailableHeight(getAnchorView(),
            mDropDownVerticalOffset /*, ignoreBottomDecorations*/);

    if (mDropDownAlwaysVisible || mDropDownHeight == ViewGroup.LayoutParams.MATCH_PARENT) {
        return maxHeight + padding;
    }

    final int childWidthSpec;
    switch (mDropDownWidth) {
    case ViewGroup.LayoutParams.WRAP_CONTENT:
        childWidthSpec = MeasureSpec.makeMeasureSpec(
                mContext.getResources().getDisplayMetrics().widthPixels - (mTempRect.left + mTempRect.right),
                MeasureSpec.AT_MOST);
        break;
    case ViewGroup.LayoutParams.MATCH_PARENT:
        childWidthSpec = MeasureSpec.makeMeasureSpec(
                mContext.getResources().getDisplayMetrics().widthPixels - (mTempRect.left + mTempRect.right),
                MeasureSpec.EXACTLY);
        break;
    default:
        childWidthSpec = MeasureSpec.makeMeasureSpec(mDropDownWidth, MeasureSpec.EXACTLY);
        break;
    }

    final int listContent = mDropDownList.measureHeightOfChildrenCompat(childWidthSpec, 0,
            DropDownListView.NO_POSITION, maxHeight - otherHeights, -1);
    // add padding only if the list has items in it, that way we don't show
    // the popup if it is not needed
    if (listContent > 0)
        otherHeights += padding;

    return listContent + otherHeights;
}

From source file:com.android.leanlauncher.CellLayout.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    LauncherAppState app = LauncherAppState.getInstance();
    DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();

    int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
    int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);
    int childWidthSize = widthSize - (getPaddingLeft() + getPaddingRight());
    int childHeightSize = heightSize - (getPaddingTop() + getPaddingBottom());
    if (mFixedCellWidth < 0 || mFixedCellHeight < 0) {
        int cw = grid.calculateCellWidth(childWidthSize, mCountX);
        int ch = grid.calculateCellHeight(childHeightSize, mCountY);
        if (cw != mCellWidth || ch != mCellHeight) {
            mCellWidth = cw;//from w  w  w  . jav a 2s .com
            mCellHeight = ch;
            mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap, mCountX,
                    mCountY);
        }
    }

    int newWidth = childWidthSize;
    int newHeight = childHeightSize;
    if (mFixedWidth > 0 && mFixedHeight > 0) {
        newWidth = mFixedWidth;
        newHeight = mFixedHeight;
    } else if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
        throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions");
    }

    int numWidthGaps = mCountX - 1;
    int numHeightGaps = mCountY - 1;

    if (mOriginalWidthGap < 0 || mOriginalHeightGap < 0) {
        int hFreeSpace = childWidthSize - (mCountX * mCellWidth);
        int vFreeSpace = childHeightSize - (mCountY * mCellHeight);
        mWidthGap = Math.min(mMaxGap, numWidthGaps > 0 ? (hFreeSpace / numWidthGaps) : 0);
        mHeightGap = Math.min(mMaxGap, numHeightGaps > 0 ? (vFreeSpace / numHeightGaps) : 0);
        mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mWidthGap, mHeightGap, mCountX,
                mCountY);
    } else {
        mWidthGap = mOriginalWidthGap;
        mHeightGap = mOriginalHeightGap;
    }
    int count = getChildCount();
    int maxWidth = 0;
    int maxHeight = 0;
    for (int i = 0; i < count; i++) {
        View child = getChildAt(i);
        int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY);
        int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY);
        child.measure(childWidthMeasureSpec, childheightMeasureSpec);
        maxWidth = Math.max(maxWidth, child.getMeasuredWidth());
        maxHeight = Math.max(maxHeight, child.getMeasuredHeight());
    }
    if (mFixedWidth > 0 && mFixedHeight > 0) {
        setMeasuredDimension(maxWidth, maxHeight);
    } else {
        setMeasuredDimension(widthSize, heightSize);
    }
}

From source file:chickennugget.spaceengineersdata.material.widgets.ListPopupWindow.java

/**
 * <p>Builds the popup window's content and returns the height the popup
 * should have. Returns -1 when the content already exists.</p>
 *
 * @return the content's height or -1 if content already exists
 *//*from   w  w  w.  j av  a2  s.c  o  m*/
private int buildDropDown() {
    int otherHeights = 0;

    if (mDropDownList == null) {
        ViewGroup dropDownView;
        Context context = mContext;

        /**
         * This Runnable exists for the sole purpose of checking if the view layout has got
         * completed and if so call showDropDown to display the drop down. This is used to show
         * the drop down as soon as possible after user opens up the search dialog, without
         * waiting for the normal UI pipeline to do it's job which is slower than this method.
         */
        mShowDropDownRunnable = new Runnable() {
            public void run() {
                // View layout should be all done before displaying the drop down.
                View view = getAnchorView();
                if (view != null && view.getWindowToken() != null) {
                    show();
                }
            }
        };

        mDropDownList = new DropDownListView(context, !mModal);
        if (mDropDownListHighlight != null) {
            mDropDownList.setSelector(mDropDownListHighlight);
        }
        mDropDownList.setAdapter(mAdapter);
        mDropDownList.setOnItemClickListener(mItemClickListener);
        mDropDownList.setFocusable(true);
        mDropDownList.setFocusableInTouchMode(true);
        mDropDownList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                if (position != -1) {
                    DropDownListView dropDownList = mDropDownList;

                    if (dropDownList != null) {
                        dropDownList.mListSelectionHidden = false;
                    }
                }
            }

            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        mDropDownList.setOnScrollListener(mScrollListener);

        if (mItemSelectedListener != null) {
            mDropDownList.setOnItemSelectedListener(mItemSelectedListener);
        }

        dropDownView = mDropDownList;

        View hintView = mPromptView;
        if (hintView != null) {
            // if a hint has been specified, we accomodate more space for it and
            // add a text view in the drop down menu, at the bottom of the list
            LinearLayout hintContainer = new LinearLayout(context);
            hintContainer.setOrientation(LinearLayout.VERTICAL);

            LinearLayout.LayoutParams hintParams = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, 0, 1.0f);

            switch (mPromptPosition) {
            case POSITION_PROMPT_BELOW:
                hintContainer.addView(dropDownView, hintParams);
                hintContainer.addView(hintView);
                break;

            case POSITION_PROMPT_ABOVE:
                hintContainer.addView(hintView);
                hintContainer.addView(dropDownView, hintParams);
                break;

            default:
                Log.e(TAG, "Invalid hint position " + mPromptPosition);
                break;
            }

            // measure the hint's height to find how much more vertical space
            // we need to add to the drop down's height
            int widthSpec = MeasureSpec.makeMeasureSpec(mDropDownWidth, MeasureSpec.AT_MOST);
            int heightSpec = MeasureSpec.UNSPECIFIED;
            hintView.measure(widthSpec, heightSpec);

            hintParams = (LinearLayout.LayoutParams) hintView.getLayoutParams();
            otherHeights = hintView.getMeasuredHeight() + hintParams.topMargin + hintParams.bottomMargin;

            dropDownView = hintContainer;
        }

        mPopup.setContentView(dropDownView);

    } else {
        final View view = mPromptView;
        if (view != null) {
            LinearLayout.LayoutParams hintParams = (LinearLayout.LayoutParams) view.getLayoutParams();
            otherHeights = view.getMeasuredHeight() + hintParams.topMargin + hintParams.bottomMargin;
        }
    }

    // getMaxAvailableHeight() subtracts the padding, so we put it back
    // to get the available height for the whole window
    int padding = 0;
    Drawable background = mPopup.getBackground();
    if (background != null) {
        background.getPadding(mTempRect);
        padding = mTempRect.top + mTempRect.bottom;

        // If we don't have an explicit vertical offset, determine one from the window
        // background so that content will line up.
        if (!mDropDownVerticalOffsetSet) {
            mDropDownVerticalOffset = -mTempRect.top;
        }
    } else {
        mTempRect.setEmpty();
    }

    int systemBarsReservedSpace = 0;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        //  getMaxAvailableHeight() on Lollipop seems to ignore the system bars.
        systemBarsReservedSpace = Math.max(getSystemBarHeight("status_bar_height"),
                getSystemBarHeight("navigation_bar_height"));
    }

    // Max height available on the screen for a popup.
    boolean ignoreBottomDecorations = mPopup.getInputMethodMode() == PopupWindow.INPUT_METHOD_NOT_NEEDED;
    final int maxHeight = mPopup.getMaxAvailableHeight(getAnchorView(),
            mDropDownVerticalOffset /*, ignoreBottomDecorations*/) - systemBarsReservedSpace;

    if (mDropDownAlwaysVisible || mDropDownHeight == ViewGroup.LayoutParams.MATCH_PARENT) {
        return maxHeight + padding;
    }

    final int childWidthSpec;
    switch (mDropDownWidth) {
    case ViewGroup.LayoutParams.WRAP_CONTENT:
        childWidthSpec = MeasureSpec.makeMeasureSpec(
                mContext.getResources().getDisplayMetrics().widthPixels - (mTempRect.left + mTempRect.right),
                MeasureSpec.AT_MOST);
        break;
    case ViewGroup.LayoutParams.MATCH_PARENT:
        childWidthSpec = MeasureSpec.makeMeasureSpec(
                mContext.getResources().getDisplayMetrics().widthPixels - (mTempRect.left + mTempRect.right),
                MeasureSpec.EXACTLY);
        break;
    default:
        childWidthSpec = MeasureSpec.makeMeasureSpec(mDropDownWidth, MeasureSpec.EXACTLY);
        break;
    }

    final int listContent = mDropDownList.measureHeightOfChildrenCompat(childWidthSpec, 0,
            DropDownListView.NO_POSITION, maxHeight - otherHeights, -1);
    // add padding only if the list has items in it, that way we don't show
    // the popup if it is not needed
    if (listContent > 0)
        otherHeights += padding;

    return listContent + otherHeights;
}

From source file:com.baidu.zhuanche.view.LazyViewPager.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));

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

    /*//from  w ww  . ja  v  a  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;
                Log.d(TAG, "gravity: " + lp.gravity + " hgrav: " + hgrav + " vgrav: " + vgrav);
                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;
                }

                final int widthSpec = MeasureSpec.makeMeasureSpec(childWidthSize, widthMode);
                final int heightSpec = MeasureSpec.makeMeasureSpec(childHeightSize, 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) {
                child.measure(mChildWidthMeasureSpec, mChildHeightMeasureSpec);
            }
        }
    }
}

From source file:cn.ieclipse.af.view.StaggeredGridView.java

/**
 * Measure and layout all currently visible children.
 *
 * @param queryAdapter true to requery the adapter for view data
 *//*from   w w w  .  j  a va 2 s  .c om*/
final void layoutChildren(boolean queryAdapter) {
    final int paddingLeft = getPaddingLeft();
    final int paddingRight = getPaddingRight();
    final int itemMargin = mItemMargin;
    final int colWidth = (getWidth() - paddingLeft - paddingRight - itemMargin * (mColCount - 1)) / mColCount;
    int rebuildLayoutRecordsBefore = -1;
    int rebuildLayoutRecordsAfter = -1;

    Arrays.fill(mItemBottoms, Integer.MIN_VALUE);

    final int childCount = getChildCount();
    for (int i = 0; i < childCount; i++) {
        View child = getChildAt(i);
        LayoutParams lp = (LayoutParams) child.getLayoutParams();
        final int col = lp.column;
        final int position = mFirstPosition + i;
        final boolean needsLayout = queryAdapter || child.isLayoutRequested();

        if (queryAdapter) {
            View newView = obtainView(position, child);
            if (newView != child) {
                removeViewAt(i);
                addView(newView, i);
                child = newView;
            }
            lp = (LayoutParams) child.getLayoutParams(); // Might have changed
        }

        final int span = Math.min(mColCount, lp.span);
        final int widthSize = colWidth * span + itemMargin * (span - 1);

        if (needsLayout) {
            final int widthSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY);

            final int heightSpec;
            if (lp.height == LayoutParams.WRAP_CONTENT) {
                heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
            } else {
                heightSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY);
            }

            child.measure(widthSpec, heightSpec);
        }

        int childTop = mItemBottoms[col] > Integer.MIN_VALUE ? mItemBottoms[col] + mItemMargin : child.getTop();
        if (span > 1) {
            int lowest = childTop;
            for (int j = col + 1; j < col + span; j++) {
                final int bottom = mItemBottoms[j] + mItemMargin;
                if (bottom > lowest) {
                    lowest = bottom;
                }
            }
            childTop = lowest;
        }
        final int childHeight = child.getMeasuredHeight();
        final int childBottom = childTop + childHeight;
        final int childLeft = paddingLeft + col * (colWidth + itemMargin);
        final int childRight = childLeft + child.getMeasuredWidth();
        child.layout(childLeft, childTop, childRight, childBottom);

        for (int j = col; j < col + span; j++) {
            mItemBottoms[j] = childBottom;
        }

        final LayoutRecord rec = mLayoutRecords.get(position);
        if (rec != null && rec.height != childHeight) {
            // Invalidate our layout records for everything before this.
            rec.height = childHeight;
            rebuildLayoutRecordsBefore = position;
        }

        if (rec != null && rec.span != span) {
            // Invalidate our layout records for everything after this.
            rec.span = span;
            rebuildLayoutRecordsAfter = position;
        }
    }

    // Update mItemBottoms for any empty columns
    for (int i = 0; i < mColCount; i++) {
        if (mItemBottoms[i] == Integer.MIN_VALUE) {
            mItemBottoms[i] = mItemTops[i];
        }
    }

    if (rebuildLayoutRecordsBefore >= 0 || rebuildLayoutRecordsAfter >= 0) {
        if (rebuildLayoutRecordsBefore >= 0) {
            invalidateLayoutRecordsBeforePosition(rebuildLayoutRecordsBefore);
        }
        if (rebuildLayoutRecordsAfter >= 0) {
            invalidateLayoutRecordsAfterPosition(rebuildLayoutRecordsAfter);
        }
        for (int i = 0; i < childCount; i++) {
            final int position = mFirstPosition + i;
            final View child = getChildAt(i);
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            LayoutRecord rec = mLayoutRecords.get(position);
            if (rec == null) {
                rec = new LayoutRecord();
                mLayoutRecords.put(position, rec);
            }
            rec.column = lp.column;
            rec.height = child.getHeight();
            rec.id = lp.id;
            rec.span = Math.min(mColCount, lp.span);
        }
    }
}