Android中的View与ViewGroup绘制过程,手势监听顺序与使用


一 View ,ViewGroup的绘制过程

ViewGroup绘制包括两个步骤:1.measure 2.layout

在两个步骤中分别调用回调函数:1.onMeasure()   2.onLayout()

1.onMeasure() 在这个函数中,ViewGroup会接受childView的请求的大小,然后通过childView的measure(newWidthMeasureSpec, heightMeasureSpec)函数存储到childView中,以便childView的getMeasuredWidth() andgetMeasuredHeight() 的值可以被后续工作得到。

2.onLayout() 在这个函数中,ViewGroup会拿到childView的getMeasuredWidth() andgetMeasuredHeight(),用来布局所有的childView。

3.View.MeasureSpec与 LayoutParams 这两个类,是ViewGroup与childView协商大小用的。其中,View.MeasureSpec是ViewGroup用来部署childView用的, LayoutParams是childView告诉ViewGroup 我需要多大的地方。

4.在View 的onMeasure的最后要调用setMeasuredDimension()这个方法存储View的大小,这个方法决定了当前View的大小。

具体详见Android官方文档 dev guide->User Interface->How Android Draws Views

二 View,ViewGroup的手势监听顺序与使用

View的手势监听相关回调函数:onTouchEvent()

ViewGroup的手势监听相关回调函数:onTouchEvent(),onInterceptTouchEvent()

1.这两个回调函数都会返回一个boolean变量,表示是否消费了此手势。如果消费了,返回true,如果未消费,返回false。

2.当用户触摸一下屏幕,产生手势MotionEvent,

ViewGroup的onInterceptTouchEvent()会接受此MotionEvent。

如果此回调函数返回true,则表示此ViewGroup消费了此手势,不想再让他的childView去处理,childView的onTouchEvent()便不会再接受此手势,同时此ViewGroup的onTouchEvent()会接受此手势。

如果此回调函数返回false,则表示此ViewGroup未消费了此手势,想让他的childView去处理,childView的onTouchEvent()接受此手势,同时此ViewGroup的onTouchEvent()不会接受此手势。

相关内容