In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to display the interface of Activity". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to display the interface of Activity"!
Animation display
In order to facilitate your understanding, let's first show you the relationship between these people in the form of animation:
Source code parsing
From the birth of love when I was young
Before the Activity interface was displayed, it was still an Activity that we couldn't see, so I'll give it a nickname-Xiao'ai.
How was little love born? Anyone familiar with the Activity startup process knows that Xiao Ai's creation takes place in performLaunchActivity:
/ / ActivityThread.java private Activity performLaunchActivity (ActivityClientRecord r, Intent customIntent) {/ / create ContextImpl ContextImpl appContext = createBaseContextForActivity (r); Activity activity = null; try {java.lang.ClassLoader cl = appContext.getClassLoader (); / / create Activity activity = mInstrumentation.newActivity (cl, component.getClassName (), r.intent) } try {if (activity! = null) {/ / initialize some important data of activity activity.attach (appContext, this, getInstrumentation (), r.token, r.ident, app, r.intent, r.activityInfo, title, r.parent, r.embeddedID, r.lastNonConfigurationInstances, config R.referrer, r.voiceInteractor, window, r.configCallback, r.assistToken) / / call activity's onCreate method if (r.isPersistable ()) {mInstrumentation.callActivityOnCreate (activity, r.state, r.persistentState);} else {mInstrumentation.callActivityOnCreate (activity, r.state);} return activity;}
In this process, three main things have been done:
Activity is instantiated
The attach method is called for initialization
Call the onCreate method to load the layout from the layout file and prepare for the View display.
Find a helper for Xiao Ai to interact with View (PhoneWindow)
As we all know, Xiao Ai was busy after she was founded, so she certainly couldn't manage every View himself, so he found a helper to help her interact with View and manage View.
(decoupling of Activity and View)
What is this helper? This is the window Window, that is, the implementation class PhoneWindow.
This process occurs in the attach method:
/ / Activity.java final void attach () {/ / create PhoneWindow mWindow = new PhoneWindow (this, window, activityConfigCallback); mWindow.setCallback (this); mWindow.setWindowManager ((WindowManager) context.getSystemService (Context.WINDOW_SERVICE), mToken, mComponent.flattenToString (), (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED)! = 0);}
To make it easier to remember, we call this PhoneWindow housekeeper window housekeeper.
Load layout file (DecorView)
With the window housekeeper, you can continue with the onCreate method, the most important of which is the setContentView method in the onCreate method.
Through setContentView, you can load the View in the layout file.
As mentioned earlier, the management of View is left to the window butler, so it is directly called to the setContentView method of PhoneWindow:
/ / Activity.java public void setContentView (@ LayoutRes int layoutResID) {getWindow () .setContentView (layoutResID); initWindowDecorActionBar ();}
Then the work of loading the layout file begins.
But considering one thing, Activity has different themes, and different themes have different layout structures. So we have to set a top-level View as the boss of all View before loading the layout file we set up.
And this top-level View is DecorView, for convenience, I call him the top little brother, referred to as the little brother.
Take a look at how my brother DecorView was created:
/ / PhoneWindow.java @ Override public void setContentView (int layoutResID) {if (mContentParent = = null) {installDecor ();} if (! hasFeature (FEATURE_CONTENT_TRANSITIONS)) {mLayoutInflater.inflate (layoutResID, mContentParent);}} private void installDecor () {if (mDecor = = null) {mDecor = generateDecor (- 1) } else {mDecor.setWindow (this);} if (mContentParent = = null) {mContentParent = generateLayout (mDecor);}} protected DecorView generateDecor (int featureId) {return new DecorView (context, featureId, this, getAttributes ());}
In this way, the younger brother DecorView was created, and then it was time for the younger brother to work.
As mentioned above, what did little brother DecorView do when he was created?
To set up different layout structures according to different themes, this work takes place in the generateLayout method, which we will not analyze today.
Looks like the little brother's work is done, too?
Wait, wait, wait.
Going back to the setContentView method above, after calling the installDecor method to create the younger brother, I did one more thing:
/ / load xml layout files mLayoutInflater.inflate (layoutResID, mContentParent); public View inflate (@ LayoutRes int resource, @ Nullable ViewGroup root, boolean attachToRoot) {final Resources res = getContext () .getResources (); final XmlResourceParser parser = res.getLayout (resource); try {return inflate (parser, root, attachToRoot);} finally {parser.close ();}}
And this inflate is the well-known method of loading layout files. Pass in the xml layout file, parse and combine the parent view--mContentParent we passed in, transform it into a complete tree structure, and finally return the top-level View.
At this point, setContentView's work is done.
To put it simply, we created a younger brother DecorView and combined this top-level view with our incoming xml layout file to generate a multi-tier View.
Shows this View (ViewRootImpl)
View is in place, and the structure is settled. The next step is how to display this View structure so that our phones can show the picture?
That's right, it's drawing.
Who is better to do the drawing of View? Recall the current members:
Xiao Ai Activity: big boss, responsible for co-ordinating.
Window housekeeper PhoneWindow: responsible for managing each View.
DecorView: the top-level View is responsible for the layout of the theme.
It seems that there is no one in charge of View drawing? Drawing is so important that it is necessary to recruit another friend.
ViewRootImpl shines? Take the stage, for convenience, I call him Xiao Wei.
When was Xiao Wei founded?
Let's take a look at the calling process of Activity. After the onCreate call, the onResume method is called, which starts with the handleResumeActivity method.
@ Override public void handleResumeActivity () {/ / onResume final ActivityClientRecord r = performResumeActivity (token, finalStateRequest, reason); / / addView if (r.window = = null & &! a.mFinished & & willBeVisible) {r.window = r.activity.getWindow (); View decor = r.window.getDecorView (); ViewManager wm = a.getWindowManager () WindowManager.LayoutParams l = r.window.getAttributes () wm.addView (decor, l);}
This method mainly does two things:
Call the onResume method
Call the addView method of WM.
Xiao Wei doesn't seem to be out yet?
Continue to look at the addView method:
/ / WindowManagerGlobal.java public void addView () {synchronized (mLock) {root = new ViewRootImpl (view.getContext (), display); view.setLayoutParams (wparams); mViews.add (view); mRoots.add (root); mParams.add (wparams) Try {root.setView (view, wparams, panelParentView);} public ViewRootImpl (Context context, Display display) {mContext = context; mWindowSession = WindowManagerGlobal.getWindowSession (); mThread = Thread.currentThread ();}
Finally, Wei ViewRootImpl has been created, and in this ViewRootImpl, there are two variables worth watching:
MWindowSession . The type is IWindowSession, which is a Binder object for interprocess communication. Its implementation on the server side is Session, which can be used to complete the work related to WMS.
MThread . The thread variable is set to the current thread, that is, the thread when ViewRootImpl is instantiated. Generally, when different threads update UI, it will determine whether the current thread and mThread are equal, and if they are different, an exception will be thrown.
Next, call the setView method of ViewRootImpl, which is naturally the way Wei ViewRootImpl does things:
/ / ViewRootImpl.java public void setView () {synchronized (this) {/ / draw requestLayout () / / call WMS's addWindow method res = mWindowSession.addToDisplay (mWindow, mSeq, mWindowAttributes, getHostVisibility (), mDisplay.getDisplayId (), mWinFrame, mAttachInfo.mContentInsets, mAttachInfo.mStableInsets, mAttachInfo.mOutsets, mAttachInfo.mDisplayCutout, mInputChannel) / / set this (ViewRootImpl) to parent view.assignParent (this) of view (decorView);}}
There are three main functions:
Trigger drawing (including measurement, layout, drawing)
/ / ViewRootImpl.java @ Override public void requestLayout () {if (! mHandlingLayoutInLayoutRequest) {checkThread (); mLayoutRequested = true; scheduleTraversals ();}}-> scheduleTraversals ()-> performMeasure () performLayout () performDraw ()-> measure, layout, draw methods
Call the addWindow method of WMS through Binder
The addToDisplay method eventually WMS the addWindow method of the process, assigning Surface to the window, and this Surface is responsible for displaying the final interface and eventually drawing it to the screen.
Set ViewRootImpl to parent of decorView
After this setting, when the sub-view requests to draw (requestLayout), you can finally find the ViewRootImpl through the parent, and then ViewRootImpl will be responsible for all the View drawing. The whole calling process is:
/ / View.java public void requestLayout () {if (mParent! = null & &! mParent.isLayoutRequested ()) {mParent.requestLayout ();}} at this point, I believe you have a deeper understanding of "how to display the Activity interface". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.