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:com.appunite.list.HorizontalListView.java

/**
 * Measure a particular list child./*  w  w  w .  j av a 2  s  .  co  m*/
 * TODO: unify with setUpChild.
 * @param child The child.
 */
private void measureItem(View child) {
    ViewGroup.LayoutParams p = child.getLayoutParams();
    if (p == null) {
        p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    int childHeightSpec = ViewGroup.getChildMeasureSpec(mHeightMeasureSpec,
            mListPadding.top + mListPadding.bottom, p.height);
    int lpWidth = p.width;
    int childWidthSpec;
    if (lpWidth > 0) {
        childWidthSpec = MeasureSpec.makeMeasureSpec(lpWidth, MeasureSpec.EXACTLY);
    } else {
        childWidthSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    }
    child.measure(childWidthSpec, childHeightSpec);
}

From source file:com.awrtechnologies.carbudgetsales.hlistview.widget.HListView.java

/**
 * Measure a particular list child. TODO: unify with setUpChild.
 * //from   w w  w .ja  v  a 2s  .  c  o m
 * @param child
 *           The child.
 */
private void measureItem(View child) {
    ViewGroup.LayoutParams p = child.getLayoutParams();
    if (p == null) {
        p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
    }

    int childHeightSpec = ViewGroup.getChildMeasureSpec(mHeightMeasureSpec,
            mListPadding.top + mListPadding.bottom, p.height);
    int lpWidth = p.width;
    int childWidthSpec;
    if (lpWidth > 0) {
        childWidthSpec = MeasureSpec.makeMeasureSpec(lpWidth, MeasureSpec.EXACTLY);
    } else {
        childWidthSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    }
    child.measure(childWidthSpec, childHeightSpec);
}

From source file:com.appunite.list.ListView.java

/**
 * Measure a particular list child./*from w w w  . ja v a 2 s  .  c om*/
 * TODO: unify with setUpChild.
 * @param child The child.
 */
private void measureItem(View child) {
    ViewGroup.LayoutParams p = child.getLayoutParams();
    if (p == null) {
        p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    int childWidthSpec = ViewGroup.getChildMeasureSpec(mWidthMeasureSpec,
            mListPadding.left + mListPadding.right, p.width);
    int lpHeight = p.height;
    int childHeightSpec;
    if (lpHeight > 0) {
        childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);
    } else {
        childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    }
    child.measure(childWidthSpec, childHeightSpec);
}

From source file:com.awrtechnologies.valor.valorfireplace.hlistview.widget.HListView.java

/**
 * Measure a particular list child. TODO: unify with setUpChild.
 *
 * @param child// w w w .j  a v  a 2s  .  co  m
 *           The child.
 */
private void measureItem(View child) {
    ViewGroup.LayoutParams p = child.getLayoutParams();
    if (p == null) {
        p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
    }

    int childHeightSpec = ViewGroup.getChildMeasureSpec(mHeightMeasureSpec,
            mListPadding.top + mListPadding.bottom, p.height);
    int lpWidth = p.width;
    int childWidthSpec;
    if (lpWidth > 0) {
        childWidthSpec = View.MeasureSpec.makeMeasureSpec(lpWidth, View.MeasureSpec.EXACTLY);
    } else {
        childWidthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    }
    child.measure(childWidthSpec, childHeightSpec);
}

From source file:com.ad.view.staggeredgridview.StaggeredGridView.java

/**
 * Measure and layout all currently visible children.
 * //ww  w.  j a  va 2 s  .  c o m
 * @param queryAdapter
 *            true to requery the adapter for view data
 */
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;
    mColWidth = colWidth;
    int rebuildLayoutRecordsBefore = -1;
    int rebuildLayoutRecordsAfter = -1;

    Arrays.fill(mItemBottoms, Integer.MIN_VALUE);

    final int childCount = getChildCount();
    int amountRemoved = 0;

    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 == null) {
                // child has been removed
                removeViewAt(i);
                if (i - 1 >= 0)
                    invalidateLayoutRecordsAfterPosition(i - 1);
                amountRemoved++;
                continue;
            } else 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 - amountRemoved); 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);
        }
    }

    if (this.mSelectorPosition != INVALID_POSITION) {
        View child = getChildAt(mMotionPosition - mFirstPosition);
        if (child != null)
            positionSelector(mMotionPosition, child);
    } else if (mTouchMode > TOUCH_MODE_DOWN) {
        View child = getChildAt(mMotionPosition - mFirstPosition);
        if (child != null)
            positionSelector(mMotionPosition, child);
    } else {
        mSelectorRect.setEmpty();
    }
}

From source file:com.appunite.list.HorizontalListView.java

private void measureScrapChild(View child, int position, int heightMeasureSpec) {
    LayoutParams p = (LayoutParams) child.getLayoutParams();
    if (p == null) {
        p = (LayoutParams) generateDefaultLayoutParams();
        child.setLayoutParams(p);/*from www . j av  a  2s. c  o  m*/
    }
    p.viewType = mAdapter.getItemViewType(position);
    p.forceAdd = true;

    int childHeightSpec = ViewGroup.getChildMeasureSpec(heightMeasureSpec,
            mListPadding.top + mListPadding.bottom, p.height);
    int lpWidth = p.width;
    int childWidthSpec;
    if (lpWidth > 0) {
        childWidthSpec = MeasureSpec.makeMeasureSpec(lpWidth, MeasureSpec.EXACTLY);
    } else {
        childWidthSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    }
    child.measure(childWidthSpec, childHeightSpec);
}

From source file:com.google.android.flexbox.FlexboxLayout.java

/**
 * Sub method for {@link #onMeasure(int, int)} when the main axis direction is vertical
 * (either from top to bottom or bottom to top).
 *
 * @param widthMeasureSpec  horizontal space requirements as imposed by the parent
 * @param heightMeasureSpec vertical space requirements as imposed by the parent
 * @see #onMeasure(int, int)/*from  ww  w  .  j a  v  a  2  s  . c o  m*/
 * @see #setFlexDirection(int)
 * @see #setFlexWrap(int)
 * @see #setAlignItems(int)
 * @see #setAlignContent(int)
 */
private void measureVertical(int widthMeasureSpec, int heightMeasureSpec) {
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);
    int childState = 0;

    mFlexLines.clear();

    // Determine how many flex lines are needed in this layout by measuring each child.
    // (Expand or shrink the view depending on the flexGrow and flexShrink attributes in a later
    // loop)
    int childCount = getChildCount();
    int paddingTop = getPaddingTop();
    int paddingBottom = getPaddingBottom();
    int largestWidthInColumn = Integer.MIN_VALUE;
    FlexLine flexLine = new FlexLine();
    flexLine.mMainSize = paddingTop + paddingBottom;
    // The index of the view in a same flex line.
    int indexInFlexLine = 0;
    for (int i = 0; i < childCount; i++) {
        View child = getReorderedChildAt(i);
        if (child == null) {
            addFlexLineIfLastFlexItem(i, childCount, flexLine);
            continue;
        } else if (child.getVisibility() == View.GONE) {
            flexLine.mItemCount++;
            addFlexLineIfLastFlexItem(i, childCount, flexLine);
            continue;
        }

        FlexboxLayout.LayoutParams lp = (LayoutParams) child.getLayoutParams();
        if (lp.alignSelf == LayoutParams.ALIGN_SELF_STRETCH) {
            flexLine.mIndicesAlignSelfStretch.add(i);
        }

        int childHeight = lp.height;
        if (lp.flexBasisPercent != LayoutParams.FLEX_BASIS_PERCENT_DEFAULT
                && heightMode == MeasureSpec.EXACTLY) {
            childHeight = Math.round(heightSize * lp.flexBasisPercent);
            // Use the dimension from the layout_height attribute if the heightMode is not
            // MeasureSpec.EXACTLY even if any fraction value is set to layout_flexBasisPercent.
            // There are likely quite few use cases where assigning any fraction values
            // with heightMode is not MeasureSpec.EXACTLY (e.g. FlexboxLayout's layout_height
            // is set to wrap_content)
        }

        int childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec,
                getPaddingLeft() + getPaddingRight() + lp.leftMargin + lp.rightMargin, lp.width);
        int childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec,
                getPaddingTop() + getPaddingBottom() + lp.topMargin + lp.bottomMargin, childHeight);
        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);

        // Check the size constraint after the first measurement for the child
        // To prevent the child's width/height violate the size constraints imposed by the
        // {@link LayoutParams#minWidth}, {@link LayoutParams#minHeight},
        // {@link LayoutParams#maxWidth} and {@link LayoutParams#maxHeight} attributes.
        // E.g. When the child's layout_height is wrap_content the measured height may be
        // less than the min height after the first measurement.
        checkSizeConstraints(child);

        childState = ViewCompat.combineMeasuredStates(childState, ViewCompat.getMeasuredState(child));
        largestWidthInColumn = Math.max(largestWidthInColumn,
                child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin);

        if (isWrapRequired(heightMode, heightSize, flexLine.mMainSize,
                child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin, lp, i, indexInFlexLine)) {
            if (flexLine.mItemCount > 0) {
                addFlexLine(flexLine);
            }

            flexLine = new FlexLine();
            flexLine.mItemCount = 1;
            flexLine.mMainSize = paddingTop + paddingBottom;
            largestWidthInColumn = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
            indexInFlexLine = 0;
        } else {
            flexLine.mItemCount++;
            indexInFlexLine++;
        }
        flexLine.mMainSize += child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
        flexLine.mTotalFlexGrow += lp.flexGrow;
        flexLine.mTotalFlexShrink += lp.flexShrink;
        // Temporarily set the cross axis length as the largest child width in the column
        // Expand along the cross axis depending on the mAlignContent property if needed
        // later
        flexLine.mCrossSize = Math.max(flexLine.mCrossSize, largestWidthInColumn);

        if (hasDividerBeforeChildAtAlongMainAxis(i, indexInFlexLine)) {
            flexLine.mMainSize += mDividerHorizontalHeight;
        }
        addFlexLineIfLastFlexItem(i, childCount, flexLine);
    }

    determineMainSize(mFlexDirection, widthMeasureSpec, heightMeasureSpec);
    determineCrossSize(mFlexDirection, widthMeasureSpec, heightMeasureSpec,
            getPaddingLeft() + getPaddingRight());
    // Now cross size for each flex line is determined.
    // Expand the views if alignItems (or alignSelf in each child view) is set to stretch
    stretchViews(mFlexDirection, mAlignItems);
    setMeasuredDimensionForFlex(mFlexDirection, widthMeasureSpec, heightMeasureSpec, childState);
}

From source file:chan.android.app.bitwise.util.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  v  a 2  s . com
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;
    mColWidth = colWidth;
    int rebuildLayoutRecordsBefore = -1;
    int rebuildLayoutRecordsAfter = -1;

    Arrays.fill(mItemBottoms, Integer.MIN_VALUE);

    final int childCount = getChildCount();
    int amountRemoved = 0;

    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 == null) {
                // child has been removed
                removeViewAt(i);
                if (i - 1 >= 0)
                    invalidateLayoutRecordsAfterPosition(i - 1);
                amountRemoved++;
                continue;
            } else 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;

            //                final int colEnd = Math.min(mColCount, col + lp.span);
            for (int j = 0; j < mColCount; 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);

        final int colEnd = Math.min(mColCount, col + lp.span);
        for (int j = col; j < colEnd; 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 - amountRemoved); 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);
        }
    }

    if (this.mSelectorPosition != INVALID_POSITION) {
        View child = getChildAt(mMotionPosition - mFirstPosition);
        if (child != null)
            positionSelector(mMotionPosition, child);
    } else if (mTouchMode > TOUCH_MODE_DOWN) {
        View child = getChildAt(mMotionPosition - mFirstPosition);
        if (child != null)
            positionSelector(mMotionPosition, child);
    } else {
        mSelectorRect.setEmpty();
    }
}

From source file:com.awrtechnologies.carbudgetsales.hlistview.widget.HListView.java

private void measureScrapChildWidth(View child, int position, int heightMeasureSpec) {
    LayoutParams p = (LayoutParams) child.getLayoutParams();
    if (p == null) {
        p = (AbsHListView.LayoutParams) generateDefaultLayoutParams();
        child.setLayoutParams(p);/*from  w  ww. j ava  2  s . c  o  m*/
    }
    p.viewType = mAdapter.getItemViewType(position);
    p.forceAdd = true;

    int childHeightSpec = ViewGroup.getChildMeasureSpec(heightMeasureSpec,
            mListPadding.top + mListPadding.bottom, p.height);
    int lpWidth = p.width;
    int childWidthSpec;
    if (lpWidth > 0) {
        childWidthSpec = MeasureSpec.makeMeasureSpec(lpWidth, MeasureSpec.EXACTLY);
    } else {
        childWidthSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    }
    child.measure(childWidthSpec, childHeightSpec);
}

From source file:com.appunite.list.ListView.java

private void measureScrapChild(View child, int position, int widthMeasureSpec) {
    LayoutParams p = (LayoutParams) child.getLayoutParams();
    if (p == null) {
        p = (AbsListView.LayoutParams) generateDefaultLayoutParams();
        child.setLayoutParams(p);//from w ww  .jav a 2  s.  c o  m
    }
    p.viewType = mAdapter.getItemViewType(position);
    p.forceAdd = true;

    int childWidthSpec = ViewGroup.getChildMeasureSpec(widthMeasureSpec, mListPadding.left + mListPadding.right,
            p.width);
    int lpHeight = p.height;
    int childHeightSpec;
    if (lpHeight > 0) {
        childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);
    } else {
        childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    }
    child.measure(childWidthSpec, childHeightSpec);
}