一、view作用
This class represents the basic building block for user interface components. A View occupies a rectangular area on the screen and is responsible
for drawing and event handling.
这个类表示用户界面组件的基本构建块。视图在屏幕上占据一个矩形区域,负责绘图和事件处理。
二、一般步骤
1、自定义View的属性
2、在View的构造方法中获得我们自定义的属性
3、重写onMesure
4、重写onDraw
onMeasure负责对当前View的尺寸进行测量,onDraw负责把当前这个View绘制出来
二、获取ViewGroup的size,因为只有知道ViewGroup的大小才能进一步去绘制自己View的位置大小
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Mode的模式
UNSPECIFIED | 随意 |
EXACTLY | 当前的获取尺寸就是当前View应该取的尺寸 |
AT_MOST | 最多只能用这么多 |