Android and androidadt

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.