Example usage for android.view View measure

List of usage examples for android.view View measure

Introduction

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

Prototype

public final void measure(int widthMeasureSpec, int heightMeasureSpec) 

Source Link

Document

This is called to find out how big a view should be.

Usage

From source file:android.support.v7.internal.widget.ListViewCompat.java

/**
 * Measures the height of the given range of children (inclusive) and returns the height
 * with this ListView's padding and divider heights included. If maxHeight is provided, the
 * measuring will stop when the current height reaches maxHeight.
 *
 * @param widthMeasureSpec             The width measure spec to be given to a child's
 *                                     {@link View#measure(int, int)}.
 * @param startPosition                The position of the first child to be shown.
 * @param endPosition                  The (inclusive) position of the last child to be
 *                                     shown. Specify {@link #NO_POSITION} if the last child
 *                                     should be the last available child from the adapter.
 * @param maxHeight                    The maximum height that will be returned (if all the
 *                                     children don't fit in this value, this value will be
 *                                     returned).
 * @param disallowPartialChildPosition In general, whether the returned height should only
 *                                     contain entire children. This is more powerful--it is
 *                                     the first inclusive position at which partial
 *                                     children will not be allowed. Example: it looks nice
 *                                     to have at least 3 completely visible children, and
 *                                     in portrait this will most likely fit; but in
 *                                     landscape there could be times when even 2 children
 *                                     can not be completely shown, so a value of 2
 *                                     (remember, inclusive) would be good (assuming
 *                                     startPosition is 0).
 * @return The height of this ListView with the given children.
 *///from www.  j  a v a2  s.c  om
public int measureHeightOfChildrenCompat(int widthMeasureSpec, int startPosition, int endPosition,
        final int maxHeight, int disallowPartialChildPosition) {

    final int paddingTop = getListPaddingTop();
    final int paddingBottom = getListPaddingBottom();
    final int paddingLeft = getListPaddingLeft();
    final int paddingRight = getListPaddingRight();
    final int reportedDividerHeight = getDividerHeight();
    final Drawable divider = getDivider();

    final ListAdapter adapter = getAdapter();

    if (adapter == null) {
        return paddingTop + paddingBottom;
    }

    // Include the padding of the list
    int returnedHeight = paddingTop + paddingBottom;
    final int dividerHeight = ((reportedDividerHeight > 0) && divider != null) ? reportedDividerHeight : 0;

    // The previous height value that was less than maxHeight and contained
    // no partial children
    int prevHeightWithoutPartialChild = 0;

    View child = null;
    int viewType = 0;
    int count = adapter.getCount();
    for (int i = 0; i < count; i++) {
        int newType = adapter.getItemViewType(i);
        if (newType != viewType) {
            child = null;
            viewType = newType;
        }
        child = adapter.getView(i, child, this);

        // Compute child height spec
        int heightMeasureSpec;
        final ViewGroup.LayoutParams childLp = child.getLayoutParams();
        if (childLp != null && childLp.height > 0) {
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(childLp.height, MeasureSpec.EXACTLY);
        } else {
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        }
        child.measure(widthMeasureSpec, heightMeasureSpec);

        if (i > 0) {
            // Count the divider for all but one child
            returnedHeight += dividerHeight;
        }

        returnedHeight += child.getMeasuredHeight();

        if (returnedHeight >= maxHeight) {
            // We went over, figure out which height to return.  If returnedHeight >
            // maxHeight, then the i'th position did not fit completely.
            return (disallowPartialChildPosition >= 0) // Disallowing is enabled (> -1)
                    && (i > disallowPartialChildPosition) // We've past the min pos
                    && (prevHeightWithoutPartialChild > 0) // We have a prev height
                    && (returnedHeight != maxHeight) // i'th child did not fit completely
                            ? prevHeightWithoutPartialChild
                            : maxHeight;
        }

        if ((disallowPartialChildPosition >= 0) && (i >= disallowPartialChildPosition)) {
            prevHeightWithoutPartialChild = returnedHeight;
        }
    }

    // At this point, we went through the range of children, and they each
    // completely fit, so return the returnedHeight
    return returnedHeight;
}

From source file:cc.mintcoin.wallet.ui.SendCoinsFragment.java

private void popup(@Nonnull final View anchor, @Nonnull final View contentView) {
    contentView.measure(MeasureSpec.makeMeasureSpec(MeasureSpec.UNSPECIFIED, 0),
            MeasureSpec.makeMeasureSpec(MeasureSpec.UNSPECIFIED, 0));

    popupWindow = new PopupWindow(contentView, contentView.getMeasuredWidth(), contentView.getMeasuredHeight(),
            false);// w  w w .  ja v  a  2  s .  com
    popupWindow.showAsDropDown(anchor);

    // hack
    contentView.setBackgroundResource(
            popupWindow.isAboveAnchor() ? R.drawable.popup_frame_above : R.drawable.popup_frame_below);
}

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

public boolean flagActionItems() {
    final ArrayList<MenuItemImpl> visibleItems = mMenu.getVisibleItems();
    final int itemsSize = visibleItems.size();
    int maxActions = mMaxItems;
    int widthLimit = mActionItemWidthLimit;
    final int querySpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    final ViewGroup parent = (ViewGroup) mMenuView;

    int requiredItems = 0;
    int requestedItems = 0;
    int firstActionWidth = 0;
    boolean hasOverflow = false;
    for (int i = 0; i < itemsSize; i++) {
        MenuItemImpl item = visibleItems.get(i);
        if (item.requiresActionButton()) {
            requiredItems++;/*from   w  w  w .j  a  v a 2  s  .co m*/
        } else if (item.requestsActionButton()) {
            requestedItems++;
        } else {
            hasOverflow = true;
        }
        if (mExpandedActionViewsExclusive && item.isActionViewExpanded()) {
            // Overflow everything if we have an expanded action view and we're
            // space constrained.
            maxActions = 0;
        }
    }

    // Reserve a spot for the overflow item if needed.
    if (mReserveOverflow && (hasOverflow || requiredItems + requestedItems > maxActions)) {
        maxActions--;
    }
    maxActions -= requiredItems;

    final SparseBooleanArray seenGroups = mActionButtonGroups;
    seenGroups.clear();

    int cellSize = 0;
    int cellsRemaining = 0;
    if (mStrictWidthLimit) {
        cellsRemaining = widthLimit / mMinCellSize;
        final int cellSizeRemaining = widthLimit % mMinCellSize;
        cellSize = mMinCellSize + cellSizeRemaining / cellsRemaining;
    }

    // Flag as many more requested items as will fit.
    for (int i = 0; i < itemsSize; i++) {
        MenuItemImpl item = visibleItems.get(i);

        if (item.requiresActionButton()) {
            View v = getItemView(item, mScrapActionButtonView, parent);
            if (mScrapActionButtonView == null) {
                mScrapActionButtonView = v;
            }
            if (mStrictWidthLimit) {
                cellsRemaining -= ActionMenuView.measureChildForCells(v, cellSize, cellsRemaining, querySpec,
                        0);
            } else {
                v.measure(querySpec, querySpec);
            }
            final int measuredWidth = v.getMeasuredWidth();
            widthLimit -= measuredWidth;
            if (firstActionWidth == 0) {
                firstActionWidth = measuredWidth;
            }
            final int groupId = item.getGroupId();
            if (groupId != 0) {
                seenGroups.put(groupId, true);
            }
            item.setIsActionButton(true);
        } else if (item.requestsActionButton()) {
            // Items in a group with other items that already have an action slot
            // can break the max actions rule, but not the width limit.
            final int groupId = item.getGroupId();
            final boolean inGroup = seenGroups.get(groupId);
            boolean isAction = (maxActions > 0 || inGroup) && widthLimit > 0
                    && (!mStrictWidthLimit || cellsRemaining > 0);

            if (isAction) {
                View v = getItemView(item, mScrapActionButtonView, parent);
                if (mScrapActionButtonView == null) {
                    mScrapActionButtonView = v;
                }
                if (mStrictWidthLimit) {
                    final int cells = ActionMenuView.measureChildForCells(v, cellSize, cellsRemaining,
                            querySpec, 0);
                    cellsRemaining -= cells;
                    if (cells == 0) {
                        isAction = false;
                    }
                } else {
                    v.measure(querySpec, querySpec);
                }
                final int measuredWidth = v.getMeasuredWidth();
                widthLimit -= measuredWidth;
                if (firstActionWidth == 0) {
                    firstActionWidth = measuredWidth;
                }

                if (mStrictWidthLimit) {
                    isAction &= widthLimit >= 0;
                } else {
                    // Did this push the entire first item past the limit?
                    isAction &= widthLimit + firstActionWidth > 0;
                }
            }

            if (isAction && groupId != 0) {
                seenGroups.put(groupId, true);
            } else if (inGroup) {
                // We broke the width limit. Demote the whole group, they all overflow now.
                seenGroups.put(groupId, false);
                for (int j = 0; j < i; j++) {
                    MenuItemImpl areYouMyGroupie = visibleItems.get(j);
                    if (areYouMyGroupie.getGroupId() == groupId) {
                        // Give back the action slot
                        if (areYouMyGroupie.isActionButton())
                            maxActions++;
                        areYouMyGroupie.setIsActionButton(false);
                    }
                }
            }

            if (isAction)
                maxActions--;

            item.setIsActionButton(isAction);
        } else {
            // Neither requires nor requests an action button.
            item.setIsActionButton(false);
        }
    }
    return true;
}

From source file:com.library.core.view.HorizontalListView.java

private void addAndMeasureChild(final View child, int viewPos) {
    LayoutParams params = (LayoutParams) child.getLayoutParams();
    params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);

    addViewInLayout(child, viewPos, params, true);

    int heightMeasureSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY);
    int childHeightSpec = ViewGroup.getChildMeasureSpec(heightMeasureSpec, getPaddingTop() + getPaddingBottom(),
            params.height);/*  w  ww . j a  v  a 2s .  co m*/
    int childWidthSpec = MeasureSpec.makeMeasureSpec(params.width > 0 ? params.width : 0,
            MeasureSpec.UNSPECIFIED);
    child.measure(childWidthSpec, childHeightSpec);

}

From source file:com.coco.draggablegridviewpager.DraggableGridViewPager.java

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    final int childCount = getChildCount();
    mPageCount = (childCount + mPageSize - 1) / mPageSize;
    mGridWidth = (getWidth() - mPaddingLeft - mPaddingRight - (mColCount - 1) * mGridGap) / mColCount;
    mGridHeight = (getHeight() - mPaddingTop - mPaddingButtom - (mRowCount - 1) * mGridGap) / mRowCount;
    mGridWidth = mGridHeight = Math.min(mGridWidth, mGridHeight);
    mMaxOverScrollSize = mGridWidth / 2;
    mEdgeSize = mGridWidth / 2;//from  www . java  2s .  c  om
    newPositions.clear();
    for (int i = 0; i < childCount; i++) {
        final View child = getChildAt(i);
        final Rect rect = getRectByPosition(i);
        child.measure(MeasureSpec.makeMeasureSpec(rect.width(), MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(rect.height(), MeasureSpec.EXACTLY));
        DEBUG_LOG("child.layout position=" + i + ", rect=" + rect);
        child.layout(rect.left, rect.top, rect.right, rect.bottom);
        newPositions.add(-1);
    }
    if (mCurItem > 0 && mCurItem < mPageCount) {
        final int curItem = mCurItem;
        mCurItem = 0;
        setCurrentItem(curItem);
    }
}

From source file:com.acbelter.scheduleview.ScheduleView.java

private void addAndMeasureItemViews() {
    View child;
    for (int i = 0; i < mAdapter.getCount(); i++) {
        // TODO Cache non visible views and use them
        child = mAdapter.getView(i, null, this);
        ViewGroup.LayoutParams params = child.getLayoutParams();
        if (params == null) {
            params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        }//from  w ww .  ja  va 2  s.  co m
        addViewInLayout(child, i, params, true);

        GeneralScheduleItem item = mAdapter.getItem(i);
        child.measure(MeasureSpec.makeMeasureSpec(mItemWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(
                calculateDistance(item.getStartTime(), item.getEndTime()), MeasureSpec.EXACTLY));
    }
    setSelection();
}

From source file:android.widget.PinnedHeaderListView.java

private void ensurePinnedHeaderLayout(final int viewIndex) {
    final View view = mHeaders[viewIndex].view;
    if (view.isLayoutRequested()) {
        final ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        int widthSpec;
        int heightSpec;

        if (layoutParams != null && layoutParams.width > 0)
            widthSpec = View.MeasureSpec.makeMeasureSpec(layoutParams.width, View.MeasureSpec.EXACTLY);
        else/*from  w ww  .j  a  va  2  s .com*/
            widthSpec = View.MeasureSpec.makeMeasureSpec(mHeaderWidth, View.MeasureSpec.EXACTLY);

        if (layoutParams != null && layoutParams.height > 0)
            heightSpec = View.MeasureSpec.makeMeasureSpec(layoutParams.height, View.MeasureSpec.EXACTLY);
        else
            heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        view.measure(widthSpec, heightSpec);
        final int height = view.getMeasuredHeight();
        mHeaders[viewIndex].height = height;
        view.layout(0, 0, view.getMeasuredWidth(), height);
    }
}

From source file:com.aizou.core.widget.pagerIndicator.indicator.FixedIndicatorView.java

private void measureScrollBar(boolean needChange) {
    if (scrollBar == null)
        return;//from   w  w  w .  ja v a  2 s . c om
    View view = scrollBar.getSlideView();
    if (view.isLayoutRequested() || needChange) {
        if (mAdapter != null && mAdapter.getCount() > 0 && mSelectedTabIndex >= 0
                && mSelectedTabIndex < mAdapter.getCount()) {
            int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), mWidthMode);
            int heightSpec;
            ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
            if (layoutParams != null && layoutParams.height > 0) {
                heightSpec = MeasureSpec.makeMeasureSpec(layoutParams.height, MeasureSpec.EXACTLY);
            } else {
                heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
            }
            view.measure(widthSpec, heightSpec);
            View curr = getChildAt(mSelectedTabIndex);
            view.layout(0, 0, scrollBar.getWidth(curr.getMeasuredWidth()), scrollBar.getHeight(getHeight()));
        }
    }
}

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

private void measureChildWithDecorationsAndMargin(View child, int widthSpec, int heightSpec) {
    calculateItemDecorationsForChild(child, mDecorInsets);
    RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) child.getLayoutParams();
    widthSpec = updateSpecWithExtra(widthSpec, lp.leftMargin + mDecorInsets.left,
            lp.rightMargin + mDecorInsets.right);
    heightSpec = updateSpecWithExtra(heightSpec, lp.topMargin + mDecorInsets.top,
            lp.bottomMargin + mDecorInsets.bottom);
    child.measure(widthSpec, heightSpec);
}