1. 继承ViewGroup
例如:ViewGroup、LinearLayout、FrameLayout、RelativeLayout等。
2. 继承View
例如:View、TextView、ImageView、Button等。
自定义控件基本绘制原理:
View的绘制基本上由measure()、layout()、draw()这个三个函数完成
1.)测量-Measure过程是计算视图大小,View measure过程相关方法主要有三个:
measure调用onMeasure,onMeasure测量宽度、高度
MeasureSpec.getSize(widthMeasureSpec);// 获取宽度值
MeasureSpec.getMode(widthMeasureSpec);// 获取宽度模式
MeasureSpec.getMode(heightMeasureSpec);// 获取高度模式
MeasureSpec.getSize(heightMeasureSpec);// 获取高度值
// 按照最新的高度和宽度设置空间宽高
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
public final void measure(int widthMeasureSpec, int heightMeasureSpec)
然后调用setMeasureDimension保存测量结果
protected final void setMeasuredDimension(int measuredWidth, int measuredHeight)
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
布局-Layout过程用于设置视图在屏幕中显示的位置,View layout过程相关方法主要要三个:
public void layout(int l, int t, int r, int b)
protected boolean setFrame(int left, int top, int right, int bottom)
protected void onLayout(boolean changed, int left, int top, int right, int bottom)