自定义view

来源:互联网 发布:人工智能猜出眉毛 编辑:程序博客网 时间:2024/06/06 13:16

一、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最多只能用这么多



0 0
原创粉丝点击