Android and androidadt
Android obtains the height and width of the mobile phone screen. We know that the value obtained in the onCreate method is 0. Some people say that the value can be obtained in the onClick method. This is also a method, however, the onWindowFocusChanged method can be directly obtained, and sometimes the scenario requires that the value be obtained immediately after the Activity rendering is complete. At this time, the onWindowFocusChanged () method will use callback, similar to PopupWindow display, if executed in the onCreate method, an exception without a token is reported because the Activity is not rendered successfully.
OnWindowFocusChanged (): This method is called back when the current Window of the Activity gets or loses focus. When this method is called back, it indicates that the Activity is completely visible to the user (only visible, but also a piece of black call, to be draw ..). This method is called when the dialog box pops up/disappears and the Activity is newly created and rolled back.
In contrast, the onResume () method means that the Activity enters the visible state, but only the State, not the real interface build.
Demo
Only one TestImageView is displayed for an Activity, and the onMeasure (), onLayout (), and onDraw () methods under TestImageView are overwritten, And the onGlobalLayout () Listener is set for the Activity, observe the call sequence of these methods and the onResume () and onWindowFocusChanged () of the Activity as follows:
ClipDrawableActivity onCreate() time=1376920332125 ClipDrawableActivity onResume() TestImageView.width=0 time=1376920332149 MainActivity onWindowFocusChanged hasFocus=false ClipDrawableActivity TestImageView onMeasure() ClipDrawableActivity TestImageView onMeasure() ClipDrawableActivity TestImageView onMeasure() ClipDrawableActivity TestImageView onMeasure() ClipDrawableActivity TestImageView onLayout() ClipDrawableActivity onGlobalLayout() TestImageView.width=736 time=1376920332185 ClipDrawableActivity onWindowFocusChanged() hasFocus=true TestImageView.width=736 time=1376920332187 ClipDrawableActivity TestImageView onMeasure() ClipDrawableActivity TestImageView onMeasure() ClipDrawableActivity TestImageView onLayout() ClipDrawableActivity onGlobalLayout() TestImageView.width=736 time=1376920332194 ClipDrawableActivity TestImageView onDraw()
Activity. oncreate () → Activity. onResume () → TestImageView. onMeasure () → TestImageView. onLayout () → onGlobalLayoutListener () → Activity. onWidnowFocusChanged () → ..... → TextImageView. onDraw ()
Summary
So far, if you need to obtain the width and height of a View, you can directly obtain it at onWindowFocusChanged (). This is more convenient than setting onGlobalLayoutListener for a view. For example, if you want to read the local file record to determine whether a text image is prompted on the page for the first time, you can also read the file in this method and then display it on the ui.
I am the dividing line of tiantiao