How is Android based on qualifiers to find the right resource file?

Source: Internet
Author: User
Tags linecap

Reprint Please specify source: http://blog.csdn.net/zhaokaiqiang1992

This article mainly describes what rules the Android system uses to filter and select these folders when we use qualifiers to decorate our resource folders, such as drawable-en-ldpi.

This article is not a serious translation, you want to see the source of the stamp providing Resources

As we all know, when we use qualifiers to decorate resource folders, such as drawable or values, the Android system dynamically chooses the most appropriate resource file based on the device properties and configuration of the runtime, so how exactly does this process work? Below we take the Drawable folder as an example, detailed introduction of the process of this choice and considerations.

Now we're going to assume that the following folders contain 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/

and suppose our runtime device is configured as follows:

    • Locale = EN-GB
    • Screen orientation = port
    • Screen pixel density = hdpi
    • Touchscreen type = Notouch
    • Primary Text Input method = 12key

By comparing these drawable folders with the device configuration and the above available qualifiers, the final Android device selects the pictures from the Drawable-en-port folder.

The system is based on the following logic to determine why to choose this folder inside the picture:

Translated version:

Created with Rapha?l 2.1.2 1. Exclusions and device configuration inconsistent qualifiers 2. Select the next qualifier according to the precedence of the qualifier 3. Is there a resource folder that uses this qualifier? 4. Exclude a folder that does not contain this qualifier (there should be a loopback to the 2nd step, which cannot be drawn) Yes No
    1. First exclude and device configuration want to contradict the Drawable folder. Because drawable-fr-rca/this folder and the language environment EN-GB contradictory, so directly to be ruled out. Now our resource folder is as follows:

      • drawable/
      • drawable-en/
      • drawable-fr-rca/
      • drawable-en-port/
      • drawable-en-notouch-12key/
      • drawable-port-ldpi/
      • drawable-port-notouch-12key/

Note: The pixel density modifier of the screen will not be excluded because it is not the same as the device configuration, even though our device is now hdpi, but drawable-port-ldpi/is not excluded because of the qualifier, because at this stage, All screen densities are considered for adaptation. For more information, refer to support multiple screens.
2. In the order in the table, select the next high-priority modifier qualifier. (starting from MCC, always down)

The order of qualifiers is as follows, from top to bottom, with decreasing precedence. Because a lot of very rarely used, so this is part, more detailed please refer to the original text.

mcc310
configuration Qualifier Value Example
MCC MNC
language en-rus
layout Direction Ldrtl ldltr
Minimum width qualifier sw320dp
to get width w720dp
to get the height h720dp
Screen size large
screen aspect long Notlong
Screen Orientation Port Land
UI mode Car appliance Watch
Night mode night notnight
screen pixel density mdpi nodpi
touch screen type notouch finger

3. Are there any folders that contain these qualifiers?
-If no, return to the second step and look at the next qualifier. (In this case, the answer is no before the language qualifier is found.)
-if yes, continue to step fourth.
4. Exclude the Resource folder that does not contain this qualifier. In this example, the system excludes all resource folders that do not contain language qualifiers.

    • drawable/
    • drawable-en/
    • drawable-en-port/
    • drawable-en-notouch-12key/
    • drawable-port-ldpi/
    • drawable-port-notouch-12key/

Warning: If the qualifier mentioned above is screen density, then the system chooses the resource folder closest to the screen density of the device. Typically, Android prefers to shrink a larger picture rather than zooming in on a smaller one.
5. Go back up and repeat 2,3,4 steps until there is only one folder left. In this example, the screen orientation is the next qualifier, so a folder that does not specify a screen orientation qualifier is excluded.

    • drawable-en/
    • drawable-en-port/
    • drawable-en-notouch-12key/

Now the rest of our folder is drawable-en-port/.

Although this step will be performed when we request each resource file, the system optimizes some aspects. Once our device configuration is known, then those folders that will never be matched are excluded. For example, if our language configuration is English, resource folders with other language qualifiers are excluded from the system and checked out, which can improve our performance. Of course, there is a resource folder that does not specify a language qualifier.

When we choose a limit based on the size of the screen, if there is no very suitable resource folder, then the system will choose a smaller than the current screen resource folder. For example, if we do not configure the appropriate resource file for the large screen, then the system will select a resource of the normal screen size. However, if the screen size modifier qualifier for the currently available resource folder is larger than the current screen, our system will not use these resources, but will crash directly. For example, if our layout resource has only the XLarge qualifier, if we run on the normal-size device, it will crash directly.

Note: The precedence order of the preceding qualifiers is more important than the number of qualifiers. For example, before fourth, the optional resource folder Drawable-port-notouch-12key may have three qualifiers that match the current device, whereas Drawable-en only has one qualifier, but because the language qualifier has a higher precedence, So the Drawable-port-notouch-12key folder is directly excluded.

How is Android based on qualifiers to find the right resource file?

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.