In-depth analysis of the android UI framework

Source: Internet
Author: User

The UI is the face of every application that interacts with the user. A good face is not just to be beautiful but to be human. It is best to have a good meal, but to have a strong temperament and be gentle and patient.

For developers, forging such a face requires not only superb skills, but also powerful tools and materials worthy of the party. As the saying goes, yunmu cannot be carved. Furong is not made in a single day. It is not a platform that can be called a special book. There is a set of easy-to-use UI frameworks. for developers, it is like the dew in the desert. If they encounter the cup UI kit, the whole interface development is like a nightmare.

The core of the android UI framework is the resource and layout system. Then, through the complete control library and concise interface design, we can further help developers, you can build your own interface as quickly as possible (hear this, Symbian started to drill into the ground ...).

UI control

The UI is sometimes like building blocks. In Android, the most atomic block is view. All other UI elements are derived from this class.

I also stole a picture from the SDK to describe the structure of the android UI control. In every window, this is a standard and complete tree structure. View has a subclass viewgroup, which is equivalent to a container class or a composite control. All subclasses derived from and viewgroup can assume the responsibility of the parent node in this UI tree, however, if viewgroup is bypassed to pass through the view, the layout can only be in the leaf node category.

As shown in, assume that you have created an interface that is structured as a dotted line box. By inserting the interface into the activity using the setcontentview method of the activity, a logical relationship of the graph is formed. Every activity contains a window object, which indicates the interface logic on a top-level whole screen. In the android source code, the implementation is midwindow, which contains a framelayout object, which is the kind of interface with a title. A bunch of custom controls will be inserted into the window interface. In the activity, the processing logic of all events is enjoyed by the window first, and no consumption is left by the controls.

At the top of the entire control tree is a logical tree, viewparent. The implementation in the source code is viewroot. It is the translator of event information between the entire control tree and windowmanager. Windowmanager is an important service in Android. It translates user operations into commands and sends them to various windows displayed on the interface. Activity will register the top-level controls to windowmanager. When the user really touches the screen or keyboard, windowmanager will notify you, and when the control has some requests, it will also be sent back to windowmanager via viewparent. To complete the entire communication process.

This is a standard control tree, because the parent control has absolute control over the child control, and the footprint and location of each child control are allocated based on the parent control, events that can be accepted and processed are also distributed by the parent control. This structure is widely recognized by many platforms and frameworks. Compared with the traditional win development and the Symbian of the cup, although the event propagation channel is longer, many operations are less efficient, however, the entire structure is more hierarchical. Each control only needs multiple parent controls to be in charge of sub-controls, with clear responsibilities and simple logic, facilitating development and design.

When talking about controls on any platform, there are inevitable theme, such as how each control identifies, how to set the size and location, how to accept and process events, and how to draw.

Identifier

In Android, you can set an ID for each control. The global uniqueness of this ID does not need to be guaranteed, but it is recognizable within a certain local range, in this way, you can find this control through this ID (if you do not need to find it, do not set it ...).

However, the find comparison in the parent control step by step, find the control that matches the ID, and then perform the transformation, it is a relatively heavy operation, so Android hides another property for the control, Tag. It accepts data of any object type. You can heap the content related to this control object. For example, in the list, we often encapsulate all the control elements related to each list item into an object and threw them into the tag, so we do not need to compare the ID every time for searching, more efficient and quick.

Dimensions

In Android, the most important size attribute of a control is width/height. developers can specify the size of the control and set it to fill_parent and wrap_content. You can measure and set the widget position in two steps.

The first step is measure. It is used to pass in the width/Height Information of the control. The control calculates the actually needed width/height based on its own parameters. Then, it calls the setmeasureddimension method and caches it as a member variable for future use.

After calculating the size, layout is another step. In this process, the parent control calculates the positions of each child control on it to complete the entire process of determining the size and position. The entire measure and layout processes are promoted from top to bottom from the top of the tree to the leaves.

When developers need custom controls, they may need to pay attention to these items. By reloading the onmeasure and onlayout methods, they can define their own control measurement methods.

Event

In Android, all buttons, touch screens, and other events are distributed from top to bottom. Each viewgroup object maintains a focused variable, which indicates that the focus control is available in this parent control. When a key time occurs, the focused subcontrol is found, and pass it. Similarly, the distribution of touch-screen events is similar, but it has nothing to do with focus. The parent control traverses all child controls to see who is in the touch position and then transmits it to whom.

There are also some events that are not initiated logically from top to bottom. For example, if you modify the content of a sub-control so that the size and content of the sub-control change, you need to re-paint the control, these operations are not only the sub-controls, but also all the controls on the entire control tree. In Android, a sub-control is used to maintain a viewparent object. This object represents the manager of the entire control tree. When a sub-control generates an event that affects the entire control tree, viewparent will be notified, and viewparent will convert it into a top-down event and distribute it.

The event processing logic of Android adopts the observer mode. The android control provides the Add/set listener interface for some columns, allowing external observers to process control events. For example, if you need to do something when a button is clicked, You need to derive a view. the onclicklistener object acts as the observer and calls the setonclicklistener interface of the control to register it. When the button is clicked, you will be given the opportunity to process the click event. Of course, sometimes the logic you need to deal with is more complex. Simply standing outside and shouting cannot solve the problem, you may need to derive a control and reload event processing functions such as onxxxx, for more complete control.

Focus

For a non-touch-screen machine, maintaining focus is an extremely important thing. In the age of touch-screen machines, the focus still needs to be properly protected despite its decline.

In Android, focus is managed in the unit of Control tree. For each control, you can set the focus object to be transferred from top to bottom to left. When a focus transfer event occurs on a control, Android will jump from top to bottom to the correct control based on the set focus transfer logic. Compared with Symbian, it's true...

Layout

Layout is a special type of viewgroup controls. They do not have any display content, such as transparent glass boxes. The only reason for survival is the internal structure, better place its child controls.

For example, linear layout and linearlayout. The child control placed in this layout is arranged horizontally or vertically, and one is arranged in order. Tablelayout: You can place child controls one by one in the form of tables. Relativelayout is more flexible and allows you to set the alignment and arrangement relationships between controls. It is suitable for customizing complex interfaces.

With layout, controls and controls are no longer separated, but are more organically combined and easier to set. It is easier to maintain the relationship between various controls than Symbian.

More

For the complete answer to these questions, see the view page in the SDK:/reference/Android/View/view.html.

Http://developer.android.com/guide/topics/ui/index.html

Implementation

With these awareness of Android UI controls, you can see more comprehensive implementation details, that is, the UI implementation of the activity.

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.