@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int heightSize = View.MeasureSpec.getSize(heightMeasureSpec); int heightMode = View.MeasureSpec.getMode(heightMeasureSpec); int tmpHeightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, heightMode); int widthSize = View.MeasureSpec.getSize(widthMeasureSpec); int widthMode = View.MeasureSpec.getMode(widthMeasureSpec); int tmpWidthMeasureSpec = MeasureSpec.makeMeasureSpec(widthSize, widthMode); // fixed scrollview height problems if (heightMode == MeasureSpec.UNSPECIFIED && getParent() != null && (getParent() instanceof ScrollView)) { int baseHeight = 0; Context context = getContext(); if (context instanceof Activity) { Activity act = (Activity) context; int measuredHeight = act.findViewById(android.R.id.content).getMeasuredHeight(); baseHeight = measuredHeight; } else { baseHeight = getScreenHeight(); } tmpHeightMeasureSpec = MeasureSpec.makeMeasureSpec(baseHeight, heightMode); } mPercentLayoutHelper.adjustChildren(tmpWidthMeasureSpec, tmpHeightMeasureSpec); super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (mPercentLayoutHelper.handleMeasuredStateTooSmall()) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure( MeasureSpec.makeMeasureSpec( AndroidUtilities.dp(76) + getPaddingLeft() + getPaddingRight(), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(78), MeasureSpec.EXACTLY)); }
public void setAdapter(TaskDescriptionAdapter adapter) { Log.d(TAG, "adapter.getCount() = " + adapter.getCount()); mAdapter = adapter; mAdapter.registerDataSetObserver( new DataSetObserver() { public void onChanged() { update(); } public void onInvalidated() { update(); } }); DisplayMetrics dm = getResources().getDisplayMetrics(); int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(dm.widthPixels, MeasureSpec.AT_MOST); int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(dm.heightPixels, MeasureSpec.AT_MOST); View child = mAdapter.createView(mLinearLayout); child.measure(childWidthMeasureSpec, childheightMeasureSpec); mNumItemsInOneScreenful = (int) FloatMath.ceil(dm.widthPixels / (float) child.getMeasuredWidth()); addToRecycledViews(child); mCellWidth = child.getMeasuredWidth(); for (int i = 0; i < mNumItemsInOneScreenful - 1; i++) { addToRecycledViews(mAdapter.createView(mLinearLayout)); } }
/** * Calculates control width and creates text layouts * * @param widthSize the input layout width * @param mode the layout mode * @return the calculated control width */ private int calculateLayoutWidth(int widthSize, int mode) { initResourcesIfNecessary(); // TODO: make it static itemsLayout.setLayoutParams( new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); itemsLayout.measure( MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); int width = itemsLayout.getMeasuredWidth(); if (mode == MeasureSpec.EXACTLY) { width = widthSize; } else { width += 2 * PADDING; // Check against our minimum width width = Math.max(width, getSuggestedMinimumWidth()); if (mode == MeasureSpec.AT_MOST && widthSize < width) { width = widthSize; } } itemsLayout.measure( MeasureSpec.makeMeasureSpec(width - 2 * PADDING, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); return width; }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (videoAspectRatio == 0) { // Aspect ratio not set. return; } int width = getMeasuredWidth(); int height = getMeasuredHeight(); float viewAspectRatio = (float) width / height; float aspectDeformation = videoAspectRatio / viewAspectRatio - 1; if (Math.abs(aspectDeformation) <= MAX_ASPECT_RATIO_DEFORMATION_FRACTION) { // We're within the allowed tolerance. return; } if (aspectDeformation > 0) { height = (int) (width / videoAspectRatio); } else { width = (int) (height * videoAspectRatio); } super.onMeasure( MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)); }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // 父容器传过来的宽度方向上的模式 int widthMode = MeasureSpec.getMode(widthMeasureSpec); // 父容器传过来的高度方向上的模式 int heightMode = MeasureSpec.getMode(heightMeasureSpec); // 父容器传过来的宽度的值 int width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight(); // 父容器传过来的高度的值 int height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingLeft() - getPaddingRight(); if (widthMode == MeasureSpec.EXACTLY && heightMode != MeasureSpec.EXACTLY && ratio != 0.0f) { // 判断条件为,宽度模式为Exactly,也就是填充父窗体或者是指定宽度; // 且高度模式不是Exaclty,代表设置的既不是fill_parent也不是具体的值,于是需要具体测量 // 且图片的宽高比已经赋值完毕,不再是0.0f // 表示宽度确定,要测量高度 height = (int) (width / ratio + 0.5f); heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); } else if (widthMode != MeasureSpec.EXACTLY && heightMode == MeasureSpec.EXACTLY && ratio != 0.0f) { // 判断条件跟上面的相反,宽度方向和高度方向的条件互换 // 表示高度确定,要测量宽度 width = (int) (height * ratio + 0.5f); widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY); } super.onMeasure(widthMeasureSpec, heightMeasureSpec); }
public void Layout(int top, int left) { // 每行剩余的宽度. int FreeWidth = MaxWidth - UsedWidth; // 多出来的宽度均分给每个控件 int avgWidth = (int) (FreeWidth / mView.size() + 0.5f); for (int i = 0; i < mView.size(); i++) { View view = mView.get(i); int vMeasuredWidth = view.getMeasuredWidth(); int vMeasuredHeight = view.getMeasuredHeight(); int mHeight = (Height - vMeasuredHeight) / 2; // 有大有小时,让小控件居中显示 if (avgWidth > 0) { // 重新测量 view.measure( MeasureSpec.makeMeasureSpec(vMeasuredWidth + avgWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(vMeasuredHeight, MeasureSpec.EXACTLY)); } vMeasuredWidth = view.getMeasuredWidth(); vMeasuredHeight = view.getMeasuredHeight(); int vTop = top + mHeight; int vBottom = top + vMeasuredHeight + mHeight; int vLeft = left; int vRight = left + vMeasuredWidth; left += vMeasuredWidth + space; view.layout(vLeft, vTop, vRight, vBottom); } }
/** * Overriding onMeasure to handle the case where WRAP_CONTENT might be specified in the layout. * Since we don't know the dimensions of the profile photo, we need to handle this case * specifically. * * <p>The approach is to default to a NORMAL sized amount of space in the case that a preset size * is not specified. This logic is applied to both width and height */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { ViewGroup.LayoutParams params = getLayoutParams(); boolean customMeasure = false; int newHeight = MeasureSpec.getSize(heightMeasureSpec); int newWidth = MeasureSpec.getSize(widthMeasureSpec); if (MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY && params.height == ViewGroup.LayoutParams.WRAP_CONTENT) { newHeight = getPresetSizeInPixels(true); // Default to a preset size heightMeasureSpec = MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY); customMeasure = true; } if (MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY && params.width == ViewGroup.LayoutParams.WRAP_CONTENT) { newWidth = getPresetSizeInPixels(true); // Default to a preset size widthMeasureSpec = MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY); customMeasure = true; } if (customMeasure) { // Since we are providing custom dimensions, we need to handle the measure // phase from here setMeasuredDimension(newWidth, newHeight); measureChildren(widthMeasureSpec, heightMeasureSpec); } else { // Rely on FrameLayout to do the right thing super.onMeasure(widthMeasureSpec, heightMeasureSpec); } }
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); int prefWidth = this.tnContainer.getPreferredWidth(); int prefHeight = this.tnContainer.getPreferredHeight(); if (prefWidth > 0 && (widthMode == MeasureSpec.UNSPECIFIED || widthMode == MeasureSpec.AT_MOST || widthMode == MeasureSpec.EXACTLY)) { if (widthSize > 0 && prefWidth > widthSize) prefWidth = widthSize; widthMeasureSpec = MeasureSpec.makeMeasureSpec(prefWidth, MeasureSpec.EXACTLY); } if (prefHeight > 0 && (heightMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.EXACTLY)) { if (heightSize > 0 && prefHeight > heightSize) prefHeight = heightSize; heightMeasureSpec = MeasureSpec.makeMeasureSpec(prefHeight, MeasureSpec.EXACTLY); } super.onMeasure(widthMeasureSpec, heightMeasureSpec); }
// 测量当前布局,修改测量规则 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // 获取宽度模式 int widthMode = MeasureSpec.getMode(widthMeasureSpec); // 得到宽度 int widthSize = MeasureSpec.getSize(widthMeasureSpec); // 去除左右内边距,得到真实的宽度 widthSize = widthSize - this.getPaddingLeft() - this.getPaddingRight(); // 获取宽度模式 int heightMode = MeasureSpec.getMode(heightMeasureSpec); // 得到高度 int heightSize = MeasureSpec.getSize(heightMeasureSpec); // 去除上下内边距,得到真实的高度 heightSize = heightSize - this.getPaddingTop() - this.getPaddingBottom(); // 哪个是精确值,就以哪个为基准进行按比例设置 if (widthMode == MeasureSpec.EXACTLY && heightMode != MeasureSpec.EXACTLY) { // 对高度进行根据宽度的比例设置,+0.5是为了四舍五入 heightSize = (int) (widthSize / ratio + 0.5); } else if (widthMode != MeasureSpec.EXACTLY && heightMode == MeasureSpec.EXACTLY) { // 由于高度是精确的值 ,宽度随着高度的变化而变化 widthSize = (int) ((heightSize * ratio) + 0.5f); } // 重新指定测量规则 widthMeasureSpec = MeasureSpec.makeMeasureSpec( widthSize + this.getPaddingLeft() + this.getPaddingRight(), MeasureSpec.EXACTLY); heightMeasureSpec = MeasureSpec.makeMeasureSpec( heightSize + this.getPaddingTop() + this.getPaddingBottom(), MeasureSpec.EXACTLY); super.onMeasure(widthMeasureSpec, heightMeasureSpec); }
private void addSectionHeader(int actualSection) { View previousHeader = mHeader.getChildAt(0); if (previousHeader != null) { mHeader.removeViewAt(0); } if (mAdapter.hasSectionHeaderView(actualSection)) { mHeaderConvertView = mAdapter.getSectionHeaderView(actualSection, mHeaderConvertView, mHeader); mHeaderConvertView.setLayoutParams( new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); mHeaderConvertView.measure( MeasureSpec.makeMeasureSpec(mHeader.getWidth(), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); mHeader.getLayoutParams().height = mHeaderConvertView.getMeasuredHeight(); mHeaderConvertView.scrollTo(0, 0); mHeader.scrollTo(0, 0); mHeader.addView(mHeaderConvertView, 0); } else { mHeader.getLayoutParams().height = 0; mHeader.scrollTo(0, 0); } mScrollView.bringToFront(); }
@Override protected void onMeasureChild(final View child, final LayoutParams layoutParams) { final int viewType = layoutParams.viewType; final int position = layoutParams.position; if (viewType == ITEM_VIEW_TYPE_HEADER_OR_FOOTER || viewType == ITEM_VIEW_TYPE_IGNORE) { // for headers and weird ignored views super.onMeasureChild(child, layoutParams); } else { if (DBG) Log.d(TAG, "onMeasureChild BEFORE position:" + position + " h:" + getMeasuredHeight()); // measure it to the width of our column. int childWidthSpec = MeasureSpec.makeMeasureSpec(mColumnWidth, MeasureSpec.EXACTLY); int childHeightSpec; if (layoutParams.height > 0) { childHeightSpec = MeasureSpec.makeMeasureSpec(layoutParams.height, MeasureSpec.EXACTLY); } else { childHeightSpec = MeasureSpec.makeMeasureSpec(LayoutParams.WRAP_CONTENT, MeasureSpec.UNSPECIFIED); } child.measure(childWidthSpec, childHeightSpec); } final int childHeight = getChildHeight(child); setPositionHeightRatio(position, childHeight); if (DBG) Log.d(TAG, "onMeasureChild AFTER position:" + position + " h:" + childHeight); }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); mMenuView.measure( MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY)); }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { float textSize = getTextSize(); float textLen = getText().length(); mRadius = Math.round(2 * textSize); mTickLen = Math.round(mRadius / 180 * 3.14 * mSweepAngMin); mMaxRippleWidth = mRadius; mLineWidth = Math.round(mRadius / 10); mRippleWidth = mLineWidth; mInitRippleWidth = mRippleWidth; mRecWidth = textLen * textSize; float mWidth = mRecWidth + 2 * (mRadius + mLineWidth + mMaxRippleWidth); float mHeight = 2 * (mRadius + mLineWidth + mMaxRippleWidth); mLineRadius = mRadius + mLineWidth; cxLeft = mLineRadius + mMaxRippleWidth; cyLeft = mLineRadius + mMaxRippleWidth; cxRight = cxLeft + mRecWidth; cyRight = cyLeft; mLinePaint.setStrokeWidth(mLineWidth); mLinePaint.setStrokeCap(Paint.Cap.ROUND); mTickPaint.setStrokeWidth(mLineWidth); mTickPaint.setStrokeCap(Paint.Cap.ROUND); int width = MeasureSpec.makeMeasureSpec((int) mWidth, MeasureSpec.getMode(widthMeasureSpec)); int height = MeasureSpec.makeMeasureSpec((int) mHeight, MeasureSpec.getMode(heightMeasureSpec)); super.onMeasure(width, height); }
/** {@inheritDoc} */ @Override protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) { final int specWidthSize = MeasureSpec.getSize(widthMeasureSpec); final int specWidthMode = MeasureSpec.getMode(widthMeasureSpec); final int specHeightSize = MeasureSpec.getSize(heightMeasureSpec); final int specHeightMode = MeasureSpec.getMode(heightMeasureSpec); // We expect to be somewhere inside a MaterialCalendarView, which should measure EXACTLY if (specHeightMode == MeasureSpec.UNSPECIFIED || specWidthMode == MeasureSpec.UNSPECIFIED) { throw new IllegalStateException("MonthView should never be left to decide it's size"); } // The spec width should be a correct multiple final int measureTileSize = specWidthSize / DEFAULT_DAYS_IN_WEEK; // Just use the spec sizes setMeasuredDimension(specWidthSize, specHeightSize); int count = getChildCount(); for (int i = 0; i < count; i++) { final View child = getChildAt(i); int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(measureTileSize, MeasureSpec.EXACTLY); int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(measureTileSize, MeasureSpec.EXACTLY); child.measure(childWidthMeasureSpec, childHeightMeasureSpec); } }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int measuredWidth = 0; int measuredHeight = 0; if (mUIElement != null) { final int paddingLeft = getPaddingLeft(); final int paddingTop = getPaddingTop(); final int paddingRight = getPaddingRight(); final int paddingBottom = getPaddingBottom(); final int viewWidthSize = MeasureSpec.getSize(widthMeasureSpec); final int viewWidthMode = MeasureSpec.getMode(widthMeasureSpec); final int viewHeightSize = MeasureSpec.getSize(heightMeasureSpec); final int viewHeightMode = MeasureSpec.getMode(heightMeasureSpec); final int elementWidth = viewWidthSize - paddingLeft - paddingRight; final int elementWidthSpec = MeasureSpec.makeMeasureSpec(elementWidth, viewWidthMode); final int elementHeight = viewHeightSize - paddingTop - paddingBottom; final int elementHeightSpec = MeasureSpec.makeMeasureSpec(elementHeight, viewHeightMode); mUIElement.measure(elementWidthSpec, elementHeightSpec); measuredWidth = mUIElement.getMeasuredWidth() + paddingLeft + paddingRight; measuredHeight = mUIElement.getMeasuredHeight() + paddingTop + paddingBottom; } measuredWidth = Math.max(measuredWidth, getSuggestedMinimumWidth()); measuredHeight = Math.max(measuredHeight, getSuggestedMinimumHeight()); setMeasuredDimension(measuredWidth, measuredHeight); }
private void measureFloating(View preview, Rect margins, Rect out) { final int marginLeft; final int marginTop; final int marginRight; if (margins == null) { marginLeft = 0; marginTop = 0; marginRight = 0; } else { marginLeft = margins.left; marginTop = margins.top; marginRight = margins.right; } final Rect container = mContainerRect; final int containerWidth = container.width(); final int adjMaxWidth = containerWidth - marginLeft - marginRight; final int widthMeasureSpec = MeasureSpec.makeMeasureSpec(adjMaxWidth, MeasureSpec.AT_MOST); final int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); preview.measure(widthMeasureSpec, heightMeasureSpec); // Align at the vertical center, mToastOffset away from this View. final int containerHeight = container.height(); final int width = preview.getMinimumWidth(); final int top = (containerHeight - width) / 2 + container.top; final int bottom = top + preview.getMeasuredHeight(); final int left = containerWidth - mViewWidth - mToastOffset - width + container.left; final int right = left + width; out.set(left, top, right, bottom); }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) { throw new RuntimeException("View cannot have UNSPECIFIED dimensions"); } mMainView.measure(widthMeasureSpec, heightMeasureSpec); int leftViewWidth = (int) (widthSpecSize - mLeftViewRightPadding); mLeftView.measure( MeasureSpec.makeMeasureSpec(leftViewWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(heightSpecSize, MeasureSpec.EXACTLY)); int rightViewWidth = (int) (widthSpecSize - mRightViewLeftPadding); mRightView.measure( MeasureSpec.makeMeasureSpec(rightViewWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(heightSpecSize, MeasureSpec.EXACTLY)); setMeasuredDimension(widthSpecSize, heightSpecSize); }
/** * Measure a child view to fit within cell-based formatting. The child's width will be measured to * a whole multiple of cellSize. * * <p> * * <p>Sets the expandable and cellsUsed fields of LayoutParams. * * @param child Child to measure * @param cellSize Size of one cell * @param cellsRemaining Number of cells remaining that this view can expand to fill * @param parentHeightMeasureSpec MeasureSpec used by the parent view * @param parentHeightPadding Padding present in the parent view * @return Number of cells this child was measured to occupy */ static int measureChildForCells( View child, int cellSize, int cellsRemaining, int parentHeightMeasureSpec, int parentHeightPadding) { final LayoutParams lp = (LayoutParams) child.getLayoutParams(); final int childHeightSize = MeasureSpec.getSize(parentHeightMeasureSpec) - parentHeightPadding; final int childHeightMode = MeasureSpec.getMode(parentHeightMeasureSpec); final int childHeightSpec = MeasureSpec.makeMeasureSpec(childHeightSize, childHeightMode); int cellsUsed = 0; if (cellsRemaining > 0) { final int childWidthSpec = MeasureSpec.makeMeasureSpec(cellSize * cellsRemaining, MeasureSpec.AT_MOST); child.measure(childWidthSpec, childHeightSpec); final int measuredWidth = child.getMeasuredWidth(); cellsUsed = measuredWidth / cellSize; if (measuredWidth % cellSize != 0) cellsUsed++; } final ActionMenuItemView itemView = child instanceof ActionMenuItemView ? (ActionMenuItemView) child : null; final boolean expandable = !lp.isOverflowButton && itemView != null && itemView.hasText(); lp.expandable = expandable; lp.cellsUsed = cellsUsed; final int targetWidth = cellsUsed * cellSize; child.measure(MeasureSpec.makeMeasureSpec(targetWidth, MeasureSpec.EXACTLY), childHeightSpec); return cellsUsed; }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { final View c = getChildAt(0); if (c != null) { final TextView tab = (TextView) c; LayoutParams layoutParams = (LayoutParams) tab.getLayoutParams(); final int widthSpec = MeasureSpec.makeMeasureSpec(layoutParams.width, MeasureSpec.EXACTLY); final int heightSpec = MeasureSpec.makeMeasureSpec(layoutParams.height, MeasureSpec.EXACTLY); tab.measure(widthSpec, heightSpec); setMeasuredDimension( resolveSize(0, widthMeasureSpec), resolveSize( tab.getMeasuredHeight() + this.getPaddingTop() + this.getPaddingBottom(), heightMeasureSpec)); } else { setMeasuredDimension( resolveSize(0, widthMeasureSpec), resolveSize(this.getPaddingTop() + this.getPaddingBottom(), heightMeasureSpec)); } // first time measuring, set outside offset (if not set manually), // measure children and calculate initial positions if (isFirstMeasurement) { isFirstMeasurement = false; if (mOutsideOffset < 0) mOutsideOffset = getMeasuredWidth(); measureChildren(); calculateNewPositions(true); } }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if (IMPL instanceof CardViewApi21 == false) { final int widthMode = MeasureSpec.getMode(widthMeasureSpec); switch (widthMode) { case MeasureSpec.EXACTLY: case MeasureSpec.AT_MOST: final int minWidth = (int) Math.ceil(IMPL.getMinWidth(this)); widthMeasureSpec = MeasureSpec.makeMeasureSpec( Math.max(minWidth, MeasureSpec.getSize(widthMeasureSpec)), widthMode); break; } final int heightMode = MeasureSpec.getMode(heightMeasureSpec); switch (heightMode) { case MeasureSpec.EXACTLY: case MeasureSpec.AT_MOST: final int minHeight = (int) Math.ceil(IMPL.getMinHeight(this)); heightMeasureSpec = MeasureSpec.makeMeasureSpec( Math.max(minHeight, MeasureSpec.getSize(heightMeasureSpec)), heightMode); break; } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } else { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) { throw new RuntimeException("SlidingDrawer cannot have UNSPECIFIED dimensions"); } final View handle = mHandle; measureChild(handle, widthMeasureSpec, heightMeasureSpec); if (mVertical) { int height = heightSpecSize - handle.getMeasuredHeight() - mTopOffset; mContent.measure( MeasureSpec.makeMeasureSpec(widthSpecSize, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)); } else { int width = widthSpecSize - handle.getMeasuredWidth() - mTopOffset; mContent.measure( MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(heightSpecSize, MeasureSpec.EXACTLY)); } setMeasuredDimension(widthSpecSize, heightSpecSize); }
/** * Calculates control height and creates text layouts * * @param heightSize the input layout height * @param mode the layout mode * @return the calculated control height */ private int calculateLayoutHeight(int heightSize, int mode) { mItemsLayout.setLayoutParams( new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); mItemsLayout.measure( MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.UNSPECIFIED)); int height = mItemsLayout.getMeasuredHeight(); if (mode == MeasureSpec.EXACTLY) { height = heightSize; } else { height += 2 * mItemsPadding; // Check against our minimum width height = Math.max(height, getSuggestedMinimumHeight()); if (mode == MeasureSpec.AT_MOST && heightSize < height) { height = heightSize; } } // forcing recalculating mItemsLayout.measure( // MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(400, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height - 2 * mItemsPadding, MeasureSpec.EXACTLY)); return height; }
@Override protected void onMeasure(int widthSpec, int heightSpec) { int previewWidth = MeasureSpec.getSize(widthSpec); int previewHeight = MeasureSpec.getSize(heightSpec); // Get the padding of the border background. int hPadding = mPaddingLeft + mPaddingRight; int vPadding = mPaddingTop + mPaddingBottom; // Resize the preview frame with correct aspect ratio. previewWidth -= hPadding; previewHeight -= vPadding; if (previewWidth > previewHeight * mAspectRatio) { previewWidth = (int) (previewHeight * mAspectRatio + .5); } else { previewHeight = (int) (previewWidth / mAspectRatio + .5); } // Add the padding of the border. previewWidth += hPadding; previewHeight += vPadding; // Ask children to follow the new preview dimension. super.onMeasure( MeasureSpec.makeMeasureSpec(previewWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(previewHeight, MeasureSpec.EXACTLY)); }
public int getRowHeight() { if (mRowHeight > 0) { return mRowHeight; } ListAdapter adapter = getAdapter(); int numColumns = getNumColumnsCompatible(); // adapter has not been set or has no views in it; if (adapter == null || adapter.getCount() <= numColumns * (mHeaderViewInfos.size() + mFooterViewInfos.size())) { return -1; } int mColumnWidth = getColumnWidthCompatible(); View view = getAdapter().getView(numColumns * mHeaderViewInfos.size(), mViewForMeasureRowHeight, this); AbsListView.LayoutParams p = (AbsListView.LayoutParams) view.getLayoutParams(); if (p == null) { p = new AbsListView.LayoutParams(-1, -2, 0); view.setLayoutParams(p); } int childHeightSpec = getChildMeasureSpec(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 0, p.height); int childWidthSpec = getChildMeasureSpec( MeasureSpec.makeMeasureSpec(mColumnWidth, MeasureSpec.EXACTLY), 0, p.width); view.measure(childWidthSpec, childHeightSpec); mViewForMeasureRowHeight = view; mRowHeight = view.getMeasuredHeight(); return mRowHeight; }
// 测量当前布局 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // widthMeasureSpec 宽度的规则 包含了两部分 模式 值 int widthMode = MeasureSpec.getMode(widthMeasureSpec); // 模式 int widthSize = MeasureSpec.getSize(widthMeasureSpec); // 宽度大小 int width = widthSize - getPaddingLeft() - getPaddingRight(); // 去掉左右两边的padding int heightMode = MeasureSpec.getMode(heightMeasureSpec); // 模式 int heightSize = MeasureSpec.getSize(heightMeasureSpec); // 高度大小 int height = heightSize - getPaddingTop() - getPaddingBottom(); // 去掉上下两边的padding if (widthMode == MeasureSpec.EXACTLY && heightMode != MeasureSpec.EXACTLY) { // 修正一下 高度的值 让高度=宽度/比例 height = (int) (width / ratio + 0.5f); // 保证4舍五入 } else if (widthMode != MeasureSpec.EXACTLY && heightMode == MeasureSpec.EXACTLY) { // 由于高度是精确的值 ,宽度随着高度的变化而变化 width = (int) ((height * ratio) + 0.5f); } // 重新制作了新的规则 widthMeasureSpec = MeasureSpec.makeMeasureSpec( MeasureSpec.EXACTLY, width + getPaddingLeft() + getPaddingRight()); heightMeasureSpec = MeasureSpec.makeMeasureSpec( MeasureSpec.EXACTLY, height + getPaddingTop() + getPaddingBottom()); super.onMeasure(widthMeasureSpec, heightMeasureSpec); }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) { throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions"); } final int count = getChildCount(); for (int i = 0; i < count; i++) { View child = getChildAt(i); PagedViewCellLayout.LayoutParams lp = (PagedViewCellLayout.LayoutParams) child.getLayoutParams(); lp.setup(mCellWidth, mCellHeight, mWidthGap, mHeightGap, getPaddingLeft(), getPaddingTop()); int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY); int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY); child.measure(childWidthMeasureSpec, childheightMeasureSpec); } setMeasuredDimension(widthSpecSize, heightSpecSize); }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthSie = MeasureSpec.getSize(widthMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); setMeasuredDimension(widthSie, heightSize); View bottomView = getChildAt(1); MarginLayoutParams lp = (MarginLayoutParams) bottomView.getLayoutParams(); int drawerWidthSpec = getChildMeasureSpec(widthMeasureSpec, lp.leftMargin + lp.rightMargin, lp.width); int drawerHeightSpec = getChildMeasureSpec(heightMeasureSpec, lp.bottomMargin + lp.topMargin, lp.height); bottomView.measure(drawerWidthSpec, drawerHeightSpec); View contentView = getChildAt(0); lp = (MarginLayoutParams) contentView.getLayoutParams(); int contentWidthSpec = MeasureSpec.makeMeasureSpec(widthSie - lp.leftMargin - lp.rightMargin, MeasureSpec.EXACTLY); int contentHeightSpec = MeasureSpec.makeMeasureSpec( heightSize - lp.topMargin - lp.bottomMargin, MeasureSpec.EXACTLY); contentView.measure(contentWidthSpec, contentHeightSpec); mBottomMenuView = bottomView; mContentView = contentView; }
public void measureChild(View child) { final LauncherAppState app = LauncherAppState.getInstance(); final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); final int cellWidth = mCellWidth; final int cellHeight = mCellHeight; CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); if (!lp.isFullscreen) { lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap, invertLayoutHorizontally(), mCountX); if (child instanceof LauncherAppWidgetHostView) { // Widgets have their own padding, so skip } else { // Otherwise, center the icon int cHeight = getCellContentHeight(); int cellPaddingY = (int) Math.max(0, ((lp.height - cHeight) / 2f)); int cellPaddingX = (int) (grid.edgeMarginPx / 2f); child.setPadding(cellPaddingX, cellPaddingY, cellPaddingX, 0); } } else { lp.x = 0; lp.y = 0; lp.width = getMeasuredWidth(); lp.height = getMeasuredHeight(); } int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY); int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY); child.measure(childWidthMeasureSpec, childheightMeasureSpec); }
@Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (mTarget == null) { ensureTarget(); } if (mTarget == null) { return; } mTarget.measure( MeasureSpec.makeMeasureSpec( getMeasuredWidth() - getPaddingLeft() - getPaddingRight(), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec( getMeasuredHeight() - getPaddingTop() - getPaddingBottom(), MeasureSpec.EXACTLY)); mCircleView.measure( MeasureSpec.makeMeasureSpec(mCircleWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(mCircleHeight, MeasureSpec.EXACTLY)); if (!mUsingCustomStart && !mOriginalOffsetCalculated) { mOriginalOffsetCalculated = true; mCurrentTargetOffsetTop = mOriginalOffsetTop = -mCircleView.getMeasuredHeight(); } mCircleViewIndex = -1; // Get the index of the circleview. for (int index = 0; index < getChildCount(); index++) { if (getChildAt(index) == mCircleView) { mCircleViewIndex = index; break; } } }