Confusion in “smallest width dp” in Android

不羁岁月 提交于 2019-12-11 03:18:26

问题


I am learning how to use "smallest width dp" to support different screens using this in android.

i get that below number are the smallest of side of device in dp.

Typical numbers for screen width dp are:

320: a phone screen (240x320 ldpi, 320x480 mdpi, 480x800 hdpi, etc).

480: a tweener tablet like the Streak (480x800 mdpi).

600: a 7” tablet (600x1024).

720: a 10” tablet (720x1280, 800x1280, etc).

According to this post nexus 6 has 730 x 410 dp.

The Nexus 6 boasts an impressive 5.96” Quad HD screen display at a resolution of 2560 x 1440 (493 ppi). This translates to ~ 730 x 410 dp (density independent pixels).

but as explained in this

           dp = (px/dpi)*160
              = (1440/493)*160
              = ~467

then how come this translates to 730 x 410 dp? further more when i run the demo in nexus 6, device is using padding dimension defined under res/values-sw320dp/dimens.xml

this confuses me. how does one actually calculates dp and create view accordingly using "smallest width dp" ?

apart from res/values-sw320dp i have res/value and res/values-sw600dp that has dimens.xml under it.

UPDATE figured out my confusion. check comment under question.


回答1:


You can get the correct values with the following code:

DisplayMetrics displayMetrics = this.getResources().getDisplayMetrics();
    float dpHeight = displayMetrics.heightPixels / displayMetrics.density;
    float dpWidth = displayMetrics.widthPixels / displayMetrics.density;
    Log.d("dpHeight-----",String.valueOf(dpHeight));
    Log.d("dpWidth------",String.valueOf(dpWidth));

For example for the Xperia Z 1080x1920px the values are 360x592dp, the settings is values-sw360dp



来源:https://stackoverflow.com/questions/30869168/confusion-in-smallest-width-dp-in-android

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