过去,程序员通常以像素为单位设计计算机用户界面。例如:图片大小为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. oracle数据据 Python+Pandas 获取Oracle数据库并加入DataFrame

    import pandas as pd import sys import imp imp.reload(sys) from sqlalchemy import create_engine impor ...

  2. 从零开始学Sketch——进阶篇

    本文转自 http://www.jianshu.com/p/ff70b5f35c8f 从零开始学Sketch——进阶篇 Sketch是一款矢量绘图应用,而矢量绘图无疑是目前进行网页.图标以及界面设计的 ...

  3. c++对象的生命周期

    C++ 的new 运算子和C 的malloc 函数都是为了配置内存,但前者比之后者的优点是,new 不但配置对象所需的内存空间时,同时会引发构造式的执行. 所谓构造式(constructor),就是对 ...

  4. 解决安装laravel/homestead vagrant环境报"A VirtualBox machine with the name 'homestead' already exists."的错误

    之前在mac上安装laravel/homestead vagrant虚拟机环境时由于参照的教程是: 每次都必须在~/Homestead目录下边运行vagrant up/halt命令,觉得实在是不方便, ...

  5. windows内核情景分析之—— KeRaiseIrql函数与KeLowerIrql()函数

    windows内核情景分析之—— KeRaiseIrql函数与KeLowerIrql()函数 1.KeRaiseIrql函数 这个 KeRaiseIrql() 只是简单地调用 hal 模块的 KfRa ...

  6. 【WEB前端开发最佳实践系列】高可读的HTML

    一.HTML语义化 HTML5中增加了很多标签都是基于此类原则设计的(article   nav  header  footer).页面标签语义化的优点是使得搜索引擎以及第三方抓包工具等更容易读懂页面 ...

  7. LeetCode - 637. Average of Levels in Binary Tree

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...

  8. SSL延迟有多大?

    http://www.ruanyifeng.com/blog/2014/09/ssl-latency.html 作者: 阮一峰 日期: 2014年9月24日 据说,Netscape公司当年设计SSL协 ...

  9. spring整合websocket通信

    1. maven依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...

  10. 2333: [SCOI2011]棘手的操作[离线线段树]

    2333: [SCOI2011]棘手的操作 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2325  Solved: 909[Submit][Stat ...