简体   繁体   中英

When scaling is determined, why is there a need for both screen size and density?

When scaling (A layout, an image, and so on), Android determines the scaling based on two factors: Screen size and screen density.

Why is there a need for both of them?

For simplicity, let's assume an app that has a single layout file, with several elements defined with dp units, and with no provided alternative resources. In such a case, a scaling is applied (listing 2, Italic added):

At runtime, the system ensures the best possible display on the current screen with the following procedure for any given resource:

  1. The system uses the appropriate alternative resource [...]

  2. 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 density

Now, given the above, what would not be possible, scaling-wise, if scaling would have been determined only by one of these factors?

A side note:

Numerous resources online regards the scaling functionality, but I'm having a hard time finding a direct explanation for the need of Android to use both size and density factors for it.

To add on that, the most cited resource, Supporting Multiple Screens , left things unclear for me when first stating that the four generalized sizes ( small, medium, large, xlarge ) are deprecated beginning with Android 3.2, while later repeatedly referring to those generalizations as if they are still used (Eg, when stating that "the system uses the appropriate resources based on the generalized size or density"), and other documentation pages do so as well (Eg, the table here ).

On the other hand, the above cited page defines only one type of independency - a density independency, and not a size independency. Therefore, I was left confused.

the system uses the default resource and scales it up or down as needed to match the current screen size and density

I think this is written in a confusing way, but is generally true. Image resources are scaled based on the screen density (when an appropriate alternative resource is not specified), but other resources can be "scaled" based on the screen size. For example, a layout resource will be "scaled up" on a tablet as opposed to a phone. I don't really know why the documentors chose to call this "scaling", but I believe this is what they mean.

In other words, the resources framework provides qualifiers for both density (eg -xxhdpi ) and for size (eg -xlarge or -sw720dp ), but will use non-qualified resources as fallbacks.

the four generalized sizes (small, medium, large, xlarge) are deprecated beginning with Android 3.2, while later repeatedly referring to those generalizations as if they are still used

They are deprecated, and yet they are still used. You are free to go ahead and create a layout-xlarge directory and put alternative layouts in it, and the system will use them when it deems doing so appropriate. It's just not recommended; you're better off using the -swXXXdp qualifiers these days, as this gives you much more control over your layouts.

You usually need bigger margins or layout with different positions (like side by side instead of vertical) with the same views in bigger screens (small, large, xlarge, etc).

But for densitities (mdpi, hdpi, xhdpi, etc) usually you just need bigger image resources (bigger with the original resolution and thickness) so you dont rely on the default scale up image algorithm of Android for images (leading to blur images).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM