android DPI 計算 及單位換算

來源:互聯網
上載者:User

1. 術語和概念

術語

說明

備忘

Screen size(螢幕尺寸)

指的是手機實際的物理尺寸,比如常用的2.8英寸,3.2英寸,3.5英寸,3.7英寸。指手機螢幕對角線長度。

摩托羅拉milestone手機是3.7英寸

Aspect Ratio(寬高比率)

指的是實際的物理尺寸寬高比率,分為long和nolong

Milestone是16:9,屬於long

Resolution(解析度)

和電腦的解析度概念一樣,指手機螢幕縱、橫方向像素個數

Milestone是854*480

DPI(dot per inch)

每英吋像素數,如120dpi,160dpi等,假設QVGA(320*240)解析度的螢幕物理尺寸是(2英寸*1.5英寸),dpi=160

可以反映螢幕的清晰度,用於縮放UI的

Density(密度)

螢幕裡像素值濃度,resolution/Screen size可以反映出手機密度

 

Density-independent pixel (dip)

指的是邏輯密度計算單位,dip和具體像素值的對應公式是dip/pixel=dpi值/160

 

 

2. DPI值計算

比如:計算WVGA(800*480)解析度,3.7英寸的密度DPI,1所示

 

                圖1 

Diagonal pixel表示對角線的像素值(=),DPI=933/3.7=252

 

 

 

3. 手機螢幕的分類

 

3.1 根據手機螢幕密度(DPI)或螢幕尺寸大小分為以下3類,2所示

 

 

                        

                          圖2

 

3. 2 手機螢幕分類和像素密度的對應關係如表1所示:

 

Low density (120), ldpi

Medium density (160), mdpi

High density (240), hdpi

Small screen

QVGA (240x320)

 

 

Normal screen

WQVGA400 (240x400)WQVGA432 (240x432)

HVGA (320x480)

WVGA800 (480x800)WVGA854 (480x854)

Large screen

 

WVGA800* (480x800)WVGA854* (480x854)

 

                                       表1

3.3 手機尺寸分布情況(http://developer.android.com/resources/dashboard/screens.html)3所示,目前主要是以解析度為800*480和854*480的手機使用者居多

                                                         圖3

4 UI設計

從開發角度講,應用程式會根據3類Android手機螢幕提供3套UI布局檔案,但是相應介面表徵圖也需要提供3套,如表2所示

Icon Type

Standard Asset Sizes (in Pixels), for Generalized Screen Densities

 

Low density screen (ldpi)

Medium density screen (mdpi)

High density screen (hdpi)

Launcher

36 x 36 px

48 x 48 px

72 x 72 px

Menu

36 x 36 px

48 x 48 px

72 x 72 px

Status Bar

24 x 24 px

32 x 32 px

48 x 48 px

Tab

24 x 24 px

32 x 32 px

48 x 48 px

Dialog

24 x 24 px

32 x 32 px

48 x 48 px

List View

24 x 24 px

32 x 32 px

48 x 48 px

下面是幾種不同單位的相互轉換.

public static int dip2px(Context context, float dipValue){ 
final float scale = context.getResources().getDisplayMetrics().density; 
return (int)(dipValue * scale + 0.5f); 

public static int px2dip(Context context, float pxValue){ 
final float scale = context.getResource().getDisplayMetrics().density; 
return (int)(pxValue / scale + 0.5f); 

public static int dip2px(Context context, float dipValue){ 
final float scale = context.getResources().getDisplayMetrics().density; 
return (int)(dipValue * scale + 0.5f); 

public static int px2dip(Context context, float pxValue){ 
final float scale = context.getResource().getDisplayMetrics().density; 
return (int)(pxValue / scale + 0.5f); 

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

    在一個Activity的onCreate方法中,寫入如下代碼:
        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        int width = metric.widthPixels;  // 螢幕寬度(像素)
        int height = metric.heightPixels;  // 螢幕高度(像素)
        float density = metric.density;  // 螢幕密度(0.75 / 1.0 / 1.5)
        int densityDpi = metric.densityDpi;  // 螢幕密度DPI(120 / 160 / 240)
這還是挺簡單的, 可是你有沒有在800*480的機器上試過, 是不是得到的寬度是533 ? 因為android剛開始時預設的density是1.0 , 此時你可以再manifest.xml中加入

1.uses-sdk節點, <uses-sdk android:minSdkVersion="4" /> , 表示不sdk1.6以下的機器不能安裝你的apk了.

2.supports-screens 節點. 

   <supports-screens
            android:smallScreens="true"
            android:normalScreens="true"
            android:largeScreens="true"
            android:resizeable="true"
            android:anyDensity="true" />

                                         表2

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.