How should you (not) use density independent pixels on Android?

点点圈 提交于 2019-12-23 05:15:09

问题


One poster suggested using a project called SDP, as an answer to a question regarding the Android density independence mechanism. He justified it, saying:

It can help Android developers with supporting multiple screens

Why is that a bad answer or a bad idea in general?


回答1:


Multiple reasons:

Bad practice

The approach taken by this project is arguably useless, even destructive.

Destructive, because it breaks Android density independence. It takes images that need little scaling to match the display's actual pixels per inch property, because they were designed for that device's generalized density. And it scales them up, even up to 2.6 times on 10″ tablets. This must result in blurry or pixelated bitmaps.

Useless because:

You don't want bigger physical size on bigger devices in the first place anyway. You don't want to take an app and just scale everything on bigger screens. This is what Apple did when the iPad first came out in 2010 and people hated it.

What you do want is to put a limit on the width of text, buttons and other UI elements that shouldn't be stretched too much. You also want wider margins. But you handle this, by providing an alternative layout for large screens, not by fiddling with how Android handles the screen density. The documentation agrees with me on that.

And if you really wanted some graphic to be bigger on a large screen (which you generally shouldn't, the second image here is perfectly fine, except for the horizontal stretching), then you should handle it by providing drawables for every size/density pair that you want to support. For example drawable-hdpi, drawable-xhdpi, drawable-sw720dp-hdpi, drawable-sw720dp-xhdpi. This way the bitmap won't need scaling for those bigger screens and will be displayed in high quality.

Misleading

The screenshots do not use the same scale for each device. Nexus 7 looks just as tiny as Nexus One.

When the scale is preserved, the comparison looks like this when using the project:

And like this, when not using it:

Irrelevant

That project deals with physical size and not screen density. What it does is scale units depending on varying physical screen size.

It does not apply to the question that was asked. Which could be paraphrased as "How does Android generalize an actual screen density?".



来源:https://stackoverflow.com/questions/33918276/how-should-you-not-use-density-independent-pixels-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!