onMeasure
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);
此处调用了MeasureSpec类下的getMode和getSize
MODE_MASK属性等于0x3 << 30;
即二进制的3左移30位11 00 0000 0000 0000 0000 0000 0000 0000
&运算符将两个值转化二进制,而后进行比较,同1得1,非1和同0则位数为0
例如3为0011 4为0100 则对比可得0000 结果为0 而2为0010 与3对比可得0010 结果为2
getMode用于确定类型
public static int getMode(int measureSpec) {
//noinspection ResourceType
return (measureSpec & MODE_MASK);
//返回传入值与11 00 0000 0000 0000 0000 0000 0000 0000进行与运算结果
}
getSize用于确定大小
public static int getSize(int measureSpec) {
return (measureSpec & ~MODE_MASK);
//返回传入值对MODE_MASK取反的结果进行与运算
}
之后调用BoringLayout.Metrics创建对象,此类负责对普通文本进行绘制
UNKNOWN_BORING=new BoringLayout.Metrics();
BoringLayout.Metrics boring = UNKNOWN_BORING;
BoringLayout.Metrics hintBoring = UNKNOWN_BORING;
此后获得一个TextDirectionHeuristic对象,在获取最大宽度的限制值,判断是否为精确模式
if (mTextDir == null) { mTextDir = getTextDirectionHeuristic(); }
//文字的排序
final float widthLimit = (widthMode == MeasureSpec.AT_MOST)? (float) widthSize : Float.MAX_VALUE;
//AT_MOSE=2<<30也表示为warp模式,如果是warp模式,那么采用本身的宽
//不是则采用float最大值,这里只是暂时定为float最大值,后续还会做更多判断
if (widthMode == MeasureSpec.EXACTLY) { width = widthSize;}
//精确模式下,将宽等于widthSize
if (mLayout != null && mEllipsize == null) {
des = desired(mLayout);//行数不为空是调用,dew=-1表示行数大于1行
}
if (des < 0) {
boring = BoringLayout.isBoring(mTransformed, mTextPaint, mTextDir, mBoring);
if (boring != null) {
mBoring = boring;
}//<0就是-1行数大于1然后判断边界值,将mBoring变成边界值
} else {
fromexisting = true;
}
if (boring == null || boring == UNKNOWN_BORING) {
//边界值为空或边界值还是初始化的状态
if (des < 0) {
des = (int) Math.ceil(Layout.getDesiredWidthWithLimit(mTransformed, 0,
mTransformed.length(), mTextPaint, mTextDir, widthLimit));
//
}
width = des;
} else {
width = boring.width;
}
此后判断初始化hint的宽度不赘述
EMS模式下表示宽度会根据行高与宽度共同决定
mMaxWidth * getLineHeight()
此后调用了makeNewLayout()方法暂时略过
然后判断高度,也和此前大致相仿。