標籤:
Android源碼中是這樣來描述DisplayMetrics的。
/** * A structure describing general information about a display, such as its * size, density, and font scaling. * <p>To access the DisplayMetrics members, initialize an object like this:</p> * <pre> DisplayMetrics metrics = new DisplayMetrics(); * getWindowManager().getDefaultDisplay().getMetrics(metrics);</pre> */
按照DisplayMetrics注釋中的那樣,我們直接寫個例子來測試下,就什麼都明白了。我用的是小米3:
DisplayMetrics metrics = new DisplayMetrics(); getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics); /** * 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. * * <p>This value does not exactly follow the real screen size (as given by * {@link #xdpi} and {@link #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). * * density 是邏輯上的螢幕密度,約定在160dpi的裝置上,1px=1dip。 * density = densityDpi / 160 * * @see #DENSITY_DEFAULT */ float density = metrics.density; /** * The screen density expressed as dots-per-inch. May be either * {@link #DENSITY_LOW}, {@link #DENSITY_MEDIUM}, or {@link #DENSITY_HIGH}. * densityDpi 表示每英寸的點數(並不是像素數) */ int densityDpi = metrics.densityDpi; /** * The absolute height of the display in pixels. * 螢幕的絕對高度,以像素為單位。 */ int heightPixels = metrics.heightPixels; /** * The absolute width of the display in pixels. * 同樣,這個是螢幕的絕對寬度,以像素為單位。 */ int widthPixels = metrics.widthPixels; /** * The exact physical pixels per inch of the screen in the X dimension. * 橫向每一英寸確切的物理像素,我們可以嘗試通過這個值和widthPixels 來計算螢幕的寬度英寸。 */ float xdpi = metrics.xdpi; /** * The exact physical pixels per inch of the screen in the Y dimension. * 橫向每一英寸確切的物理像素,我們可以嘗試通過這個值和heightPixels 來計算螢幕的高度英寸。 */ float ydpi = metrics.ydpi; /** * A scaling factor for fonts displayed on the display. This is the same * as {@link #density}, except that it may be adjusted in smaller * increments at runtime based on a user preference for the font size. * scaledDensity 這個值從上面的注釋看,是和字型相關的factor,通常情況下這個值和density是相等的,但是假如使用者手動的調整了 * 系統字型的大小,那麼這個值就有可能改變(以小米3為例,標準字型,這個值=3,最大字型這個值=3.25)。 * 因此現在很多情況下,我們把字型單位寫成dp,儘管Google建議的字型讓寫成sp。 */ float scaledDensity = metrics.scaledDensity; LogUtil.logd(TAG, "metrics.density = " + density); LogUtil.logd(TAG, "metrics.densityDpi = " + densityDpi); LogUtil.logd(TAG, "metrics.heightPixels = " +heightPixels); LogUtil.logd(TAG, "metrics.widthPixels = " +widthPixels); LogUtil.logd(TAG, "metrics.xdpi = " +xdpi); LogUtil.logd(TAG, "metrics.ydpi = " +ydpi); LogUtil.logd(TAG, "metrics.scaledDensity = " +scaledDensity); //來計算手機是幾英寸的 float pixelsToDipWidth = widthPixels / xdpi; float pixelsToDipHeight = heightPixels / ydpi; LogUtil.logd(TAG, "pixelsToDipWidth = " + pixelsToDipWidth); LogUtil.logd(TAG, "pixelsToDipHeight = " + pixelsToDipHeight); double mobileInch = Math.sqrt(pixelsToDipHeight * pixelsToDipHeight + pixelsToDipWidth * pixelsToDipWidth); //我用的小米3,得到的值是4.917646062686045 LogUtil.logd(TAG, "mobileInch = " + mobileInch);
通過這個例子,我們明白了dip及px代表著什麼,我們就可以來寫出dip與px相互轉換的方法。(dip = px / density)
/** * 根據手機的解析度從 dp 的單位 轉成為 px(像素) */ public static int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } /** * 根據手機的解析度從 px(像素) 的單位 轉成為 dp */ public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); }
上面 加的這個0.5f的原因,Google官方文檔中其實有介紹:
Then add 0.5f to round the figure up to the nearest whole number, when converting to an integer.
說白了就是為了四捨五入。。。
比如我上面的例子:
mobileInch = 4.917646062686045 ,我+0.5f(=5.417646062686045),然後再取整之後,就得到5。
Android DisplayMetrics 擷取和螢幕相關的資訊