The Setcontentview of 1.Android view and view drawing analysis notes

Source: Internet
Author: User

Since the advent of the first graphical user interface pc in 1983, almost all PC operating systems have supported visualization, and Android is no exception. For all Android developer, the control we touch most is view. In general, we use custom view, we need to know the most in addition to event distribution, is the view of the drawing process. However, about the drawing of the view, involved in the complexity of the knowledge points, so much code knowledge, to comb up, must first look for a head. So what is the most common method we use? Of course it is setContentView() !

Setcontentview

First we find an activity directly in Android Studio (note that this article analyzes the activity, if you are looking at appcompatactivity, the actual code will be the same), then find the SetContent method and then click In, We can see

public void Setcontentview (@LayoutRes int layoutresid) {
GetWindow (). Setcontentview (LAYOUTRESID);
···
}

Then find the getWindow() method

Private Window Mwindow;
Public Window GetWindow () {
return Mwindow;
}

It is known that the SetContent () method of the window class is called. Then the full class is searched for Mwindow, and an assignment statement is found in the Attach method.

final void attach (context context, Activitythread Athread,
Instrumentation instr, IBinder token, int ident,
Application application, Intent Intent, Activityinfo info,
Charsequence title, Activity parent, String ID,
Nonconfigurationinstances Lastnonconfigurationinstances,
Configuration config, String referrer, Ivoiceinteractor Voiceinteractor,
Window window) {
···
Mwindow = new Phonewindow (this, window);
Mwindow.setwindowcontrollercallback (this);
Mwindow.setcallback (this);
Mwindow.setonwindowdismissedcallback (this);
Mwindow.getlayoutinflater (). Setprivatefactory (this);
if (info.softinputmode! = WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {
Mwindow.setsoftinputmode (Info.softinputmode);
}
if (info.uioptions! = 0) {
Mwindow.setuioptions (info.uioptions);
}
···
Mwindow.setwindowmanager (
(WindowManager) Context.getsystemservice (Context.window_service),
Mtoken, mcomponent.flattentostring (),
(Info.flags & activityinfo.flag_hardware_accelerated)! = 0);
if (mparent! = null) {
Mwindow.setcontainer (Mparent.getwindow ());
}
Mwindowmanager = Mwindow.getwindowmanager ();
···
}

And then it leads us to the phonewindow of our main analysis today.

Phonewindow

To find the Setcontentview method for Phonewindow, you can see that there are three overloaded methods.

@Override
public void Setcontentview (int layoutresid) {
if (mcontentparent = = null) {
Installdecor ();
} else if (!hasfeature (feature_content_transitions)) {
Mcontentparent.removeallviews ();
}

if (Hasfeature (feature_content_transitions)) {
Final Scene newscene = Scene.getsceneforlayout (mcontentparent, Layoutresid,
GetContext ());
Transitionto (Newscene);
} else {
Mlayoutinflater.inflate (Layoutresid, mcontentparent);
}
Mcontentparent.requestapplyinsets ();
Final Callback cb = Getcallback ();
if (CB! = null &&!isdestroyed ()) {
Cb.oncontentchanged ();
}
Mcontentparentexplicitlyset = true;
}

@Override
public void Setcontentview (view view) {
Setcontentview (view, New Viewgroup.layoutparams (Match_parent, match_parent));
}

@Override
public void Setcontentview (view view, Viewgroup.layoutparams params) {
if (mcontentparent = = null) {
Installdecor ();
} else if (!hasfeature (feature_content_transitions)) {
Mcontentparent.removeallviews ();
}

if (Hasfeature (feature_content_transitions)) {
View.setlayoutparams (params);
Final scene newscene = new Scene (mcontentparent, view);
Transitionto (Newscene);
} else {
Mcontentparent.addview (view, params);
}
Mcontentparent.requestapplyinsets ();
Final Callback cb = Getcallback ();
if (CB! = null &&!isdestroyed ()) {
Cb.oncontentchanged ();
}
Mcontentparentexplicitlyset = true;
}

Three methods The overall process is:

    1. Initialize Decorview
    2. Check and process transition animations
    3. Add the layout or view that you actually want to display to the Mcontentparent
    4. Notifies callback (that is, activity) to call the Oncontentchanged method
Decorview

In the above process, relatively important is the first step of the initialization of Decorview, which is the installDecor() method, below we continue to analyze this method.

 private void Installdecor () {

Mdecor = Generatedecor ( -1);

} else {
Mdecor.setwindow (this);
Mcontentparent = Generatelayout (Mdecor);


}
/span>

In the method, the main thing is to installDecor() do two things, one is Generatedecor, generate Mdecor, there is a generatelayout, generate mcontentparent. Generatedecor, there is not much complex logic, is to make some judgments, and then instantiate a Decorview, One thing to say here is that before Android7.0, Decorview is Phonewindow's inner class, and after 7.0, Decorview is presented as a separate class, so if it is useful to reflection, there may be problems, which need to be done in version judgment. Then we look at the logic of Generatelayout:

Protected ViewGroup generatelayout (Decorview decor) {
Get style from System
TypedArray a = Getwindowstyle ();
Then a series of judgments, get the attributes in the style, and then set the features
such as whether it is suspended, whether there is a title, etc.
···
int Layoutresource;
int features = Getlocalfeatures ();
Get the features you just set up, make a series of if eles judgments, find the corresponding ResourceID
Then call Decorview's onresourcesloaded to inflate the layout.
···
Mdecor.startchanging ();
Mdecor.onresourcesloaded (Mlayoutinflater, Layoutresource);

Find the corresponding mcontentparent by com.android.internal.r.id.content
ViewGroup contentparent = (viewgroup) Findviewbyid (id_android_content);
if (contentparent = = null) {
throw new RuntimeException ("window couldn ' t find Content container view");
}
After a series of subsequent settings
···
Mdecor.finishchanging ();
Finally return to Contentparent
return contentparent;
}

In Generatelayout, the approximate process is like this.

    1. Get style first
    2. Set the style to features in window
    3. Get features, judge the corresponding ResourceID.
    4. A viewgroup is added to the Mdecor by Resourceid,inflate.
    5. Again through the Findviewbyid, find just inflate viewgroup in the com.android.internal.r.id.content, as Mcontentparent
    6. After a series of settings
    7. Back to Contentparent
Summary

To this, our setcontentview has basically gone, the rest is waiting for activity, WindowManager, Windowmanagerglobal, Viewrootimpl to call, these classes of calls, Involves the activation process of the activity, and we'll analyze the process in detail in other notes.

A sequence diagram of the entire setcontentview is then used to solidify the process just now.

Timing diagram for Android Setcontentview

Series Articles

Android View and view drawing analysis notes Setcontentview
Onmeasure of view Drawing analysis notes
OnLayout of view Drawing analysis notes
OnDraw of view Drawing analysis notes

The Setcontentview of 1.Android view and view drawing analysis notes

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.