1.View是Activity上的一块矩形区域
2.View主要负责被动展示自己(onMeasure(),onLayout(),onDraw())
和响应屏幕点击时间(onTouch事件的分发)
3.自定义view就是重写onMeasure,onLayout,onDraw;
4. MeasureSpec是由父布局与View 自身的LayoutParams来决定的。再根据自身的内容决定宽和高。
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}
5.总而言之就是onMeasure和onLayout方法会根据父View和自身的LayoutParams来决定宽高和位置。
6.onDraw()方法实际就是在View上盖一层画布,然后开始画。
7。 postInvalidate 与 invalidate
界面刷新 onDraw方法会执行,区别就是Invalidate不能直接在线程中调用,因为他是违背了单线程模型:Android UI操作并不是线程安全的,并且这些操作必须在UI线程中调用。 鉴于此,如果要使用invalidate的刷新,那我们就得配合handler的使用,使异步非ui线程转到ui线程中调用,如果要在非ui线程中直接使用就调用postInvalidate方法即可,这样就省去使用handler的烦恼。