android 解釋dp,px,pt,sp單位

來源:互聯網
上載者:User

標籤:android   des   style   blog   color   使用   

1.dp(dip):不同裝置有不同的顯示效果,這個和裝置硬體有關係,一般我們為了支援WVGA,HVGA和QVGA對劍使用這個,它是不依賴像素的

2.px:pixels(像素),不同裝置顯示效果相同,一般我們HVGA代表320×480像素,這個用的比較多

3.pt:point,是一個標準的長度單位,1pt=1/72英寸,用於印刷業,非常簡單易用

4.sp:scaled pixels(放大像素),主要用於字型顯示best for textsize

在apk的資源套件中,當螢幕density=240時使用hdpi標籤的資源

         當螢幕desity=160時候、,使用mdpi標籤的資源

         當螢幕density=120時,使用ldoi標籤的資源

在沒英寸160點的顯示器上,1dp=1px

5.下面是集中不同單位的相互轉換:

 1 public static int dip2px(Context context, float dipValue){ 2 final float scale = context.getResources().getDisplayMetrics().density; 3 return (int)(dipValue * scale + 0.5f); 4 } 5 public static int px2dip(Context context, float pxValue){ 6 final float scale = context.getResource().getDisplayMetrics().density; 7 return (int)(pxValue / scale + 0.5f); 8 } 9 public static int dip2px(Context context, float dipValue){10 final float scale = context.getResources().getDisplayMetrics().density;11 return (int)(dipValue * scale + 0.5f);12 }13 public static int px2dip(Context context, float pxValue){14 final float scale = context.getResource().getDisplayMetrics().density;15 return (int)(pxValue / scale + 0.5f);16 }

6.下面說下如何擷取解析度:

1     //在一個Activity的onCreate方法中,寫入如下代碼:2         DisplayMetrics metric = new DisplayMetrics();3         getWindowManager().getDefaultDisplay().getMetrics(metric);4         int width = metric.widthPixels;  // 螢幕寬度(像素)5         int height = metric.heightPixels;  // 螢幕高度(像素)6         float density = metric.density;  // 螢幕密度(0.75 / 1.0 / 1.5)7         int densityDpi = metric.densityDpi;  // 螢幕密度DPI(120 / 160 /240)        

7.Android支援下列所有單位。
px(像素):螢幕上的點。
in(英寸):長度單位。
mm(毫米):長度單位。
pt(磅):1/72英寸。
dp(與密度無關的像素):一種基於螢幕密度的抽象單位。在每英寸160點的顯示器上,1dp = 1px。
dip:與dp相同,多用於android/ophone樣本中。
sp(與刻度無關的像素):與dp類似,但是可以根據使用者的字型大小喜好設定進行縮放

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.