Android Application resources-provide resources (3)

Source: Internet
Author: User

Use resources to provide the best device compatibility

To allow applications to support multiple device configurations, it is essential to always provide default resources for each type of resource used by the application.

For example, if an application supports several languages, it must always contain the values/directory without a language or regional qualifier. If all string files are stored in the directory with language and regional delimiters, the program crashes when the application runs on a device that does not support the language set provided by the application. However, as long as the default values/resources are provided, the application runs correctly (even if the user cannot understand the language set provided by the application, it is better than the program crash .)

Similarly, if different layout resources are provided based on the screen direction, you should also select a direction as the default layout. For example, do not provide horizontal and vertical layout Resources in the layout-land/and layout-port directories, but retain one of them as the default layout, for example, the layout/directory is used as the storage location for the default layout resources, while the layout-Port/directory stores the vertical layout resources.

It is important to provide default resources, not only because it allows applications to run on devices that are not expected, but also because of some configuration delimiters added to the new Android version, which are not supported by the old version. If a new resource qualifier is used, but the compatibility between the Code and the old Android version must be maintained, if the default resource is provided when the application runs on the old Android version, the application crashes because resources named with the new qualifier are not available to the application. For example, if minsdkversion is set to 4, and all trace resources are limited to the night mode (night and notnight delimiters are added after API Level 8 ), then the device at API Level 4 cannot access these descriptive resources, and the application crashes. In this case, the resource in notnight mode can be used as the default resource. Therefore, the notnight qualifier should be removed from the directory name of the resource that can be traced, so that the Resource Directory Name is as follows: drawable/and drawable-night /.

Therefore, to provide the best device compatibility, you should always provide default resources so that the application can obtain the resources correctly executed, and then use the configuration qualifier to create optional resources for special device configurations.

This rule applies only to one scenario. If the minsdkversion of an application is 4 or larger, you do not need to use the screen resolution qualifier to provide optional trace resources. Because, even if there is no default trace resource, android can find the most matched resource between the optional screen resolutions and scale the bitmap as needed. However, for all device types, to get the best experience, you should provide optional resources for the three types of resolutions. If minsdkversion is smaller than 4 (android1.5 or a lower version), note that the screen size, resolution, and appearance delimiters are not supported, therefore, additional compatibility processing is required for platforms of these versions.

Provide screen resource compatibility for android1.5

Android1.5 and earlier versions do not support the following configuration delimiters:

Resolution:

Ldpi, mdpi, hdpi, and nodpi

Screen Size:

Small, normal, large

Screen appearance:

Long, notlong

These configuration delimiters are introduced in android1.6, so android1.5 (API Level 3 and earlier versions do not support them. If you use these configuration delimiters and do not provide the corresponding default resources, the android1.5 device may use the aforementioned screen configuration to restrict the resources in any resource directory named, because it ignores these delimiters, and use it to find the first matching descriptive resource.

For example, if the application supports android1.5 and contains the trace resources (drawable-ldpi/, drawable-mdpi/, drawable-hdpi/) of each resolution type /), and does not contain the default trace Resource (drawable/), then android1.5 will use any trace resources in the Resource Directory, which will lead to unsatisfactory user interface.

Therefore, when using the screen configuration qualifier, it is necessary to provide compatibility with android1.5 and earlier versions:

1. Medium Resolution, normal screen size, and non-long screen are provided as default resources.

Because all android1.5 devices have medium resolutions, normal screen sizes, and non-long screens, these types of resources can be placed in the corresponding default Resource Directory. For example, put all the Middle-resolution trace resources in the drawable/directory (instead of drawable-mdpi/), and put the normal-size Resources in the corresponding default Resource Directory, and place the notlong resource in the corresponding default Resource Directory.

2. Make sure that the SDK version is R6 or later.

The SDK tool requires a version 6 or later because it contains a new packaging tool that automatically applies the resources in the corresponding version qualifier to any android1.0 without a resource directory. For example, the resolution qualifier is introduced in android1.6 (API Level 4). When the packaging tool encounters such a directory, it adds "V4" to the directory name ", to ensure that the old version does not use these resources (only resources in the qualifiers supported by API level 4 and later ). In this way, by placing medium-resolution resources in directories without mdpi delimiters, they can still be accessed by android1.5, this default resource is also used by any device that supports this resolution Qualifier and has a medium-resolution screen, because they match the most with the device.

Note: later versions of Android, such as the configuration qualifier not supported by the old version introduced in API Level 8. To provide the best compatibility, you should always include the default resources used by a group of applications for each type of resources.

How does Android find the best matching resource?

When a resource is requested, Android selects an optional resource at runtime based on the current device configuration. To demonstrate how Android selects available resources, assume that each of the following trace directories contains different versions of the same image:

Drawable/

Drawable-en/

Drawable-fr-RCA/

Drawable-en-Port/

Drawable-en-notouch-12key/

Drawable-Port-ldpi/

Drawable-port-notouch-12key/

Assume that the device configuration is as follows:

Region = en-GB

Screen direction = port

Screen pixel resolution = hdpi

Touch screen type = notouch

Main text input method = 12key

Select a valid Resource: drawable-en-port by comparing the device configurations.

The system uses the following logic to determine the resources to use:

1. Exclude conflicting device configuration resource files.

Drawable-fr-RCA/directory is excluded because it conflicts with En-GB

Drawable/

Drawable-en/

Drawable-fr-RCA/

Drawable-en-Port/

Drawable-en-notouch-12key/

Drawable-Port-ldpi/

Drawable-port-notouch-12key/

Exception: even conflicting screen pixel resolution delimiters are not excluded. Even if the device's screen resolution hdpi, drawable-Port-ldpi/is not excluded because at this point in time, each resolution is considered a match.

2. Select the delimiter with the highest priority in List 2. (Start from MCC and move down ).

3. Does any resource directory contain this qualifier?

If no, the system returns Step 1 and continues to compare the next qualifier (in this example, the system returns to "no" until the language qualifier is compared ").

If "yes" is returned, proceed to step 4.

4. Remove the directories whose names do not contain this qualifier. In this example, the system will exclude all directories that do not contain the language qualifier:

Drawable/

Drawable-en/

Drawable-en-Port/

Drawable-en-notouch-12key/

Drawable-Port-ldpi/

Drawable-port-notouch-12key/

Exception: If the qualifier in the question is the screen pixel resolution, Android will select the option closest to the screen resolution of the device. Generally, Android provides the ability to scale down a large original image.

5. Return. Repeat steps 2, 3, and 4 until the last directory is left. In this example, the screen direction is the next qualifier to be matched, and the Resource Directory that does not specify the screen direction will be excluded:

Drawable-en/

Drawable-en-Port/

Drawable-en-notouch-12key/

Figure 2. flowchart of how Android finds the best matching resource

 

The last directory is drawable-en-port.

Although every requested resource needs to execute this process, the system has made some optimizations in some aspects. One of these optimizations is that once you understand the device configurations, You can exclude optional resources that cannot be matched. For example, if the configuration language is English ("en"), any resource directories that do not need to be set as English language delimiters will not be included in the checked resource pool.

When you select a resource based on the screen size qualifier, if there is no better match for the resource, the system will use the resource designed for the screen that is smaller than the current screen. For example, if needed, a large screen will use resources of a normal screen. However, if only resources larger than the current screen are available, the system will not use them, and if no other resources are available, the application will crash. For example, if all layout resources are for the xlarge qualifier, but the device does have a normal screen size, the application will crash.

Note: the priority of a qualifier (in table 2) is more important than the number of qualifiers that exactly match the device. For example, in step 4 above, the last selected list contains three fully-matched delimiters (Direction, touch screen type, and input method ), drawable-en matches only one parameter (language. But because the language priority is higher than other delimiters, so the drawable-port-notouche-12key is excluded.

Known issues

Andrid1.5 and 1.6: Version delimiters must be exactly matched, rather than the best match.

The correct action is to match the system with the resource marked with a version qualifier equal to or less than the platform version of the device. However, in android1.5 and 1.6 (API Level 3 and 4), because of a bug, the system can only use resources that fully match the device version and the marked version qualifier.

Solution: Provide vulgar resources according to such behaviors. However, this bug is fixed in Versions later than Android 1.6. Therefore, if you need to differentiate resources between Android 1.6, and later versions, you only need to use a resource with a version qualifier of 1.6 and a resource that matches all subsequent versions. In this way, the problem will not be significantly affected.

For example, if you want to use different tracing resources on android1.5, 1.6, and 2.0.1 (and later versions), create three tracing directories: drawable/(for version 1.5 and earlier), drawable-v4 (for version 1.6),; and drawable-v6 (for version 2.0.1 and later-Version 2.0, that is, V5 is no longer valid ).

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.