重点讨论跟屏幕分辨率相关的资源匹配
屏幕分辨率相关的qualifier如下:
- avaiable width, 比如 w720dp
When your application provides multiple resource directories with different values for this configuration, the system uses the one closest to (without exceeding) the device's current screen width.
- avaiable heigth, 比如 h720dp
When your application provides multiple resource directories with different values for this configuration, the system uses the one closest to (without exceeding) the device's current screen heigth.
- Screen Size: small, normal, large, xlarge
Note: Using a size qualifier does not imply that the resources are only for screens of that size. If you do not provide alternative resources with qualifiers that better match the current device configuration, the system may use whichever resources are the best match.
Caution: If all your resources use a size qualifier that is larger than the current screen, the system will notuse them and your application will crash at runtime (for example, if all layout resources are tagged with the xlarge
qualifier, but the device is a normal-size screen).
- Screen pixel density (dpi), hdpi, xhdpi, ... etc
The system uses the appropriate alternative resourceBased on the size and density of the current screen, the system uses any size- and density-specific resource provided in your application. For example, if the device has a high-density screen and the application requests a drawable resource, the system looks for a drawable resource directory that best matches the device configuration. Depending on the other alternative resources available, a resource directory with the hdpi
qualifier (such as drawable-hdpi/
) might be the best match, so the system uses the drawable resource from this directory.
If no matching resource is available, the system uses the default resource and scales it up or down as needed to match the current screen size and densityThe "default" resources are those that are not tagged with a configuration qualifier. For example, the resources in drawable/
are the default drawable resources. The system assumes that default resources are designed for the baseline screen size and density, which is a normal screen size and a medium-density. As such, the system scales default density resources up for high-density screens and down for low-density screens, as appropriate.
However, when the system is looking for a density-specific resource and does not find it in the density-specific directory, it won't always use the default resources. The system may instead use one of the other density-specific resources in order to provide better results when scaling. For example, when looking for a low-density resource and it is not available, the system prefers to scale-down the high-density version of the resource, because the system can easily scale a high-density resource down to low-density by a factor of 0.5, with fewer artifacts, compared to scaling a medium-density resource by a factor of 0.75.
mipmap vs drawable folders
The mipmap folders are for placing your app/launcher icons (which are shown on the homescreen) in only. Any other drawable assets you use should be placed in the relevant drawable folders as before.
According to this Google blogpost:
It’s best practice to place your app icons in mipmap- folders (not the drawable- folders) because they are used at resolutions different from the device’s current density.
When referencing the mipmap- folders ensure you are using the following reference:
android:icon="@mipmap/ic_launcher"
The reason they use a different density is that some launchers actually display the icons larger than they were intended. Because of this, they use the next size up.