Some rules about drawable in Android

preface

As soon as you enter the deep sea of Android, I believe many Android developers have a deep understanding of the fragmentation of Android system versions, the diversity of Android hardware devices, and the uncertainty of the third-party ROM. Now it's really not easy to develop a qualified commercial app. First of all, application compatibility is a double test of technology and patience. It can be said that it's impossible to perfectly adapt to various situations. It's often to find a balance between manpower and adaptation rate. Today's drawable is an important role that needs to be adapted.

Configuration qualifier

For different screen densities, different device directions, different languages and regions, alternative drawable resources will be involved. At runtime, Android will detect the current device configuration and load appropriate resources for the application according to specific rules (mentioned later). The following are the configuration qualifiers that can be used. It should be noted 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

Language and region: en, FR, en Rus, etc

Layout direction: ldrtl (right to left) ldltr (left to right)

Smallestwidth: SW < n > DP, such as sw320dp, sw600dp, sw720dp, etc. the minimum size of the available height and width of the screen and the "minimum possible size" of the screen.

Available width: W < n > DP, such as w720dp, w1024dp, etc. specify the minimum available screen width that resources should use, in DP, defined by the < n > value. When switching between landscape and portrait, this configuration value will also change in order to match the current actual width.

Available height: H < n > DP, such as h720dp, h1024dp, etc. specify the minimum available screen height that resources should use, in DP, defined by the < n > value. When switching between landscape and portrait, this configuration value will also change in order to match the current actual height.

Screen size: small, normal, large, XLarge

Screen aspect ratio: long wide screen, such as wqvga, WVGA, fwvga; Notlong non widescreen, such as QVGA, HVGA and VGA

Screen orientation: port device is vertical, and land device is horizontal

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

Night mode: night, night, nontight, day

Screen pixel density: LDPI, MDPI, hdpi, xhdpi, xxhdpi, xxhdpi, nodpi, tvdpi

Touch screen type: notch, finger

Keyboard availability: keyssexposed, keyshidden, keysoft

Main text input methods: nokeys, QWERTY, 12key

Navigation key availability: the navixposed navigation key is available to the user, but the navhidden navigation key is not available

Main non touch navigation methods: nonav, dpad, trackball, wheel

Platform version (API level): V3, V4, V7, etc. for example, V1 corresponds to API level 1 and V4 corresponds to API level 4

If you carefully study each configuration qualifier, you will realize that it is impossible to perfectly adapt to various situations. In fact, the most commonly used and basically necessary parameter is the screen pixel density. It is necessary to describe this parameter in detail here:

LDPI: low density screen; About 120 DPI.

MDPI: medium density (traditional HVGA) screen; About 160 DPI.

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 use (starter icon only, see the note in "supporting 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 density.

Tvdpi: screen with density between MDPI and hdpi; About 213dpi. It is not a "primary" density group, it is mainly used for TV, and it is not required for most applications. For most applications, providing MDPI and hdpi resources is sufficient, 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 tvdpi density). Therefore, a 9x9 (LDPI) bitmap is equivalent to a 12x12 (MDPI), 18x18 (hdpi), 24x24 (xhdpi) bitmap, and so on.

Qualifier naming rules

You can specify multiple qualifiers for a single set of resources, separated by dashes. For example, drawable en Rus land is suitable for horizontal American English equipment.

These qualifiers must follow the order listed above, so the list above is sequential. For example: error: drawable hdpi port /, correct: drawable port hdpi/

Alternate resource directories 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 case insensitive file system problems. Any capital letters used in the name are for readability only.

Only one value is supported for each qualifier type. For example, if you want to use the same drawable file for Spanish and French, you certainly can't have a directory named drawable res RFR /, but you need two resource directories containing the corresponding files.

Android matching best drawable rule

If you only use one configuration qualifier, it's good to match. Find the drawable that meets the configuration. However, when you use multiple configuration qualifiers and multiple drawable directories exist at the same time, it's not so simple to match the best drawable. Here's an official example of Android Developer. For example, your application now contains the following directories:

Meanwhile, it is assumed that the configuration of the target device is as follows:

The specific matching process is as follows:

1. Eliminate the resource files that conflict with the device configuration: the drawable fr RCA / directory conflicts with the en GB locale and is therefore eliminated (with one exception, the screen pixel density is the only qualifier that is not eliminated due to the conflict. Although the screen density of the device is hdpi, drawable port LDPI / is not eliminated because each screen density is considered to match at this time)

2. Select the qualifier with the highest priority in the above qualifier list, start from MCC, and then move down to see if there is a resource directory including this qualifier. If not, look at the next qualifier. In this example, unless the language qualifier is reached, the answer is always "no".

3. If so, eliminate the resource directory without this qualifier. In this example, all directories without language qualifiers are eliminated. Therefore, at this stage, the drawable that meets the requirements remains:

drawable-en/

drawable-en-port/

drawable-en-notouch-12key/

4. Select the qualifier of the next priority and repeat steps 2, 3 and 4. Until there is only one directory left, it should be port in this example, so after elimination, only:

drawable-en-port/

There are two points to explain:

1. Screen pixel density is the only qualifier that has not been eliminated due to conflict. If the qualifier involved is screen pixel density, Android will select the option closest to the device screen density. Generally, Android tends to reduce the large original image rather than enlarge the small original image.

2. What if a drawable that meets the qualifier doesn't have one? What else can I do? Collapse!

Differences and relations between drawable and mipmap

When you create a project through Android studio, a series of mipmap folders will be created by default instead of the previous drawable folder. So what is the relationship between mipmap and drawable? Did mipmap replace 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). See the Drawable resource type. mipmap/ For app launcher icons. The Android system retains the resources in this folder (and density-specific folders such as mipmap-xxxhdpi) regardless of the screen resolution of the device where your app is installed. This 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,see Managing Launcher Icons as mipmap Resources.

Here's the conclusion: under the mipmap folder, it is only recommended to put the app launcher icons, that is, the icon that will be displayed on the desktop after the application is installed, while other image resources, etc. are still put under the drawable folder as before.

So why put the launcher icon in the mipmap folder? The following English is the official explanation:

Different home screen launcher apps on different devices show app launcher icons at varIoUs resolutions. When 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 these display issues,apps should use the mipmap/ resource folders for launcher icons. The Android system preserves these resources regardless of density stripping,and ensures that launcher apps can pick icons with the best resolution for display. Make sure launcher apps show a high-resolution icon for your app by moving all densities of 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,be sure to add the higher resolution xxxhdpi versions of the icons to enhance the visual experience of the icons on higher resolution devices.

Here is my understanding: the sizes of launcher icons adopted by many different launcher apps are inconsistent. During application installation, Android resource optimization will delete the unnecessary resolution resources in the drawable folder. For example, delete the resources under drawable xxhdpi on the xhdpi device. Of course, the launcher icon in it will also be deleted, At this time, if the launcher icon used by the launcher app is too large and the icon under high-resolution xxhdpi is deleted, only the small icon under xhdpi can be enlarged, which will cause the display of launcher icon to be blurred. In order to avoid the above problems, mipmap is introduced. Android will ensure that the resources under mipmap will not be deleted due to resource optimization, and ensure that large launcher icon can find icon with more appropriate resolution. Therefore, mipmap is born for launcher icon, while other image resources are still placed in drawable folder, which helps to optimize Android resources, delete useless resources and reduce application volume.

summary

The above is all about some rules that drawable must know in Android. The content of this article is very practical for Android developers. I hope it will be helpful to you.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>