程序里调用了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"); } @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); } }
输出如下:
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之前