自定义控件

1.声明属性

attrs.xml中;

2.测量

MeasureSpec30位,前两位是模式,后28位是具体的size值。

mode = MeasureSpce.getMode(heightMeasureSpec);

size = MeasureSpec.getSize(heightMeasureSpec);

if(mode = MeasureSpec.EXACTLY){

result = size;

}

onMeasure:测量自身的大小

三种模式 

EXACTLY 完全根据自身的数值,如100dp,用于match_parent,此时result=size

AT_MOST 最大尺寸不能超过一个值,用于warp_content

INSPECIFIED 大小没有限制,用于listviewscrollview

setMeasureDimesions():传入测量的结果,设置尺寸

requestLayout()请求父控件中重新测量

3.布局:

onLayout()viewgroup中,父控件设置子view在其中显示的位置

4.绘制

onDraw():绘制内容区域;

invalidate(),postInvalidate()区别:UI线程和非UI线程中调用;

5.交互

onTouchEvent():自定义控件和用户交互时用

有加速度检测时,初始化 initVelocityTrackerIfNotExsits

6.拦截

onInterceptTouchEvent()viewgroup中的方法,父控件拦截事件自己处理,阻止传递给子控件


自定义view流程:

分析view所需属性,xml文件中编写,view中构造方法中获取,测量,布局,绘制;


猜你喜欢

转载自blog.csdn.net/taambernk520/article/details/73409831