Android-分辨率以及dip(dp)、dpi、ppi、px、sp、pt說明

dip(dp): device independent pixels(設備獨立像素)php

dip,就是把屏幕的高分紅480分,寬分紅320分。好比你作一條160dip的橫線,不管你在320還480的模擬器上,都是一半屏的長度。html

dpi:dot per inchandroid

dpi=(√(橫向分辨率^2+縱向分辨率^2))/屏幕尺寸)佈局

ppi:pixels per inch(跟dpi同樣)字體

計算了一下小米手機屏幕的PPI,4.0英寸、分辨率854X480,PPI(DPI)spa

 

  =√(854^2+480^2)/4=244.912……≈245.
code

px:pixelxml

sp:scaled pixels(放大像素),主要用於字體顯示。htm

pt:point,是一個標準的長度單位,1pt=1/72英寸,用於印刷業。blog

 

日常所說的hdpi等劃分方法(按DPI來劃分):

1.jpg

 

2.jpg

 

 

關於分辨率適配:

 

1、細說 layout_weight

 

    目前最爲推薦的Android多屏幕自適應解決方案。

 

    該屬性的做用是決定控件在其父佈局中的顯示權重,通常用於線性佈局中。其值越小,則對應的layout_width或layout_height的優先級就越高,通常橫向佈局中,決定的是layout_width的優先級;縱向佈局中,決定的是layout_height的優先級。

 

    傳統的layout_weight使用方法是將當前控件的layout_widthlayout_height都設置成fill_parent,這樣就能夠把控件的顯示比例徹底交給layout_weight;這樣使用的話,就出現了layout_weight越小,顯示比例越大的狀況。不過對於2個控件還好,若是控件過多,且顯示比例也不相同的時候,控制起來就比較麻煩了,畢竟反比不是那麼好肯定的。

 

    因而就有了如今最爲流行的0px設值法。看似讓人難以理解的layout_height=0px的寫法,結合layout_weight,卻可使控件成正比例顯示,輕鬆解決了當前Android開發最爲頭疼的碎片化問題之一。

 

    先看下面的stylesstyle_layout.xml

 

複製代碼
<?xml version="1.0" encoding="utf-8"?>
<resources>     <!-- 全屏幕拉伸-->   <style name="layout_full">       <item name="android:layout_width">fill_parent</item>       <item name="android:layout_height">fill_parent</item>     </style>     <!-- 固定自身大小-->   <style name="layout_wrap">       <item name="android:layout_width">wrap_content</item>       <item name="android:layout_height">wrap_content</item>     </style>   <!-- 橫向分佈-->   <style name="layout_horizontal" parent="layout_full">       <item name="android:layout_width">0px</item>     </style>      <!-- 縱向分佈-->   <style name="layout_vertical" parent="layout_full">       <item name="android:layout_height">0px</item>     </style>           </resources>  
複製代碼

 

 

能夠看到,layout_widthlayout_height兩個屬性被我封裝成了4style

 

    根據實際佈局狀況,選用當中的一種,不須要本身設置,看過我前一個ActivityGroupDemo的同窗應該很是熟悉了

 

    而後個人Demo的佈局以下(weight_layout.xml

 

複製代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"         style="@style/layout_full"         android:orientation="vertical">         <LinearLayout                 style="@style/layout_vertical"                 android:layout_weight="1"                 android:orientation="horizontal">                  <View                          style="@style/layout_horizontal"                          android:background="#aa0000"                          android:layout_weight="1"/>                  <View                          style="@style/layout_horizontal"                          android:background="#00aa00"                          android:layout_weight="4"/>                  <View                          style="@style/layout_horizontal"                          android:background="#0000aa"                          android:layout_weight="3"/>                  <View                          style="@style/layout_horizontal"                          android:background="#aaaaaa"                          android:layout_weight="2"/>                         </LinearLayout>         <LinearLayout                 style="@style/layout_vertical"                 android:layout_weight="2"                 android:orientation="vertical">                 <View                          style="@style/layout_vertical"                          android:background="#ffffff"                          android:layout_weight="4"/>                         <View                          style="@style/layout_vertical"                          android:background="#aa0000"                          android:layout_weight="3"/>                  <View                          style="@style/layout_vertical"                          android:background="#00aa00"                          android:layout_weight="2"/>                  <View                          style="@style/layout_vertical"                          android:background="#0000aa"                          android:layout_weight="1"/>           </LinearLayout>
</LinearLayout>
複製代碼

 

 

 

整個界面佈局看起來很是直觀,只是嵌套的邏輯要本身理下。顯示效果以下圖,其中左面一個是480x800的界面,右面的是320x480的界面(後面的圖也如此),能夠看出顯示比例和代碼中徹底一致,我就很少說了,你們對照下就能看出來了。
1.png

 

2、Java代碼裏動態佈局

 

複製代碼
// 第一個按鈕,寬度100%,高度10%
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(                                 LayoutParams.FILL_PARENT,                                 (int) (Constant.displayHeight * 0.1f + 0.5f));                 btn1.setLayoutParams(params);                 // 第二個按鈕,寬度100%,高度30%
                LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(                                 LayoutParams.FILL_PARENT,                                 (int) (Constant.displayHeight * 0.3f + 0.5f));                 btn2.setLayoutParams(params2);                 // 第三個按鈕,寬度50%,高度20%
                LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(                                 (int) (Constant.displayWidth * 0.5f + 0.5f),                                 (int) (Constant.displayHeight * 0.2f + 0.5f));                 btn3.setLayoutParams(params3);                 // 第三個按鈕,寬度70%,高度填滿剩下的空間
                LinearLayout.LayoutParams params4 = new LinearLayout.LayoutParams(                                 (int) (Constant.displayWidth * 0.7f + 0.5f),                                 LayoutParams.FILL_PARENT);                 btn4.setLayoutParams(params4);
複製代碼

 

 

 

3.png

 

 

其餘

 

    以上說的都是多個屏幕顯示相同內容須要考慮的問題,還有一種是在不一樣的屏幕上顯示內容不一樣的狀況,其實這個問題咱們每每是用滾動視圖來解決的,也就是ScrowView;須要注意的是ScrowView中使用layout_weight是無效的,既然使用ScrowView了,就把它裏面的控件的大小都設成固定的吧。

 

    此外關於圖片的自適應問題,主要是2點,一個是9patch圖,這個東西你們都要學會去作,不難;不過有些編譯器在識別9patch圖時會出這樣那樣的bug,像個人Eclipse就不認這個,而同一個9patch圖在別的電腦上倒是沒問題的,

 

    第二個要說的是我曾經被困擾的一個問題,對於480x800  480x854這兩個尺寸,他們顯示同一個圖片時,總有一個會拉伸(若是9patch能夠解決的還好)。其實當初困擾個人是,這兩個尺寸都是hdpi的,覺得沒法給這兩個屏幕作不一樣的圖片。後來無心中發現,圖片能夠和佈局同樣分多個尺寸的,而不只僅是根據密度分,也就是說你能夠寫這樣的文件夾drawable-hdpi-800x480drawable-hdpi-854x480,在它們裏面放不一樣的圖片,這樣圖片也能自適應了。

 

參考文章:

http://www.eoeandroid.com/forum.php?mod=viewthread&tid=173973

相關文章
相關標籤/搜索