知识点:
view的measure过程和Activity的生命周期不同步
一、View的measure过程
measure是final,不可重写;
measure---onMeasure---setMeasuredDimension--- getDefaultSize这里ATMOST和EXACTLY进行了相同的操作
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
if (widthSpecMode == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST) {
setMeasuredDimension(mWidth, mHeight);//自己设定的值
} else if (widthSpecMode == MeasureSpec.AT_MOST) {
setMeasuredDimension(mWidth, heightSpecSize);
} else if (heightSpecMode == MeasureSpec.AT_MOST) {
setMeasuredDimension(widthSpecSize, mHeight);
}
}
二、ViewGroup的measure过程
除了完成自己的measure过程外,还会遍历所有了元素的measure,提供一个measureChildren方法来进行。
三、在Activity启动时,需要获取view的宽高。
1、onWindowFocusChanged
2、view.post(通过runnable)
3、
4、
measure错误用法