List of usage examples for android.view View measure
public final void measure(int widthMeasureSpec, int heightMeasureSpec)
This is called to find out how big a view should be.
From source file:com.android.launcher2.PagedView.java
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if (!mIsDataReady) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); return;//from w ww. ja v a 2 s . c o m } final int widthMode = MeasureSpec.getMode(widthMeasureSpec); final int widthSize = MeasureSpec.getSize(widthMeasureSpec); final int heightMode = MeasureSpec.getMode(heightMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); if (widthMode != MeasureSpec.EXACTLY) { throw new IllegalStateException("Workspace can only be used in EXACTLY mode."); } // Return early if we aren't given a proper dimension if (widthSize <= 0 || heightSize <= 0) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); return; } /* Allow the height to be set as WRAP_CONTENT. This allows the particular case * of the All apps view on XLarge displays to not take up more space then it needs. Width * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect * each page to have the same width. */ int maxChildHeight = 0; final int verticalPadding = getPaddingTop() + getPaddingBottom(); final int horizontalPadding = getPaddingLeft() + getPaddingRight(); // The children are given the same width and height as the workspace // unless they were set to WRAP_CONTENT if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize); final int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { // disallowing padding in paged view (just pass 0) final View child = getPageAt(i); final LayoutParams lp = (LayoutParams) child.getLayoutParams(); int childWidthMode; if (lp.width == LayoutParams.WRAP_CONTENT) { childWidthMode = MeasureSpec.AT_MOST; } else { childWidthMode = MeasureSpec.EXACTLY; } int childHeightMode; if (lp.height == LayoutParams.WRAP_CONTENT) { childHeightMode = MeasureSpec.AT_MOST; } else { childHeightMode = MeasureSpec.EXACTLY; } final int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(widthSize - horizontalPadding, childWidthMode); final int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode); child.measure(childWidthMeasureSpec, childHeightMeasureSpec); maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight()); if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", " + child.getMeasuredHeight()); } if (heightMode == MeasureSpec.AT_MOST) { heightSize = maxChildHeight + verticalPadding; } setMeasuredDimension(widthSize, heightSize); // We can't call getChildOffset/getRelativeChildOffset until we set the measured dimensions. // We also wait until we set the measured dimensions before flushing the cache as well, to // ensure that the cache is filled with good values. invalidateCachedOffsets(); if (childCount > 0) { if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", " + getChildWidth(0)); // Calculate the variable page spacing if necessary if (mPageSpacing == AUTOMATIC_PAGE_SPACING) { // The gap between pages in the PagedView should be equal to the gap from the page // to the edge of the screen (so it is not visible in the current screen). To // account for unequal padding on each side of the paged view, we take the maximum // of the left/right gap and use that as the gap between each page. int offset = getRelativeChildOffset(0); int spacing = Math.max(offset, widthSize - offset - getChildAt(0).getMeasuredWidth()); setPageSpacing(spacing); } } updateScrollingIndicatorPosition(); if (childCount > 0) { mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1); } else { mMaxScrollX = 0; } }
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 . ja v a2 s . c o m 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); } } }
From source file:com.cxsplay.wallyskim.widget.flexbox.FlexboxLayout.java
/** * Expand the view horizontally to the size of the crossSize (considering the view margins) * * @param view the View to be stretched * @param crossSize the cross size//from ww w. ja va2 s . co m */ private void stretchViewHorizontally(View view, int crossSize) { LayoutParams lp = (LayoutParams) view.getLayoutParams(); int newWidth = crossSize - lp.leftMargin - lp.rightMargin; newWidth = Math.max(newWidth, 0); view.measure(MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(view.getMeasuredHeight(), MeasureSpec.EXACTLY)); }
From source file:com.cxsplay.wallyskim.widget.flexbox.FlexboxLayout.java
/** * Expand the view vertically to the size of the crossSize (considering the view margins) * * @param view the View to be stretched * @param crossSize the cross size/* www . ja v a 2s . c om*/ */ private void stretchViewVertically(View view, int crossSize) { LayoutParams lp = (LayoutParams) view.getLayoutParams(); int newHeight = crossSize - lp.topMargin - lp.bottomMargin; newHeight = Math.max(newHeight, 0); view.measure(MeasureSpec.makeMeasureSpec(view.getMeasuredWidth(), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY)); }
From source file:com.appunite.list.GridView.java
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // Sets up mListPadding super.onMeasure(widthMeasureSpec, heightMeasureSpec); int widthMode = MeasureSpec.getMode(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); if (widthMode == MeasureSpec.UNSPECIFIED) { if (mColumnWidth > 0) { widthSize = mColumnWidth + mListPadding.left + mListPadding.right; } else {// w ww . j a v a 2s. c om widthSize = mListPadding.left + mListPadding.right; } widthSize += getVerticalScrollbarWidth(); } int childWidth = widthSize - mListPadding.left - mListPadding.right; boolean didNotInitiallyFit = determineColumns(childWidth); int childHeight = 0; int childState = 0; mItemCount = mAdapter == null ? 0 : mAdapter.getCount(); final int count = mItemCount; if (count > 0) { final View child = obtainView(0, mIsScrap); LayoutParams p = (LayoutParams) child.getLayoutParams(); if (p == null) { p = (LayoutParams) generateDefaultLayoutParams(); child.setLayoutParams(p); } p.viewType = mAdapter.getItemViewType(0); p.forceAdd = true; int childHeightSpec = getChildMeasureSpec(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 0, p.height); int childWidthSpec = getChildMeasureSpec(MeasureSpec.makeMeasureSpec(mColumnWidth, MeasureSpec.EXACTLY), 0, p.width); child.measure(childWidthSpec, childHeightSpec); childHeight = child.getMeasuredHeight(); childState = Compat.combineMeasuredStates(childState, Compat.getMeasuredState(child)); if (mRecycler.shouldRecycleViewType(p.viewType)) { mRecycler.addScrapView(child, -1); } } if (heightMode == MeasureSpec.UNSPECIFIED) { heightSize = mListPadding.top + mListPadding.bottom + childHeight + getVerticalFadingEdgeLength() * 2; } if (heightMode == MeasureSpec.AT_MOST) { int ourSize = mListPadding.top + mListPadding.bottom; final int numColumns = mNumColumns; for (int i = 0; i < count; i += numColumns) { ourSize += childHeight; if (i + numColumns < count) { ourSize += mVerticalSpacing; } if (ourSize >= heightSize) { ourSize = heightSize; break; } } heightSize = ourSize; } if (widthMode == MeasureSpec.AT_MOST && mRequestedNumColumns != AUTO_FIT) { int ourSize = (mRequestedNumColumns * mColumnWidth) + ((mRequestedNumColumns - 1) * mHorizontalSpacing) + mListPadding.left + mListPadding.right; if (ourSize > widthSize || didNotInitiallyFit) { widthSize |= MEASURED_STATE_TOO_SMALL; } } setMeasuredDimension(widthSize, heightSize); mWidthMeasureSpec = widthMeasureSpec; }
From source file:android.support.v7.widget.StaggeredGridLayoutManager2.java
private void measureChildWithDecorationsAndMargin(View child, int widthSpec, int heightSpec) { final Rect insets = mRecyclerView.getItemDecorInsetsForChild(child); LayoutParams lp = (LayoutParams) child.getLayoutParams(); widthSpec = updateSpecWithExtra(widthSpec, lp.leftMargin + insets.left, lp.rightMargin + insets.right); heightSpec = updateSpecWithExtra(heightSpec, lp.topMargin + insets.top, lp.bottomMargin + insets.bottom); child.measure(widthSpec, heightSpec); }
From source file:com.google.android.flexbox.FlexboxHelper.java
/** * Checks if the view's width/height don't violate the minimum/maximum size constraints imposed * by the {@link FlexItem#getMinWidth()}, {@link FlexItem#getMinHeight()}, * {@link FlexItem#getMaxWidth()} and {@link FlexItem#getMaxHeight()} attributes. * * @param view the view to be checked//from ww w. j a v a 2 s. co m * @param index index of the view */ private void checkSizeConstraints(View view, int index) { boolean needsMeasure = false; FlexItem flexItem = (FlexItem) view.getLayoutParams(); int childWidth = view.getMeasuredWidth(); int childHeight = view.getMeasuredHeight(); if (childWidth < flexItem.getMinWidth()) { needsMeasure = true; childWidth = flexItem.getMinWidth(); } else if (childWidth > flexItem.getMaxWidth()) { needsMeasure = true; childWidth = flexItem.getMaxWidth(); } if (childHeight < flexItem.getMinHeight()) { needsMeasure = true; childHeight = flexItem.getMinHeight(); } else if (childHeight > flexItem.getMaxHeight()) { needsMeasure = true; childHeight = flexItem.getMaxHeight(); } if (needsMeasure) { int widthSpec = View.MeasureSpec.makeMeasureSpec(childWidth, View.MeasureSpec.EXACTLY); int heightSpec = View.MeasureSpec.makeMeasureSpec(childHeight, View.MeasureSpec.EXACTLY); view.measure(widthSpec, heightSpec); updateMeasureCache(index, widthSpec, heightSpec, view); mFlexContainer.updateViewCache(index, view); } }
From source file:android.support.v7.widget.Toolbar.java
private void measureChildConstrained(View child, int parentWidthSpec, int widthUsed, int parentHeightSpec, int heightUsed, int heightConstraint) { final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); int childWidthSpec = getChildMeasureSpec(parentWidthSpec, getPaddingLeft() + getPaddingRight() + lp.leftMargin + lp.rightMargin + widthUsed, lp.width); int childHeightSpec = getChildMeasureSpec(parentHeightSpec, getPaddingTop() + getPaddingBottom() + lp.topMargin + lp.bottomMargin + heightUsed, lp.height); final int childHeightMode = MeasureSpec.getMode(childHeightSpec); if (childHeightMode != MeasureSpec.EXACTLY && heightConstraint >= 0) { final int size = childHeightMode != MeasureSpec.UNSPECIFIED ? Math.min(MeasureSpec.getSize(childHeightSpec), heightConstraint) : heightConstraint;/*ww w .j a v a2 s . c om*/ childHeightSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY); } child.measure(childWidthSpec, childHeightSpec); }
From source file:android.support.v7.widget.Toolbar.java
/** * Returns the width + uncollapsed margins *//*from w ww.java 2 s.c o m*/ private int measureChildCollapseMargins(View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed, int[] collapsingMargins) { final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); final int leftDiff = lp.leftMargin - collapsingMargins[0]; final int rightDiff = lp.rightMargin - collapsingMargins[1]; final int leftMargin = Math.max(0, leftDiff); final int rightMargin = Math.max(0, rightDiff); final int hMargins = leftMargin + rightMargin; collapsingMargins[0] = Math.max(0, -leftDiff); collapsingMargins[1] = Math.max(0, -rightDiff); final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec, getPaddingLeft() + getPaddingRight() + hMargins + widthUsed, lp.width); final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec, getPaddingTop() + getPaddingBottom() + lp.topMargin + lp.bottomMargin + heightUsed, lp.height); child.measure(childWidthMeasureSpec, childHeightMeasureSpec); return child.getMeasuredWidth() + hMargins; }
From source file:ca.co.rufus.androidboilerplate.ui.DebugDrawerLayout.java
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthMode = MeasureSpec.getMode(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); if (widthMode != MeasureSpec.EXACTLY || heightMode != MeasureSpec.EXACTLY) { if (isInEditMode()) { // Don't crash the layout editor. Consume all of the space if specified // or pick a magic number from thin air otherwise. // TODO Better communication with tools of this bogus state. // It will crash on a real device. if (widthMode == MeasureSpec.AT_MOST) { widthMode = MeasureSpec.EXACTLY; } else if (widthMode == MeasureSpec.UNSPECIFIED) { widthMode = MeasureSpec.EXACTLY; widthSize = 300;//from www. j av a 2 s . c o m } if (heightMode == MeasureSpec.AT_MOST) { heightMode = MeasureSpec.EXACTLY; } else if (heightMode == MeasureSpec.UNSPECIFIED) { heightMode = MeasureSpec.EXACTLY; heightSize = 300; } } else { throw new IllegalArgumentException("DrawerLayout must be measured with MeasureSpec.EXACTLY."); } } setMeasuredDimension(widthSize, heightSize); final boolean applyInsets = mLastInsets != null && ViewCompat.getFitsSystemWindows(this); final int layoutDirection = ViewCompat.getLayoutDirection(this); // Gravity value for each drawer we've seen. Only one of each permitted. int foundDrawers = 0; 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 (applyInsets) { final int cgrav = GravityCompat.getAbsoluteGravity(lp.gravity, layoutDirection); //if (ViewCompat.getFitsSystemWindows(child)) { IMPL.dispatchChildInsets(child, mLastInsets, cgrav); //} // IMPL.applyMarginInsets(lp, mLastInsets, cgrav); //} else { } if (isContentView(child)) { // Content views get measured at exactly the layout's size. final int contentWidthSpec = MeasureSpec.makeMeasureSpec(widthSize - lp.leftMargin - lp.rightMargin, MeasureSpec.EXACTLY); final int contentHeightSpec = MeasureSpec .makeMeasureSpec(heightSize - lp.topMargin - lp.bottomMargin, MeasureSpec.EXACTLY); child.measure(contentWidthSpec, contentHeightSpec); } else if (isDrawerView(child)) { final int childGravity = getDrawerViewAbsoluteGravity(child) & Gravity.HORIZONTAL_GRAVITY_MASK; if ((foundDrawers & childGravity) != 0) { throw new IllegalStateException( "Child drawer has absolute gravity " + gravityToString(childGravity) + " but this " + TAG + " already has a " + "drawer view along that edge"); } final int drawerWidthSpec = getChildMeasureSpec(widthMeasureSpec, mMinDrawerMargin + lp.leftMargin + lp.rightMargin, lp.width); final int drawerHeightSpec = getChildMeasureSpec(heightMeasureSpec, lp.topMargin + lp.bottomMargin, lp.height); child.measure(drawerWidthSpec, drawerHeightSpec); } else { throw new IllegalStateException("Child " + child + " at index " + i + " does not have a valid layout_gravity - must be Gravity.LEFT, " + "Gravity.RIGHT or Gravity.NO_GRAVITY"); } } }