Didi Chuxing在其開源的Android框架中,提供了一些非常實用的公共類和工具類,其中有一個叫做DisplayMetrics的類,我們今天就來深入剖析這個類的使用。
一、DisplayMetrics是什麼?
在Android中,DisplayMetrics是一個結構體,主要用於描述顯示設備的一些物理特性,包括顯示區域的大小、密度、字體縮放比例等。
更具體地說,DisplayMetrics類中有以下屬性:
/** * The absolute width of the display in pixels. */ public int widthPixels; /** * The absolute height of the display in pixels. */ public int heightPixels; /** * The exact physical pixels per inch of the screen in the X dimension. */ public float xdpi; /** * The exact physical pixels per inch of the screen in the Y dimension. */ public float ydpi; /** * The logical density of the display. */ public float density; /** * The screen density expressed as dots-per-inch. */ public int densityDpi; /** * A scaling factor for fonts displayed on the display. */ public float scaledDensity; /** * The absolute width of the available display size in pixels. */ public int widthPixelsInner; /** * The absolute height of the available display size in pixels. */ public int heightPixelsInner;
大多數屬性名都比較直觀,這裡提一下density,它是指當前屏幕的邏輯密度,單位是dpi,在160dpi(即mdpi)的屏幕上,density的值為1,而在每個精度/密度分類中,都有相應的density標準值。例如,在240dpi(即hdpi)的屏幕上,density的值為1.5。
二、如何獲取DisplayMetrics?
獲取DisplayMetrics非常簡單,只需要調用android.content.Context類的getResources()方法獲取Resource對象,然後再通過Resource對象的getDisplayMetrics()方法獲取DisplayMetrics即可,代碼如下:
DisplayMetrics metrics = new DisplayMetrics(); getActivity().getResources().getDisplayMetrics();
需要注意的是:
1、getDisplayMetrics()方法只能在Activity、FragmentActivity、Service等有上下文的類中調用。
2、如果你的應用程序跨越多個屏幕密度分類,你應該再加上一個獲取Configuration對象的步驟(詳見下一節)。
三、如何根據屏幕大小和密度適配應用程序?
在Android開發中,要確保應用程序可以在各種屏幕密度和屏幕大小下被合理地顯示,那麼我們就需要用到布局文件中的單位和像素密度。
具體來講,Android的屏幕密度有四種等級:ldpi(低)、mdpi(中)、hdpi(高)和xhdpi(超高),分別對應的dpi值為120、160、240和320。
可以使用px(像素)作為單位,但是幾乎是不可取的,因為每個設備的像素密度不同,布局界面會因屏幕大小不同而出現不必要的拉伸或縮小。
在布局文件中,最好使用dp(dip)作為單位,並在代碼中動態地設置各種組件的屬性。例如:
TextView tv = findViewById(R.id.tv_hello); ViewGroup.LayoutParams params = tv.getLayoutParams(); params.width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics()); params.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics()); tv.setLayoutParams(params);
這裡,我們使用了TypedValue類的applyDimension()方法將100dp和50dp轉化為了不同屏幕密度下的像素值,然後將其分別作為寬和高設置到TextView上。
此外,還可以使用其他的單位,例如sp、mm等,它們的轉化方法和dp基本一致。
四、如何根據屏幕方向適配應用程序?
有時候,我們還需要根據屏幕的方向(橫屏/豎屏)來適配應用程序的界面表現。
在Android中,使用Configuration類來判斷當前屏幕方向是非常方便的,代碼如下:
Configuration configuration = getResources().getConfiguration(); if(configuration.orientation == Configuration.ORIENTATION_LANDSCAPE){ //橫屏 }else{ //豎屏 }
然後,你可以根據屏幕方向對布局界面進行調整來適應橫、豎屏的顯示要求。
五、如何根據屏幕尺寸適配應用程序?
除了屏幕密度、屏幕方向之外,有時候我們還需要根據屏幕的實際尺寸來適配應用程序的界面表現。
在Android中,使用DisplayMetrics的heightPixels和widthPixels屬性獲取當前屏幕的寬和高,代碼如下:
DisplayMetrics metrics = new DisplayMetrics(); getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics); int screenHeight = metrics.heightPixels; int screenWidth = metrics.widthPixels;
然後,你可以根據屏幕的寬和高對布局界面進行調整來適應屏幕的實際尺寸。
六、小結
本文通過深入剖析Android中的displaymetrics,從獲取DisplayMetrics、根據屏幕密度和屏幕大小適配應用程序、根據屏幕方向適配應用程序、根據屏幕尺寸適配應用程序等多個方面闡述了DisplayMetrics的使用方法,並給出了相應的代碼示例,希望本文對你的Android開發有所幫助。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/243157.html