7、onDraw ( 只有可见的 View 才在 window 中绘制 )
在代码中构造View:
setContentView(new CusView(this))输入日志信息如下:
03-25 20:37:51.284: E/yyyyy(12530): construct 1 parameter 03-25 20:37:51.294: D/yyyyy(12530): onVisibilityChanged--------===== 03-25 20:37:51.314: D/yyyyy(12530): onVisibilityChanged--------===== 03-25 20:37:51.314: D/yyyyy(12530): onRtlPropertiesChanged--------===== 03-25 20:37:51.314: D/yyyyy(12530): onRtlPropertiesChanged--------===== 03-25 20:37:51.314: E/yyyyy(12530): onAttachedToWindow 03-25 20:37:51.314: D/yyyyy(12530): onWindowVisibilityChanged--------===== 03-25 20:37:51.314: D/yyyyy(12530): onMeasure , width : 1080 ; height: 1557 03-25 20:37:51.314: D/yyyyy(12530): onSizeChanged 03-25 20:37:51.324: I/yyyyy(12530): onLayout --> l: 0 ; r : 1080 ; t: 0 ; b: 1557 : changed :true 03-25 20:37:51.324: D/yyyyy(12530): onMeasure , width : 1080 ; height: 1557 03-25 20:37:51.324: I/yyyyy(12530): onLayout --> l: 0 ; r : 1080 ; t: 0 ; b: 1557 : changed :false 03-25 20:37:51.324: D/yyyyy(12530): onDraw--------===== 03-25 20:37:51.344: D/yyyyy(12530): onWindowFocusChanged--------=====从测试结果来看,默认情况下view的长和宽默认和父 view 的长和宽一致 。
虽然调用了onDraw 函数,但是在屏幕上却看不到任何内容,什么原因?
当看不到任何内容时,请先检查 View要绘制的内容是否制定。
为什么我指定了LayoutParameters,却没有效果?
在不恰当的生命周期中指定LayoutParameters,会被忽略掉,比如如下代码:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.activity_main); view = new CusView(this); view.setImageResource(R.drawable.ic_launcher); FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(70, 70); view.setLayoutParams(params); setContentView(view); }正确的方法应该是放到 onWindowFocusChanged 方法获取到焦点后再指定LayoutParameters,如下代码:
@Override public void onWindowFocusChanged(boolean hasFocus) { // TODO Auto-generated method stub super.onWindowFocusChanged(hasFocus); if (hasFocus) { view.setImageResource(R.drawable.ic_launcher); FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(70, 70); view.setLayoutParams(params); } }
为什么我指定LayoutParameters参数时报异常?异常信息如下:
↳ android.view.ViewGroup.LayoutParams
↳ android.view.ViewGroup.MarginLayoutParams
LayoutParameters的参数类型不对,从上面继承关系可以看到MarginLayoutParameters扩展了ViewGroup的layoutParameters ,将其修改为任意支持margin动作的LayoutParameters 。
接下来我们看三份销毁 View 的日志:
android:visibility=visible
03-25 21:15:35.404: D/yyyyy(14589): onWindowFocusChanged--------===== 03-25 21:15:35.484: D/yyyyy(14589): onWindowVisibilityChanged--------===== 03-25 21:15:35.504: D/yyyyy(14589): onDetachedFromWindow--------=====android:visibility=gone
03-25 21:16:09.964: D/yyyyy(14736): onWindowFocusChanged--------===== 03-25 21:16:10.054: D/yyyyy(14736): onWindowVisibilityChanged--------===== 03-25 21:16:10.064: D/yyyyy(14736): onDetachedFromWindow--------=====android:visibility=invisible
03-25 21:16:42.534: D/yyyyy(14860): onWindowFocusChanged--------===== 03-25 21:16:42.594: D/yyyyy(14860): onWindowVisibilityChanged--------===== 03-25 21:16:42.614: D/yyyyy(14860): onDetachedFromWindow--------=====
从以上内容可以看到,visibility属性对view的销毁流程没有影响。
综上所述:View 的关键生命周期为 [改变可见性] --> 构造View --> onFinishInflate --> onAttachedToWindow --> onMeasure --> onSizeChanged --> onLayout --> onDraw --> onDetackedFromWindow