Putting screen densities into the correct bucket

前端 未结 6 2221
滥情空心
滥情空心 2020-12-01 01:39

A set of six generalized densities:

ldpi (low) ~120dpi
mdpi (medium) ~160dpi
hdpi (high) ~240dpi
xhdpi (extra-high) ~320dpi
xxhdpi (extra-extra-high) ~480dpi         


        
6条回答
  •  长情又很酷
    2020-12-01 02:02

    I am interested to know if this is the correct way to do this.

    You are mostly correct.

    The problem lies in this part:


    The reason I asked is because I have created the following value directory resources:

    values-hdpi/dimens
    values-xhdpi/dimens
    values-xxhdpi/dimens
    values-xxxhdpi/dimens
    

    In the dimens.xml I have different margins and set the dp depending on the bucket size i.e.

    100dp
    

    The purpose of dp gets defeated by defining folders like values-hdpi/dimens. Density Pixels, by design, are device-agnostic - 100dp on a device with dpi = 240 will look just as wide/long on a device with dpi = 480. So, if you want your app to look consistent, do not provide different dimensions for different screen densities.

    The correct way to think about this is to realize that the only resource that is affected by varying screen densities is drawable. A drawable on a screen with dpi = 240 will look twice as big compared to a screen with density = 480. I am sure that you're providing folders like drawable-hdpi, drawable-xhdpi etc. to deal with this. For everything else, and especially dimensions, use dp. For text sizes, use scaled-pixels - sp.

    More importantly, you should worry about the range of different screen sizes that are available for android. How would you use all the extra screen real-estate on a 10 inch device compared to a 5 inch phone? Qualifiers such as -normal, -large, xlarge should be of more interest to you.

    To summarize:

    • consider all devices of a certain screen size the same - their screen densities are irrelevant when using density pixels.
    • for every drawable resource you use, place their scaled versions in the buckets you wish to support. Remember that, if you don't provide resources for a certain bucket (say drawable-hdpi), android will scale down your drawables from drawable-xhdpi folder (provided drawable-xhdpi is defined). The reverse is also true: if you have placed all your drawables in drawable-xhdpi, android would scale-up your drawables on a xxhdpi device. The result will be blurry graphics - because of scaling-up.

    I know that its a bit of a steep slope here :). So, if you need to clarify some more, leave me a comment.

提交回复
热议问题