Don’t BB, look at the source code
getWidth()
- The View class
* Return the width of your view.
*
* @return The width of your view, in pixels.
*/
public final int getWidth(a) {
return mRight - mLeft;
}
Copy the code
-
So, what are mRight and mLeft?
-
Look at the source code
/** * The distance in pixels from The left edge of this view's parent * to The left edge(edge) of this view.@hide} * The pixel distance from the current view's left edge to the parent view's left edge */
protected int mLeft;
/**
* The distance in pixels from the left edge of this view's parent
* to the right edge of this view.
* {@hide} * Same with */
protected int mRight;
Copy the code
- So, this is just a relative value obtained by adding and subtracting, not a real measured size;
getMeasuredWidth
* Like {@link #getMeasuredWidthAndState()}, but only returns the * raw width component (that is the result is masked by * {@link #MEASURED_SIZE_MASK}). * * @return The raw measured width of this view. * */ public Final int measuredWidth () {return mMeasuredWidth & MEASURED_SIZE_MASK; }Copy the code
- There are two variables, one is mMeasuredWidth and one is MEASURED_SIZE_MASK
/**
* Bits of {@link #getMeasuredWidthAndState()} and
* {@link# measuredwidthandState ()} that provide the actual (real) measured size. */
public static final int MEASURED_SIZE_MASK = 0x00ffffff;
Copy the code
MEASURED_SIZE_MASK = MEASURED_SIZE_MASK = MEASURED_SIZE_MASK = MEASURED_SIZE_MASK = MEASURED_SIZE_MASK = MEASURED_SIZE_MASK = MEASURED_SIZE_MASK = MEASURED_SIZE_MASK = MEASURED_SIZE_MASK = MEASURED_SIZE_MASK = MEASURED_SIZE_MASK = MEASURED_SIZE_MASK = MEASURED_SIZE_MASK = MEASURED_SIZE_MASK UNSPECIFIED, MeasureSpec.AT_MOST, MeasureSpec.EXACTLY, MeasureSpec encapsulates the mode and size with the makeMeasureSpec() method. The upper 2 bits are the measurement mode, and the lower 30 bits are the size of view.
How the value of mMeasuredWidth is set
-
OnMeasure ()—->setMeasuredDimension()—–>setMeasuredDimensionRaw()
-
Step 1: setMeasuredDimension();
/**
* <p>This method must be called by {@link#onMeasure(int, int)} to store the * measured width and measured height. Failing to do so will trigger an * exception at measurement </p> * This method must be called in onMeasure(int, int)@param measuredWidth The measured width of this view. May be a complex
* bit mask as defined by {@link #MEASURED_SIZE_MASK} and
* {@link #MEASURED_STATE_TOO_SMALL}.
* @param measuredHeight The measured height of this view. May be a complex
* bit mask as defined by {@link #MEASURED_SIZE_MASK} and
* {@link #MEASURED_STATE_TOO_SMALL}.
*/
protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
boolean optical = isLayoutModeOptical(this);
if(optical ! = isLayoutModeOptical(mParent)) { Insets insets = getOpticalInsets();int opticalWidth = insets.left + insets.right;
int opticalHeight = insets.top + insets.bottom;
measuredWidth += optical ? opticalWidth : -opticalWidth;
measuredHeight += optical ? opticalHeight : -opticalHeight;
}
// Set the original size of the view
setMeasuredDimensionRaw(measuredWidth, measuredHeight);
}
Copy the code
Step 2: setMeasuredDimensionRaw();
/**
*
* Sets the measured dimension without extra processing for things like optical bounds.
* Useful for reapplying consistent values that have already been cooked with adjustments
* for optical bounds, etc. such as those from the measurement cache.
*
* @param measuredWidth The measured width of this view. May be a complex
* bit mask as defined by {@link #MEASURED_SIZE_MASK} and
* {@link #MEASURED_STATE_TOO_SMALL}.
* @param measuredHeight The measured height of this view. May be a complex
* bit mask as defined by {@link #MEASURED_SIZE_MASK} and
* {@link #MEASURED_STATE_TOO_SMALL}.
*/
private void setMeasuredDimensionRaw(int measuredWidth, int measuredHeight) {
mMeasuredWidth = measuredWidth;
mMeasuredHeight = measuredHeight;
mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
}
Copy the code
- Contains complex mask operations
- MMeasuredWidth and mMeasuredHeight contain the true size of the view, as well as the measurement mode;
- So, I getMeasuredWidth() takes the size after onMeasure();