自定义控件学习笔记1.1

测量一个View主要关心的2个属性:1.View的模式  2View的尺寸

MesasureSpc 用来存放这2个属性

View的模式有3个

mode_mask :11 00000 00000 00000 00000 00000 00000 

~mode_mask: 00 11111 11111 11111 11111 11111 11111 

private static final int MODE_SHIFT = 30; 位移的位数
/**
 * Measure specification mode: The parent has not imposed any constraint
 * on the child. It can be whatever size it wants.
 */
public static final int UNSPECIFIED = 0 << MODE_SHIFT;
 0左移30位  代表00  00000 00000 00000 00000 00000 00000 
父容器不对View做任何限制,系统内部使用
/**
 * Measure specification mode: The parent has determined an exact size
 * for the child. The child is going to be given those bounds regardless
 * of how big it wants to be.
 */
public static final int EXACTLY     = 1 << MODE_SHIFT; 
 1左移30位 代表01  00000 00000 00000 00000 00000 00000 
父容器检测出View的大小,View的大小就是SpecSize  LayoutParams match_parent 固定大小
/**
 * Measure specification mode: The child can be as large as it wants up
 * to the specified size.
 */
public static final int AT_MOST     = 2 << MODE_SHIFT; 
2也就是10  左移30位 代表10 00000 00000 00000 00000 00000 00000 
父容器指定一个可用大小,View的大小不能超过这个值,LayoutParams wrap_content

mode+size= measureSpec

ViewGroup的测量过程

measure-->onMeasure(测量子控件宽高)-->setMeasureDimension-->setMeasureDismensionRaw(保存自己的宽高)

View 的测量过程

measure-- >onMeasure-->setMeasureDimension-->setMeasureDimensionRaw(保存自己的宽高)

ViewGroup的绘制流程

1.绘制背景 drawBackground(canvas)

2.绘制自己onDraw(canvas)

3.绘制子View dispatchDraw(canvas)

4.绘制前景,滚动条等装饰onDrawForeground(canvas)

View的绘制过程

1.绘制背景 drawBackground(canvas)

2.绘制自己onDraw(canvas)

3.绘制前景,滚动条等装饰onDrawForeground(canvas)

ViewGroup 布局过程

layout(确定自己的位置,4个点位置)->onlayout(j进行字View的布局)

View的布局过程

layout(确定自己的位置,4个点位置)

自定义布局的 时候需要实现3个方法

onMeasure-->onLayout(容器)->ondraw()  

猜你喜欢

转载自blog.csdn.net/xiexiaotian11/article/details/88763532