下面列出了怎么用android.view.View.MeasureSpec的API类实例代码及写法,或者点击链接到github查看源代码。
public 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 == NTLMConstants.FLAG_NEGOTIATE_KEY_EXCHANGE || widthSpecMode == Integer.MIN_VALUE) {
this.mWidth = widthSpecSize;
} else {
this.mWidth = 0;
}
if (heightSpecMode == Integer.MIN_VALUE || heightSpecMode == 0) {
this.mHeight = dipToPx(15);
} else {
this.mHeight = heightSpecSize;
}
setMeasuredDimension(this.mWidth, this.mHeight);
}
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int wSpecMode = MeasureSpec.getMode(widthMeasureSpec);
int wSpecSize = MeasureSpec.getSize(widthMeasureSpec);
int hSpecMode = MeasureSpec.getMode(heightMeasureSpec);
int hSpecSize = MeasureSpec.getSize(heightMeasureSpec);
int resultWidth = wSpecSize;
int resultHeight = hSpecSize;
if (wSpecMode == Integer.MIN_VALUE && hSpecMode == Integer.MIN_VALUE) {
resultWidth = this.bitmap.getWidth();
resultHeight = this.bitmap.getHeight();
} else if (wSpecMode == Integer.MIN_VALUE) {
resultWidth = this.bitmap.getWidth();
resultHeight = hSpecSize;
} else if (hSpecMode == Integer.MIN_VALUE) {
resultWidth = wSpecSize;
resultHeight = this.bitmap.getHeight();
}
setMeasuredDimension(resultWidth, resultHeight);
}
/**
* 测量控件
*
* @param view
*/
public static void measureView(View view) {
ViewGroup.LayoutParams p = view.getLayoutParams();
if (p == null) {
p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
}
int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0 + 0, p.width);
int lpHeight = p.height;
int childHeightSpec;
if (lpHeight > 0) {
childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight,
MeasureSpec.EXACTLY);
} else {
childHeightSpec = MeasureSpec.makeMeasureSpec(0,
MeasureSpec.UNSPECIFIED);
}
view.measure(childWidthSpec, childHeightSpec);
}
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (this.mTarget == null) {
ensureTarget();
}
if (this.mTarget != null) {
this.mTarget.measure(MeasureSpec.makeMeasureSpec((getMeasuredWidth() - getPaddingLeft()) - getPaddingRight(), 1073741824), MeasureSpec.makeMeasureSpec((getMeasuredHeight() - getPaddingTop()) - getPaddingBottom(), 1073741824));
this.mCircleView.measure(MeasureSpec.makeMeasureSpec(this.mCircleWidth, 1073741824), MeasureSpec.makeMeasureSpec(this.mCircleHeight, 1073741824));
if (!(this.mUsingCustomStart || this.mOriginalOffsetCalculated)) {
this.mOriginalOffsetCalculated = true;
int i = -this.mCircleView.getMeasuredHeight();
this.mOriginalOffsetTop = i;
this.mCurrentTargetOffsetTop = i;
}
this.mCircleViewIndex = -1;
for (int index = 0; index < getChildCount(); index++) {
if (getChildAt(index) == this.mCircleView) {
this.mCircleViewIndex = index;
return;
}
}
}
}
/**
* Measures and takes a screenshot of the given View {@link View}
*
* @param view The view which the screenshot is taken.
* @return A {@link Bitmap} for the taken screenshot
*/
public Bitmap takeScreenshotForView(View view){
// Measures
view.measure(MeasureSpec.makeMeasureSpec(view.getWidth(), MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(view.getHeight(), MeasureSpec.EXACTLY));
view.layout((int) view.getX(), (int) view.getY(),
(int) view.getX() + view.getMeasuredWidth(), (int) view.getY() + view.getMeasuredHeight());
view.setDrawingCacheEnabled(true);
view.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
return bitmap;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
if (width >= 2 * mIllustration.getWidth() && width > height) {
mPromoContent.setOrientation(LinearLayout.HORIZONTAL);
setMaxChildWidth(mMaxChildWidthHorizontal);
ApiCompatibilityUtils.setPaddingRelative(
mIllustration, 0, 0, mIllustrationPaddingSide, 0);
} else {
mPromoContent.setOrientation(LinearLayout.VERTICAL);
setMaxChildWidth(mMaxChildWidth);
mIllustration.setPadding(0, 0, 0, mIllustrationPaddingBottom);
}
setMaxChildHeight(height - mFrameHeightMargin);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
boolean lockedExpanded = widthMode == 1073741824;
setFillViewport(lockedExpanded);
int childCount = this.mTabLayout.getChildCount();
if (childCount <= 1 || !(widthMode == 1073741824 || widthMode == Integer.MIN_VALUE)) {
this.mMaxTabWidth = -1;
} else if (childCount > 2) {
this.mMaxTabWidth = (int) (((float) MeasureSpec.getSize(widthMeasureSpec)) * 0.4f);
} else {
this.mMaxTabWidth = MeasureSpec.getSize(widthMeasureSpec) / 2;
}
int oldWidth = getMeasuredWidth();
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int newWidth = getMeasuredWidth();
if (lockedExpanded && oldWidth != newWidth) {
setCurrentItem(this.mSelectedTabIndex);
}
}
private void measureScrapChild(View child, int position, int widthMeasureSpec) {
ListView.LayoutParams p = (ListView.LayoutParams) child.getLayoutParams();
if (p == null) {
p = new ListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 0);
child.setLayoutParams(p);
}
//XXX p.viewType = mAdapter.getItemViewType(position);
//XXX p.forceAdd = true;
int childWidthSpec = ViewGroup.getChildMeasureSpec(widthMeasureSpec,
mDropDownList.getPaddingLeft() + mDropDownList.getPaddingRight(), 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);
}
public View createView() {
inflater = LayoutInflater.from(context);
content = inflater.inflate(contentResId, null);
realHeader = inflater.inflate(headerResId, null);
realHeaderLayoutParams = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
realHeaderLayoutParams.gravity = Gravity.TOP;
// Use measured height here as an estimate of the header height, later on after the layout is complete
// we'll use the actual height
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(LayoutParams.MATCH_PARENT, MeasureSpec.EXACTLY);
int heightMeasureSpec = MeasureSpec.makeMeasureSpec(LayoutParams.WRAP_CONTENT, MeasureSpec.EXACTLY);
realHeader.measure(widthMeasureSpec, heightMeasureSpec);
headerHeight = realHeader.getMeasuredHeight();
listView = (ListView) content.findViewById(android.R.id.list);
if (listView != null) {
createListView();
} else {
createScrollView();
}
return root;
}
@Override
public void onMeasure(final RenderSurfaceView pRenderSurfaceView, final int pWidthMeasureSpec, final int pHeightMeasureSpec) {
BaseResolutionPolicy.throwOnNotMeasureSpecEXACTLY(pWidthMeasureSpec, pHeightMeasureSpec);
final int specWidth = MeasureSpec.getSize(pWidthMeasureSpec);
final int specHeight = MeasureSpec.getSize(pHeightMeasureSpec);
final float desiredRatio = this.mRatio;
final float realRatio = (float)specWidth / specHeight;
int measuredWidth;
int measuredHeight;
if(realRatio < desiredRatio) {
measuredWidth = specWidth;
measuredHeight = Math.round(measuredWidth / desiredRatio);
} else {
measuredHeight = specHeight;
measuredWidth = Math.round(measuredHeight * desiredRatio);
}
pRenderSurfaceView.setMeasuredDimensionProxy(measuredWidth, measuredHeight);
}
/**
* Add any other objects that depend on the OverlayPanelContent having already been created.
*/
private OverlayPanelContent createNewOverlayPanelContentInternal() {
OverlayPanelContent content = mContentFactory.createNewOverlayPanelContent();
content.setContentViewClient(new ContentViewClient() {
@Override
public int getDesiredWidthMeasureSpec() {
if (isFullWidthSizePanel()) {
return super.getDesiredWidthMeasureSpec();
} else {
return MeasureSpec.makeMeasureSpec(
getContentViewWidthPx(),
MeasureSpec.EXACTLY);
}
}
@Override
public int getDesiredHeightMeasureSpec() {
if (isFullWidthSizePanel()) {
return super.getDesiredHeightMeasureSpec();
} else {
return MeasureSpec.makeMeasureSpec(
getContentViewHeightPx(),
MeasureSpec.EXACTLY);
}
}
});
return content;
}
public static int getDefaultSize(int size, int measureSpec) {
int result = size;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
switch (specMode) {
case Integer.MIN_VALUE:
case NTLMConstants.FLAG_NEGOTIATE_KEY_EXCHANGE /*1073741824*/:
return specSize;
case 0:
return size;
default:
return result;
}
}
@Override
public void onMeasure(final RenderSurfaceView pRenderSurfaceView, final int pWidthMeasureSpec, final int pHeightMeasureSpec) {
BaseResolutionPolicy.throwOnNotMeasureSpecEXACTLY(pWidthMeasureSpec, pHeightMeasureSpec);
final int measuredWidth = MeasureSpec.getSize(pWidthMeasureSpec);
final int measuredHeight = MeasureSpec.getSize(pHeightMeasureSpec);
pRenderSurfaceView.setMeasuredDimensionProxy(measuredWidth, measuredHeight);
}
@Override
public void animateChanges(LayoutChangeset changes,
FreeFlowContainer callback) {
this.changes = changes;
for(Pair<FreeFlowItem, Rect> item : changes.getMoved()){
Rect r = item.first.frame;
View v = item.first.view;
int wms = MeasureSpec.makeMeasureSpec(r.right-r.left, MeasureSpec.EXACTLY);
int hms = MeasureSpec.makeMeasureSpec(r.bottom-r.top, MeasureSpec.EXACTLY);
v.measure(wms,hms );
v.layout(r.left, r.top, r.right, r.bottom);
}
callback.onLayoutChangeAnimationsCompleted(this);
}
private int measureHeight(int measureSpec, int height) {
int mode = MeasureSpec.getMode(measureSpec);
int size = MeasureSpec.getSize(measureSpec);
if (mode == NTLMConstants.FLAG_NEGOTIATE_KEY_EXCHANGE) {
return size;
}
int result = height;
if (mode == Integer.MIN_VALUE) {
return Math.min(result, size);
}
return result;
}
@Override
protected void onViewSelected(View view) {
int rightLimit = getParentViewGroup().getWidth() -
getParentViewGroup().getPaddingRight();
// measure the hover card width, if it's too large, align hover card
// right edge with row view's right edge
view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams();
if (mCardLeft + view.getMeasuredWidth() > rightLimit) {
params.leftMargin = rightLimit - view.getMeasuredWidth();
} else {
params.leftMargin = mCardLeft;
}
view.requestLayout();
}
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int measureWidthSize = MeasureSpec.getSize(widthMeasureSpec);
int measureHeightSize = MeasureSpec.getSize(heightMeasureSpec);
int measureWidthMode = MeasureSpec.getMode(widthMeasureSpec);
int measureHeightMode = MeasureSpec.getMode(heightMeasureSpec);
if (measureWidthMode == Integer.MIN_VALUE && measureHeightMode == Integer.MIN_VALUE) {
setMeasuredDimension(measureWidthSize, measureHeightSize);
} else if (measureWidthMode == Integer.MIN_VALUE) {
setMeasuredDimension(measureWidthSize, measureHeightSize);
} else if (measureHeightMode == Integer.MIN_VALUE) {
setMeasuredDimension(measureWidthSize, measureHeightSize);
}
}
@Override
public void setAnimation(View view) {
view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
int h = view.getMeasuredHeight();
animatorSet.playTogether(//
ObjectAnimator.ofFloat(view, "alpha", 1, 1, 0),//
ObjectAnimator.ofFloat(view, "scaleX", 1, 0.475f, 0.1f),//
ObjectAnimator.ofFloat(view, "scaleY", 1, 0.475f, 0.1f),//
ObjectAnimator.ofFloat(view, "translationY", 0, -60, h));
}
public Bitmap convertViewToBitmap(View view) {
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
return bitmap;
}
public void adjustChildren(int widthMeasureSpec, int heightMeasureSpec) {
if (Log.isLoggable(TAG, 3)) {
Log.d(TAG, "adjustChildren: " + this.mHost + " widthMeasureSpec: " + MeasureSpec.toString(widthMeasureSpec) + " heightMeasureSpec: " + MeasureSpec.toString(heightMeasureSpec));
}
int widthHint = MeasureSpec.getSize(widthMeasureSpec);
int heightHint = MeasureSpec.getSize(heightMeasureSpec);
if (Log.isLoggable(TAG, 3)) {
Log.d(TAG, "widthHint = " + widthHint + " , heightHint = " + heightHint);
}
int N = this.mHost.getChildCount();
for (int i = 0; i < N; i++) {
View view = this.mHost.getChildAt(i);
LayoutParams params = view.getLayoutParams();
if (Log.isLoggable(TAG, 3)) {
Log.d(TAG, "should adjust " + view + " " + params);
}
if (params instanceof PercentLayoutParams) {
PercentLayoutInfo info = ((PercentLayoutParams) params).getPercentLayoutInfo();
if (Log.isLoggable(TAG, 3)) {
Log.d(TAG, "using " + info);
}
if (info != null) {
supportTextSize(widthHint, heightHint, view, info);
supportPadding(widthHint, heightHint, view, info);
supportMinOrMaxDimesion(widthHint, heightHint, view, info);
if (params instanceof MarginLayoutParams) {
info.fillMarginLayoutParams((MarginLayoutParams) params, widthHint, heightHint);
} else {
info.fillLayoutParams(params, widthHint, heightHint);
}
}
}
}
}
private void measureHeader() {
int heightMeasureSpec;
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(((getWidth() - getPaddingLeft()) - getPaddingRight()) - (isScrollBarOverlay() ? 0 : getVerticalScrollbarWidth()), NTLMConstants.FLAG_NEGOTIATE_KEY_EXCHANGE);
LayoutParams params = this.mHeader.getLayoutParams();
if (params == null || params.height <= 0) {
heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, 0);
} else {
heightMeasureSpec = MeasureSpec.makeMeasureSpec(params.height, NTLMConstants.FLAG_NEGOTIATE_KEY_EXCHANGE);
}
this.mHeader.measure(widthMeasureSpec, heightMeasureSpec);
this.mHeader.layout(getPaddingLeft(), 0, getWidth() - getPaddingRight(), this.mHeader.getMeasuredHeight());
}
@Override
public void setAnimation(View view) {
view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
int w = view.getMeasuredWidth();
animatorSet.playTogether(//
ObjectAnimator.ofFloat(view, "scaleX", 1, 0.475f, 0.1f),//
ObjectAnimator.ofFloat(view, "scaleY", 1, 0.475f, 0.1f),//
ObjectAnimator.ofFloat(view, "translationX", 0, -42, w),//
ObjectAnimator.ofFloat(view, "alpha", 1, 1, 0));
}
/**
* Ensures that a TextView is wide enough to contain its text without
* wrapping or clipping. Measures the specified view and sets the minimum
* width to the view's desired width.
*
* @param v the text view to measure
*/
private static void ensureMinimumTextWidth(TextView v) {
v.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
// Set both the TextView and the View version of minimum
// width because they are subtly different.
final int minWidth = v.getMeasuredWidth();
v.setMinWidth(minWidth);
v.setMinimumWidth(minWidth);
}
@Override
public void animateChanges(LayoutChangeset changes,
FreeFlowContainer callback) {
this.changes = changes;
for(Pair<FreeFlowItem, Rect> item : changes.getMoved()){
Rect r = item.first.frame;
View v = item.first.view;
int wms = MeasureSpec.makeMeasureSpec(r.right-r.left, MeasureSpec.EXACTLY);
int hms = MeasureSpec.makeMeasureSpec(r.bottom-r.top, MeasureSpec.EXACTLY);
v.measure(wms,hms );
v.layout(r.left, r.top, r.right, r.bottom);
}
callback.onLayoutChangeAnimationsCompleted(this);
}
private void measureChild(View child) {
final ViewGroup.LayoutParams lp = child.getLayoutParams();
final int secondarySpec = (mRowSizeSecondaryRequested == ViewGroup.LayoutParams.WRAP_CONTENT) ?
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED) :
MeasureSpec.makeMeasureSpec(mFixedRowSizeSecondary, MeasureSpec.EXACTLY);
int widthSpec, heightSpec;
if (mOrientation == HORIZONTAL) {
widthSpec = ViewGroup.getChildMeasureSpec(
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
0, lp.width);
heightSpec = ViewGroup.getChildMeasureSpec(secondarySpec, 0, lp.height);
} else {
heightSpec = ViewGroup.getChildMeasureSpec(
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
0, lp.height);
widthSpec = ViewGroup.getChildMeasureSpec(secondarySpec, 0, lp.width);
}
child.measure(widthSpec, heightSpec);
if (DEBUG) Log.v(getTag(), "measureChild secondarySpec " + Integer.toHexString(secondarySpec) +
" widthSpec " + Integer.toHexString(widthSpec) +
" heightSpec " + Integer.toHexString(heightSpec) +
" measuredWidth " + child.getMeasuredWidth() +
" measuredHeight " + child.getMeasuredHeight());
if (DEBUG) Log.v(getTag(), "child lp width " + lp.width + " height " + lp.height);
}
/**
* Ensures that a TextView is wide enough to contain its text without
* wrapping or clipping. Measures the specified view and sets the minimum
* width to the view's desired width.
*
* @param v the text view to measure
*/
private static void ensureMinimumTextWidth(TextView v) {
v.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
// Set both the TextView and the View version of minimum
// width because they are subtly different.
final int minWidth = v.getMeasuredWidth();
v.setMinWidth(minWidth);
v.setMinimumWidth(minWidth);
}
/**
* Updates the styles of the {@link ReactShadowNode} based on the Measure specs received by
* parameters.
*/
public void updateRootView(
ReactShadowNode rootCSSNode, int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
switch (widthMode) {
case EXACTLY:
rootCSSNode.setStyleWidth(widthSize);
break;
case AT_MOST:
rootCSSNode.setStyleMaxWidth(widthSize);
break;
case UNSPECIFIED:
rootCSSNode.setStyleWidthAuto();
break;
}
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
switch (heightMode) {
case EXACTLY:
rootCSSNode.setStyleHeight(heightSize);
break;
case AT_MOST:
rootCSSNode.setStyleMaxHeight(heightSize);
break;
case UNSPECIFIED:
rootCSSNode.setStyleHeightAuto();
break;
}
}
@Override
public void onMeasure(final IResolutionPolicy.Callback pResolutionPolicyCallback, final int pWidthMeasureSpec, final int pHeightMeasureSpec) {
BaseResolutionPolicy.throwOnNotMeasureSpecEXACTLY(pWidthMeasureSpec, pHeightMeasureSpec);
final int measuredWidth = MeasureSpec.getSize(pWidthMeasureSpec);
final int measuredHeight = MeasureSpec.getSize(pHeightMeasureSpec);
pResolutionPolicyCallback.onResolutionChanged(measuredWidth, measuredHeight);
}
@Override
public void initForMenu(Context context, MenuBuilder menu) {
super.initForMenu(context, menu);
final Resources res = context.getResources();
if (!mReserveOverflowSet) {
mReserveOverflow = reserveOverflow(mContext);
}
if (!mWidthLimitSet) {
mWidthLimit = res.getDisplayMetrics().widthPixels / 2;
}
// Measure for initial configuration
if (!mMaxItemsSet) {
mMaxItems = getResources_getInteger(context, R.integer.abs__max_action_buttons);
}
int width = mWidthLimit;
if (mReserveOverflow) {
if (mOverflowButton == null) {
mOverflowButton = new OverflowMenuButton(mSystemContext);
final int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
mOverflowButton.measure(spec, spec);
}
width -= mOverflowButton.getMeasuredWidth();
} else {
mOverflowButton = null;
}
mActionItemWidthLimit = width;
mMinCellSize = (int) (ActionMenuView.MIN_CELL_SIZE * res.getDisplayMetrics().density);
// Drop a scrap view as it may no longer reflect the proper context/config.
mScrapActionButtonView = null;
}
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
if (mDoMeasureInLayout && mRenderState != null) {
mRenderState.measure(
MeasureSpec.makeMeasureSpec(right - left, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(bottom - top, MeasureSpec.EXACTLY),
null);
mDoMeasureInLayout = false;
}
mMountState.mount(mCurrentRenderTree);
}