若是想在不一樣型號手機對同一個應用作適配,若是你在xml中所有使用dp沒有使用px,那麼適配上依然頗有可能出問題!
無數人存在誤區,認爲本身使用的都是dp,爲何在手機A上面和手機B上面看上去比例不同,爲何在A手機上顯示正好而手機B上卻顯示到屏幕外面
每次解釋的都很累,因此寫此blog
首先先明確幾個概念
density值表示每英寸有多少個顯示點(*)
dip/dp: device independent pixels(設備獨立像素)
注意:dip與屏幕密度有關,屏幕密度與硬件有關,硬件設置不正確,有可能致使dip不能正常顯示。在屏幕密度爲160的顯示屏上,1dip=1px
下面是一些分辨率信息
html
名稱 | 分辨率 | 屏幕密度 |
QVGA | 320*240 | 120 |
WQVGA400 | 400*240 | 120 |
WQVGA432 | 432*240 | 120 |
HVGA | 640*480 | 160 |
WSVGA | 1024*600 | 160 |
WXGA800 | 1280*800 | 160 |
WVGA800 | 800*480 | 240 |
WVGA854 | 854*480 | 240 |
WXGA720 | 1280*720 | 320 |
public void setToDefaults() { widthPixels = 0; heightPixels = 0; density = DENSITY_DEVICE / (float) DENSITY_DEFAULT; densityDpi = DENSITY_DEVICE; scaledDensity = density; xdpi = DENSITY_DEVICE; ydpi = DENSITY_DEVICE; noncompatWidthPixels = 0; noncompatHeightPixels = 0; }
其中density變量註釋以下
density變量註釋 寫道java
float android.util.DisplayMetrics.density The logical density of the display. This is a scaling factor for the Density Independent Pixel unit, where one DIP is one pixel on an approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen), providing the baseline of the system's display. Thus on a 160dpi screen this density value will be 1; on a 120 dpi screen it would be .75; etc. This value does not exactly follow the real screen size (as given by xdpi and ydpi, but rather is used to scale the size of the overall UI in steps based on gross changes in the display dpi. For example, a 240x320 screen will have a density of 1 even if its width is 1.8", 1.3", etc. However, if the screen resolution is increased to 320x480 but the screen size remained 1.5"x2" then the density would be increased (probably to 1.5). See Also: DENSITY_DEFAULT
android.util.DisplayMetrics.density 邏輯密度的顯示。 這是一個比例因子的密度獨立像素單元,其中一個是一個像素上蘸一個大約160 dpi屏幕(例如240 x320,1.5 " x2 "屏幕),提供基線系統的顯示。 所以在一個160 dpi屏幕這密度值是1;在一個120 dpi屏幕會。75;等等。 這個值並不徹底遵循真正的屏幕大小(由xdpi和ydpi,而是用來規模大小的總體UI在步驟基於總改變顯示dpi。 例如,一個240 x320屏幕將會有一個密度的1即便它的寬度爲1.8」,1.3」,等等。 然而,若是屏幕分辨率是增長到320 x480,可是屏幕大小保持1.5 " x2」而後密度會增長(多是1.5)。 參見: 密度默認
轉自:http://www.eoeandroid.com/thread-193122-1-1.htmlandroid