过去,程序员通常以像素为单位设计计算机用户界面。例如:图片大小为80×32像素。这样处理的问题在于,如果在一个dpi(每英寸点数

高的显示器上运行该程序,则用户界面会显得很小。在有些情况下,用户界面可能会小到难以看清内容。由此我们采用与分辨率无关的度量单位来

开发程序就能够解决这个问题。 

一。常用的术语:分清dp与dpi

  (1)px (pixels)像素:每个px对应屏幕上的一个点

  (2)Resolution(分辨率):和电脑的分辨率概念一样,指手机屏幕纵、横方向像素个数

  (3)Density(密度):屏幕里像素值浓度

 (4)dpi(dot per inch) 每英寸像素数:QVGA(Quarter Video Graphics Array)(320*240)分辨率的屏幕物理尺寸是(2英寸*1.5英寸),dpi=160

  (5)dp或dip(density independent pixels)密度独立像素:一种基于屏幕密度的单位,在每英寸160点的显示器上,1dip=1px

但随着屏幕密度的改变,dip和px的换算会发生改变。这个和设备硬件有关,一般我们为了支持WVGA、HVGA和QVGA 推荐使用这个

换算:px=dp*(dpi/160)

  (6)sp (scaled pixels ) 放大像素:主要处理字体的大小

  (7)in (inches)英寸:标准长度单位

  (8)mm (millimeters)毫米:标准长度单位

  (9)pt (points)点:标准长度单位,1/72 英寸

 二。为什么引入dp

1.不引入dp时

2.引入dp后

设定160dp的长度,相当于告诉Android在160dpi屏幕上显示160px,在240dpi屏幕上显示240px 。

 三。DPI(dot per inch)计算

(1)分辨率480 x 800,屏幕尺寸4.3英寸   216

(2)分辨率540 x 960,屏幕尺寸4.5英寸 244

(3)分辨率240 x 320,屏幕尺寸2.5英寸   160

2.将不同屏幕大小和不同dpi(dot per inch)的设备大致划分为四类,如下图:

三。DisplayMetrics(点击查看源码)

public float density

  The logical density of the display. This is a scaling factor for the Density Independent Pixel unit, where one DIP is one pixel

on an approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen), providing the baseline of the system's display.

Thus on a 160dpi screen this density value will be 1; on a 120 dpi screen it would be .75; etc.

  This value does not exactly follow the real screen size (as given by xdpi and ydpi, but rather is used to scale the size of the

overall UI in steps  based on gross changes in the display dpi. For example, a 240x320 screen will have a density of 1 even if its

width is 1.8", 1.3", etc. However, if the screen resolution is increased to 320x480 but the screen size remained 1.5"x2" then the

density would be increased (probably to 1.5).

四。代码

Resources r = getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14, r.getDisplayMetrics());
public static float convertDpToPixel(float dp, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return px;
} public static float convertPixelsToDp(float px, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float dp = px / (metrics.densityDpi / 160f);
return dp;
}

Android(三) 匹配屏幕密度的更多相关文章

  1. android多分辨率多屏幕密度下UI适配方案

    相关概念 分辨率:整个屏幕的像素数目,为了表示方便一般用屏幕的像素宽度(水平像素数目)乘以像素高度表示,形如1280x720,反之分辨率为1280x720的屏幕,像素宽度不一定为1280 屏幕密度:表 ...

  2. android屏幕适配的全攻略2--支持手机各种屏幕密度dpi

    如何为不同密度的屏幕提供不同的资源和使用密度独立的单位. 1 使用密度无关像素 坚决杜绝在布局文件中使用绝对像素来定位和设置大小.因为不同的屏幕有不同的像素密度,所以使用像素来设置控件大小是有问题的, ...

  3. 支持不同Android设备,包括:不同尺寸屏幕、不同屏幕密度、不同系统设置

    Some of the important variations that you should consider include different languages, screen sizes, ...

  4. Android中图片大小和屏幕密度的关系讲解

    Android手机适配是非常让人头疼的一件事,尤其是图片,android为了做到是适配提供了很多文件夹来存放不同大小的图片,比如:drawable-ldpi.drawable-mdpi.drawabl ...

  5. Android开发系列之屏幕密度和单位转换

    由于Android的开源性,所以目前市面上面Android手机的分辨率特别多,这样的话就给我适配带来了一定的难度.要想做好适配,我们首先应该明白什么是分辨率.PPI.屏幕大小等概念,还有在不同的屏幕密 ...

  6. android屏幕密度规律及dp px转换

    px和dp(sp) 之间转化公式: 1  乘以(dp转px)或者除以(px转dp) scal缩放因子,在上浮0.5f /** * 密度转换像素 * */ public static int dip2p ...

  7. Android Developers:支持不同的屏幕密度

    这节课程向你展示如何通过提供不同的资源和使用与分辨率无关的测量单位,支持不同屏幕密度. 使用密度无关的像素 —————————————————————————————————————————————— ...

  8. android中dip、dp、px、sp和屏幕密度

    1. dip: device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA.HVGA和QVGA 推荐使用这    这个 ...

  9. android: android中dip、dp、px、sp和屏幕密度

    android中dip.dp.px.sp和屏幕密度 转自:http://www.cnblogs.com/fbsk/archive/2011/10/17/2215539.html 1. dip: dev ...

随机推荐

  1. HTML 格式化

    格式化标签: <!DOCTYPE HTML> <html> <body> <b> This text is bold </b> # < ...

  2. Python的Flask框架与数据库连接的教程

     命令行方式运行Python脚本 在这个章节中,我们将写一些简单的数据库管理脚本.在此之前让我们来复习一下如何通过命令行方式执行Python脚本. 如果Linux 或者OS X的操作系统,需要有执行脚 ...

  3. #pragma init_seg

    先进后出原则,最先初始化的最后析构! 1.C++中全局对象.变量的构造函数调用顺序是跟声明有一定关系的,即在同一个文件中先声明的先调用.对于不同文件中的全局对象.变量,它们的构造函数调用顺序是未定义的 ...

  4. iOS UIImage:获取图片主色调

    本文转载至 http://www.wahenzan.com/a/mdev/ios/2015/0325/1677.html -(UIColor*)mostColor{ #if __IPHONE_OS_V ...

  5. listView优化方案

    1.如果自定义适配器,那么在getView方法中要考虑方法传进来的参数contentView是否为null,如果为null就创建contentView并返回,如果不为null则直接使用.在这个方法中尽 ...

  6. Android设置横屏竖屏

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FUL ...

  7. Unicode编码转换汉字

    Uri.UnescapeDataString(string) #region Unicode转换汉字 Console.WriteLine(Uri.UnescapeDataString("\u ...

  8. sublime--package control的配置与插件安装

    自动配置: 准备一个安装好的 sublime text .这里我的是版本3: 1. 快捷键:ctrl + ~:调出控制台,因为我的是版本3,所以在控制台中输入下边这段代码: import urllib ...

  9. Elasticsearch学习之ElasticSearch 5.0.0 安装部署常见错误或问题

    ElasticSearch 5.0.0 安装部署常见错误或问题 问题一: [--06T16::,][WARN ][o.e.b.JNANatives ] unable to install syscal ...

  10. hdu3507 Print Article[斜率优化dp入门题]

    Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)To ...