Go deep into Android [7]-resource files

Source: Internet
Author: User
Tags integer numbers

Resource file

As a coder, the interface is often a dream. Most of the time, we will feel that we have a strong grasp of the underlying logic implementation. Even if the demand is constantly changing, we can continue to follow up through continuous refactoring to keep everything under control. However, this is often not the case when you encounter an interface. Its quality is always tied to words such as aesthetics and experience. In the years when Fengjie Furong went out, it became a terrible thing to talk about aesthetics. You may be asked to keep changingCodeTo move a pixel and adjust a button, it is trivial and boring.
To change this situation and save the coder from being hot in water, many development platforms adopt solutions similar to resource files. The basic idea of this kind of solution is to completely separate the implementation of the interface from the implementation of the underlying logic, and describe the interface using the resource source file. The description language of a resource file is often structured, such as HTML, XML (and its deformation. Compared with development languages, these languages have weak logic, better structure, better readability, and easier understanding. They are also very friendly to automation tools and can be combined with drag-and-drop configurations on the interface. Such a strip can be independent of the underlying logic and the upper-layer interface, or even different personnel development (This point should be obvious in Web development ...), the coupling between the two is very small, and the coder's burden is suddenly reduced (well, a very frustrated resource architecture will also increase the burden on developers, Symbian students, please do not check the number for your seat ...).

The structure and format of Android resource files are Directory structure , XML files , And Pure Data File . From the perspective of format, there is no doubt that the learning threshold is very low. XML is the Swiss army knife for coder. Even if you are not used to it, it is no problem to make it clear and use it. From the perspective of supporting tools, Android ADT provides a set of visual configuration tools, which are not particularly useful, but at least unsatisfactory and can be used together, it's okay to play Symbian. [strong ad premiere: youdao dictionary for the new iPhone version is on the go. It adds the powerful word book feature. The unique electric shock trembling word Switch feature makes you want to stop it, do not hesitate to hold the shoes of the relevant equipment.
Android resource file, Wide coverage As long as it is related to the interface, it can be represented by resource source files, such as UI styles, menus, configuration files, various descriptive strings, images, audio and video files, animations, and colors, dimensions, styles and styles, etc. All resource files (excluding asset, which are not associated with the discussion...) are stored in Res Directory, different types of resources need to be placed in different Subfolders with specific names Or in a file with a specific file name (or MS is not a must, but you do not have to stand on your own here, it is also good to look for a seal ...). For example, all images, such as the UI background, need to be thrown in Drawable All strings in these folders will be placed in Values Directory as shown in Strings. xml Such a file (as shown in, is a resource file directory structure ...).

Each XML file has certain conventions. For example, a string is placed in an XML element such as <string> </string> (as shown in ...), you can use the ADT plug-in provided by eclipse to fill in the interface without paying attention to specific specifications. You can also build it directly. The former is more intuitive for beginners, and the latter is faster for laruence. ConfigurabilityProgramThe logic is always the same, but the interface often needs to be able to change 72. The first change factor isStatus. Imagine that we often have such a similar requirement. A button is a background when we don't need to press it. In the process of pressing the button, the image is changed to another one, when it is available, it needs a fresh and bright look. When it is not available, it is best to leave it in gray and easy autumn. In the traditional programming model (Symbian, brother called you out as a model...), we always need to use code to control such a thing. Listen to different events, switch the background, and pray to heaven, do not let the elder brother adjust, otherwise the elder brother and you are not finished.


In Android, it is much simpler to do this. XML attributes . As shown in, it is the background of the radio button. By combining different attributes, You can automatically convert the background. For example, the first <item> indicates that when the radio button is selected and has a focus, btn_radio_on is displayed, and the last <item>, if none of the preceding conditions are met and selected, the image btn_radio_on is displayed. Another more variable factor is Mobile phone hardware/software environment After all, not every home is an apple. A mobile phone is built on a single platform and the mobile phone model is diversified, which is almost unavoidable. No one wants their own software to shine brightly on the big screen mobile phone, but it will be terrible to change a small screen. The vertical screen looks like this is the case, and the horizontal screen looks like it will be crowded. There is Language Environment As a coder with an international vision, making world-oriented Nb software is our dream, but we cannot force everyone to learn Chinese because of our dream, A software can select the most suitable language to display based on the language environment of the mobile phone. In many cases, it is a required function.
In Android, implementation of these operations is all a little effort. The method is to put environment-related resources into a folder with a specific name. For example, the resource that represents the simplified Chinese character information can be placed in Values-ZH-RCN When the system language environment is simplified Chinese, the Chinese character information is displayed. In Android, many related configuration items can be involved in resource adaptive activities in this way, including screen size, screen orientation, screen resolution, language environment, and touch screen type, SDK version. The system will give all configuration items Priority (Or weight, order, and so on). When a user provides multiple resources, the system increases the priority from the top to the bottom. Remove alternative resources If only One That is the resource item that best matches the current system hardware and software language environment. If No , Select enable Default item (Like values, there is no resource in the tail directory ...). Therefore, the default resource is very important and must be of all other optional resource items. Superset Otherwise, the application will crash in the case of resource selection failure. For more information about resource configuration and selection, see: Guide/topics/resources/resources-i18n.html .

After the r class uses resources, the coupling between the interface logic and the underlying logic is reduced, but this does not mean that the two are not associated. For example, if you need to add a click event for a button, you need to locate the desired button. For example, you need to use a string resource to notify the user of something, you need to be able to locate the string placed in the resource. The most obvious method is to compare strings and use Name Information Locate the required content in the XML description file of the resource, load and use it. This method solves the search problem, but repeated string comparison will inevitably bring serious efficiency risks. Therefore, in Android, an R class is introduced, similar to the Symbian method.

the basic idea is to add an additional compiler for all resource items , each parameter is assigned a 32-bit integer number to indicate that different configurations of the same resource image use the same ID. This integer number is equivalent to the house number of this resource item. It can help locate the corresponding resource item. All these integer numbers are integrated into a Java class in the form of constants. This class is the r class . In this way, you can use this R class in the program to find the required resources, which simplifies the string comparison into an integer number comparison, greatly saving the overhead.
I have to say that this entire logic is consistent with the resource file pre-compilation in Symbian. The difference between the two lies in the integer number in Symbian, which represents the offset of a binary stream. The content in the resource is determined during compilation. In Android, the integer number is a value with logical meaning . It expresses the resource package, category, and footer of the resource, its specific content is determined at runtime, which greatly enhances its flexibility , the cost is efficiency .
the implementation follows the Convention. The following uses a search process as an example. If you need to use a string in activity , the getstring method is called and r. stirng. an integer of XXX in exchange for a string that matches the current machine environment configuration. Getstring, tracing, to the assetmanager class. The asset class is actually an empty shell. It only provides some convenient interfaces, and requests are sent through the JNI interface , passed in to the class library implemented by underlying C ++ . The underlying implementation is mainly implemented in C ++, assetmanager, resourcetypes, and so on. Where:

    • The JNI file is in:Framework/base/CORE/JNI
    • The header file is in:Framework/base/include/utils
    • The CPP file is in:Framework/base/libs/utils
Specific implementation, and the aforementioned Algorithm The logic is consistent. ID of each resource, 32 bits, 8-bit high Indicates the resource package, 16-bit lower Used to describe the script, 8 digits in the middle To describe the category. Files in all resources are pre-processed and put into a series of queues and tables. The specific location can be found through the ID. Then, set the object based on the cache environment and run the elimination algorithm once to obtain the corresponding file and offset of the matched resource object. Then read the value and copy it back through the JNI interface. The preceding descriptions do not help you understand the actual implementation details. They are mainly used to give you a more intuitive understanding of the Efficiency of reading resources. The entire resource reading process is relatively long, but the implementation in C ++ can be expected, the efficiency is higher than Java, developers should be able to according to their own needs, decide whether to write the content into the resource file (or in the code ...), do you need to cache it yourself, and so on.

 

==========================================================

After applying for your own space and domain name, you will feel like you are at home. If you are interested, you can move to flyvenus.net to synchronously update the relevant content and provide a better reading experience.

==========================================================

 

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.