Some rules about drawable in Android _android

Source: Internet
Author: User
Tags locale touch home screen

Objective

A deep sea of Android, I believe many Android developers are aware of the fragmentation of the Android version, the diversity of Android hardware devices, and the uncertainties of third-party Rom. Now it's not easy to develop a qualified commercial App. First of all, the application of compatibility is a technology and patience of the double test, to perfect the situation can be said to be impossible, is often in the human and adaptation ratio between the balance, today to say drawable is an important role that needs to fit.

Configuration qualifier

For different screen densities, different device orientations, different languages and regions, the alternative drawable resources are involved and, at runtime, Android detects the current device configuration and loads the appropriate resources for the application based on specific rules (referred to later). The following are the configuration qualifiers that you can use, and you need to note that these configuration qualifiers are valid not only for drawable, but also for other resource types (such as: layout):

Mobile Country Code (MCC): mcc310, mcc310-mnc004, mcc208-mnc00

Languages and areas: En, fr, En-rus, etc.

Layout direction: Ldrtl (from right to left) ldltr (left to right)

SMALLESTWIDTH:SW<N>DP such as: SW320DP, SW600DP, SW720DP, etc., screen available height and width of the minimum size, screen of the "minimum possible size."

Available widths: W<N>DP such as: W720DP, W1024DP, and so on, specify the minimum available screen width that the resource should use, in DP, by the <N> value definition. When you switch between landscape and portrait, this configuration value changes to match the current actual width.

Available Heights: H<N>DP such as: H720DP, H1024DP, and so on, specify the minimum available screen height that the resource should use, in DP units, by <N> value. When you switch between landscape and portrait, this configuration value changes to match the current actual height.

Screen size: small, normal, large, xlarge

Screen aspect ratios: Long widescreen, such as WQVGA, WVGA, Fwvga;notlong widescreen, such as QVGA, HVGA, and VGA

Screen Orientation: Port device is in portrait (vertical), land device is horizontal (horizontal)

UI mode: Car, desk, television, appliance, watch

Night mode: Night Night, nontight daytime

Screen pixel density: ldpi, mdpi, hdpi, xhdpi, xxhdpi, xxxhdpi, nodpi, tvdpi

Touch screen type: Notouch, finger

Keyboard availability: keysexposed, Keyshidden, Keyssoft

Main text Input Method: Nokeys, QWERTY, 12key

Navigation key availability: Navexposed navigation keys available to users, Navhidden navigation keys not available

Main non-touch navigation methods: Nonav, Dpad, trackball, Wheel

Platform version (API level): V3, v4, V7, etc., such as V1 corresponding to API level 1,V4 corresponding to API level 4

If you carefully study the next each configuration qualifier, you will realize: want to perfect fit all kinds of situations can be said to be impossible, in fact, we usually most commonly used is the basic must be used is the screen pixel density, here is necessary to say this parameter in detail:

LDPI: low density screen; about 120dpi.

MDPI: Medium density (traditional HVGA) screen, about 160dpi.

HDPI: High-density screen, about 240dpi.

XHDPI: Ultra high-density screen; about 320dpi. New configuration in API level 8

XXHDPI: Ultra-high density screen, about 480dpi. New configuration in API level 16

XXXHDPI: Ultra-high density screen usage (launcher icon only, see comments in support for multiple screens); about 640dpi. New configuration in API level 18

NODPI: It can be used for bitmap resources that you do not want to scale to match device densities.

TVDPI: A screen with a density between mdpi and hdpi; about 213dpi. It is not a "primary" density group, mainly used for television, and most applications do not need it. For most applications, it is sufficient to provide mdpi and hdpi resources, and the system will scale them as needed. This qualifier was introduced in API level 13.

The scaling ratio between the six main densities is 3:4:6:8:12:16 (ignoring the tvdpi density). Therefore, the 9x9 (ldpi) bitmap corresponds to the 12x12 (MDPI), 18x18 (hdpi), 24x24 (xhdpi) bitmap, and so on.

Qualifier naming rules

You can specify multiple qualifiers for a single set of resources and separate them with dashes. For example, Drawable-en-rus-land is suitable for horizontal American English devices.

These qualifiers must follow the order listed above, so the list above is sequential. Example: Error: drawable-hdpi-port/, correct: drawable-port-hdpi/

An alternate resource directory cannot be nested. For example, you cannot have res/drawable/drawable-en/.

Values are not case-sensitive. Before processing, the resource compiler converts directory names to lowercase to avoid problems with case-insensitive file systems. Any uppercase letters used in the name are for readability only.

For each qualifier type, only one value is supported. For example, to use the same drawable file for Spanish and French, you must not have a directory named drawable-res-rfr/, but you need two resource directories that contain the appropriate files.

Android Match best drawable rule

If you use only one configuration qualifier, then match well, find the drawable that matches the configuration, but when you use multiple configuration qualifiers at the same time and multiple drawable directories, the best drawable is not as simple as the Android The official example of Developer, for example: Now your application contains the following directory:

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

Also, assume that the target device is configured as follows:

Locale = EN-GB 
screen orientation = port 
screen pixel density = hdpi 
touch Screen type = Notouch 
Primary text Input method = 12key

The exact matching process is as follows:

1. Eliminate the resource files that conflict with the device configuration: where the drawable-fr-rca/directory conflicts with the EN-GB locale, it is eliminated (with one exception, screen pixel density is the only qualifier that is not eliminated as a result of the conflict, although the screen density of the device is hdpi, Drawable-port-ldpi/is not eliminated because each screen density is considered a match at this time

2. Select the qualifier with the highest precedence in the qualifier list above, start with MCC and then move down to see if there is a resource directory that includes this qualifier and, if not, the next qualifier, in this example, the answer is always "no" unless the language qualifier is reached.

3. If so, the resource directory without this qualifier is eliminated. In this example, all directories that do not contain a language qualifier are eliminated. So the drawable to meet the requirements of this step are left:

drawable-en/

drawable-en-port/

drawable-en-notouch-12key/

4, select the next priority qualifier, repeat steps 2, 3, 4. Until only one directory is left, the example should be port, so there is only left after the elimination:

drawable-en-port/

There are two points to explain:

1. Screen pixel density is the only qualifier that has not been eliminated by conflict, and if the qualifier involved is screen pixel density, Android will choose the option closest to the device's screen density. Often Android tends to shrink large raw images rather than zooming in on small, original images.

2. What if a qualifier is not drawable? What else can I do, Crash!

The difference and connection between drawable and Mipmap

Now the project is created from Android Studio, and a series of Mipmap folders are created by default instead of the previous drawable folder. So what exactly is mipmap and drawable? Has mipmap replaced drawable? Let's take a look at the official instructions:

drawable/
For bitmap files (PNG, JPEG, or GIF), 9-patch image files, and XML files that describe drawable shapes or drawable objects That contain multiple states (normal, pressed, or focused). The drawable resource type.
mipmap/
For app launcher icons. The Android system retains this folder (and Density-specific folders such as mipmap-xxxhdpi) regardless O f The screen resolution of the device where your app is installed. This is behavior allows launcher apps to pick the best resolution icon for your app to display on the home screen. For more information about using the Mipmap folders, to the managing Launcher Icons as Mipmap resources.

here is the conclusion:Mipmap folder, only recommended to put the launch icon (app launcher icons), that is, after the application installation, will be displayed on the desktop icon, and other picture resources, or in accordance with the previous way, placed in the Drawable folder.

So why put the Launcher Icon in the Mipmap folder? The following English is an official explanation:

Different home screen launcher apps on different devices show app launcher icons at various resolutions. When the app resource optimization techniques Remove resources for unused screen densities, launcher icons can wind up looking Fuzzy because the Launcher app has to upscale a lower-resolution icon for display. To avoid this display issues, apps should use the Mipmap/resource folders for launcher. The Android system preserves regardless of density stripping, and ensures that launcher apps can pick icon s with the best resolution for display.
Make sure launcher apps show a high-resolution icon-for-your app by moving all densities of the your launcher icons to density -specific Res/mipmap/folders (for example Res/mipmap-mdpi/and res/mipmap-xxxhdpi/). The Mipmap/folders replace the drawable/folders for launcher icons. For XXHPDI launcher icons, is sure to add the higher resolution of the XXXHDPI to versions the visual icons Ence of the icons on higher resolution devices.

Here's what I understand: many different Launcher apps use Launcher Icon sizes that are inconsistent, and when installed, Android resource optimizations Remove unwanted resolution resources from drawable folders, such as in xhdpi Spare the resources under drawable-xxhdpi deleted, of course, the Launcher icon will also be deleted, then if Launcher App used Launcher icon size is large, and high resolution xxhdpi under the icon was deleted, just Can be xhdpi under the small size of the icon to enlarge the display, which will cause Launcher icon display blurred. In order to avoid the above problems, the introduction of mipmap,android will ensure that the resources under mipmap will not be deleted because of resource optimization, to ensure that large size Launcher icon can find a more appropriate resolution icon. So Mipmap is born for Launcher Icon, and other image resources are placed under the Drawable folder, which helps the Android resource optimization remove unwanted resources and reduce the volume of applications.

Summarize

That's all there is to know about the rules of drawable in Android, and it's useful for Android developers, and I hope it will help you.

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.