整体流程
onMeasure -> onLayout -> onDraw
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);
...
if (widthMode == MeasureSpec.EXACTLY) {
// 精确模式,以父容器给的宽度作当前TextView的宽度.
width = widthSize;
} else {
if (mLayout != null && mEllipsize == null) {
//调用desired方法计算期望值,如果行数大于1就返回-1,否则返回单行宽度
des = desired(mLayout);
}
//小于0,即行数大于1行,就去判断是否是boring
if (des < 0) {
boring = BoringLayout.isBoring(mTransformed, mTextPaint, mTextDir, mBoring);
if (boring != null) {
mBoring = boring;
}
} else {
fromexisting = true;
}
...
//加上Drawable的宽度
final Drawables dr = mDrawables;
if (dr != null) {
width = Math.max(width, dr.mDrawableWidthTop);
width = Math.max(width, dr.mDrawableWidthBottom

本文解析了Android TextView组件的测量(onMeasure)、布局(onLayout)和绘制(onDraw)关键步骤,涉及MeasureSpec、Layout参数计算、BoringLayout与StaticLayout等。了解这些核心过程有助于深入理解View绘制原理。
最低0.47元/天 解锁文章
1082

被折叠的 条评论
为什么被折叠?



