您的位置:首页 > 移动开发 > Android开发

android DPI 计算 及单位换算

2016-04-06 15:39 471 查看
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" />
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: