android之View的启动过程
转自:http://www.cdtarena.com/gpx/201308/9607.html
程序里调用了onSizeChanged方法进行了一些设置,不知道onSizeChanged是在什么时候启动的,所以研究了一下View的启动流程
代码如下:
- public class TestView extends View {
- public TestView(Context context) {
- super(context);
- Log.d("mDebug", "TestView context");
- }
- public TestView(Context context, AttributeSet attrs) {
- super(context, attrs);
- Log.d("mDebug", "TestView context, attrs="+attrs.getAttributeValue(0));
- }
- public TestView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- Log.d("mDebug", "TestView context,attrs,defStyle attrs="+attrs.getAttributeValue(0));
- }
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- Log.d("mDebug", "onDraw");
- }
- @Override
- protected void onFinishInflate() {
- super.onFinishInflate();
- Log.d("mDebug", "onFinishInflate");
- }
- 成都安卓培训、成都android培训
- @Override
- protected void onSizeChanged(int w, int h, int oldw, int oldh) {
- super.onSizeChanged(w, h, oldw, oldh);
- Log.d("mDebug", "onSizeChanged,w="+w+",h="+h+",oldw="+oldw+",oldh="+oldh);
- }
- }
输出如下:http://www.cdtarena.com/net.html
- 22:23:03.587: D/mDebug(9715): TestView context, attrs=@2131034112
- 22:23:03.597: D/mDebug(9715): onFinishInflate
- 22:23:03.667: D/mDebug(9715): onSizeChanged,w=720,h=1080,oldw=0,oldh=0
- 22:23:03.727: D/mDebug(9715): onDraw
- 22:23:03.757: D/mDebug(9715): onDraw
很显然,onSizeChanged的启动时间在onDraw之前