在看鸿洋的博客(http://blog.csdn.net/lmj623565791/article/details/43649913)的时候,在做自定义控件的时候,发现了onSizeChanged(),与dispatchDraw()等方法,所以想了解一下,他们的顺序,所以就写了一段代码,做了一个测试,这里做一个记录.
代码:
public class TestView extends View { public TestView(Context context) { super(context); Log.d("myTestView", "TestView context"); } public TestView(Context context, AttributeSet attrs) { super(context, attrs); Log.d("myTestView", "TestView context, attrs="+attrs.getAttributeValue(0)); } public TestView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); Log.d("myTestView", "TestView context,attrs,defStyle attrs="+attrs.getAttributeValue(0)); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Log.d("myTestView", "onDraw"); } @Override protected void onFinishInflate() { super.onFinishInflate(); Log.d("myTestView", "onFinishInflate"); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); Log.d("myTestView", "onSizeChanged,w="+w+",h="+h+",oldw="+oldw+",oldh="+oldh); } }结果:
20:13:23.287: D/myTestView(9715): TestView context, attrs=@2131034211 20:13:23.297: D/myTestView(9715): onFinishInflate 20:13:23.367: D/myTestView(9715): onSizeChanged,w=540,h=960,oldw=0,oldh=0 20:13:23.427: D/myTestView(9715): onDraw 20:13:23.457: D/myTestView(9715): onDraw