2.1Android View and ViewGroup

Source: Internet
Author: User

2.1.0 View and ViewGroup class relationship
Android View and ViewGroup in terms of composition architecture, it seems that ViewGroup is above View, View needs to inherit ViewGroup, but this is not actually the case. View is the base class, And ViewGroup is its subclass. This proves that View represents a piece of space that can be drawn by the user interface components. Each View occupies a rectangular area on the screen. In this area, this VIEW object is responsible for drawing and event processing. View is the parent class of widgets and ViewGroup. ViewGroup is the base class of Layout.


From the comparison of the above two figures, we can see that ViewGroup is actually a subclass of View. Therefore, ViewGroup also has the behavior characteristics of View, but because ViewGroup is the ancestor of Layout, so it has some other features that the View does not have. Generally, a View is created through XML or code. You need to pay attention to the following aspects for any View and its subclass widgets:
[1] Set attributes, such as length, width, and coloring. These attributes can be set using code or XML files. You can use these attributes to modify them during running.
ID attribute. Android requires that the ID name of each UI element be unique, but not absolute. At the same time, the same element name can be used in different Layout. Specify an ID for a UI element. You can find the ID in the code.

Tags, which is different from the ID, does not need to be searched for View. It is similar to saving some descriptive data of View.
Animation, which can be used to operate any View. Note: If a View has a child, the child also has the animation function.
Position, Size, padding and margins indicate the width and height of a View for any View. You can also set padding and margins. Not all views have margins.

Orientatiion: For the ViewGroup subclass Layout, set Orienatation to determine the position of the subclass.
FillModel: This is the default situation. Some elements cannot fully occupy the space of the parent region. In this case, unless the child VIEW has already set the specific and DPI, the parent control must be notified, you can select a space filling method, such as Fill-parent or Wrap-content.
Gravity, Gravity are different from Orientation. Gravaity is the same as the left alignment and the right alignment in a Word document. The default value is the upper left alignment.
Weight, which is used to allocate the remaining space at the same time. You need to set layout-weight to determine the proportion of the two.
[2] request focus, which can be converted by functions. Different focal points can implement different background transformations and other functions. Focus is divided into several situations in Android. One is that focus can be obtained, the other is that focus cannot be obtained, and the third is that focus can be obtained, but the current is in the touch state.
[3] Set event monitors. All views broadcast their own information when they change. Similar clicks, lost focus, etc. Generally, when an event comes, Android will pass the event to the corresponding View, and then the View will pass the event to the corresponding Listeners. In this case, the View needs to obtain the focus. If you need to re-draw the View, you need to call invalidate (0 or reqeustLayout to re-draw the entire interface.
[4] Set display and hide. You can also set scrolling for its content.
2.1.1 relationship between View, Window, Activity, and Dispay
These are key elements that constitute the display of the Android system. First, let's get to know about Dispay. Dispay indicates the hardware display screen information.

Through these functions, you can learn about the width, height, and resolution of a screen, as well as some basic information such as horizontal screen and Fixed Screen. Through these functions, when developing an application, we can easily obtain the screen size of the application that is currently installed, so as to adjust the application to provide a better user experience. Next, let's look at the relationship between the other three. Although you have read the previous View introduction and the basic introduction of The Link UI in the SDK, you are still very confused about the Android graphic window, the API also contains the WindowMangaer interface and Window class, but the description does not mention how to use these. But in fact, we need to see the Android core. The Android core bottom-layer GDI is SKIA, which is the same as chrome, but the GUI is different. Android implements the C/S mode. As shown in

The general display process is as follows:
[1] ActivityManagerService creates an Activity thread and activates an activity
[2] The system calls Instrumentation. newActivity to create an activity.
[3] after an Activity is created, attach it to a newly created phonewindow. In this way, the Activity obtains a unique WindowManager service instance.
[4] setcontentView is used to set the user UI during Activity creation. These views are added to the ContentParent of PhoneWindow.
[5] The Activity thread continues to execute. When Activity. makeVisible is executed, the root view DecoView is added to WindowManger. The WindowManger plenary session creates the corresponding ViewRoot for each DecoView.
[6] Each ViewRoot has a Surface, and each Surface will call the underlying library to create the memory space for drawing. This underlying library is SurfaceFlinger. SurfaceFlinger is also responsible for combining a View image (based on the Z axis) to display it to the user screen.
[7] If the user draws the image directly on the Canvas, it actually operates the Surface directly. However, for each View change, it is to notify ViewRoot, and then ViewRoot gets the Canvas. If the painting is complete, surfaceFlinger will be notified and merged into a Surface to the device screen.
From the analysis of the image output process, we can know that the actual display of the image is not related to the Activity. It is entirely determined by WindowManager. WindowManager is a system service. Therefore, you can directly call this service to create an interface. In addition, Dialog and Menu are managed by WindowManager. We can also see that the bottom layer is the Surface. Therefore, we recommend that you use SurfaceView to create interfaces for common game developers.

Author: "palm wine"

Related Article

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.