过去,程序员通常以像素为单位设计计算机用户界面。例如:图片大小为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. Zabbix的自定义键值和自动发现功能监控Oracle数据库的表空间

    前面介绍了利用Orabbix监控了,参考zabbix通过Orabbix监控oracle数据库,这里我们原先的模板中进行了修改,使用自动发现功能实现监控tablespace的使用情况. 1. 在被监控的 ...

  2. 微信小程序的wx-charts插件

    还有就是可以使用一些小程序的插件,比如wx-charts. 先来看一下网上对这个插件的评价: 目前在github上有1804颗星,使用的比较广泛. github地址:https://github.co ...

  3. Cookie和Session机制详解

    会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端 ...

  4. LOCAL_WHOLE_STATIC_LIBRARIES与LOCAL_STATIC_LIBRARIES的区别

    在分析Jelly Bean Audio Subsystem的时候,发现HAL层的库audio_policy.xxx.so与其依赖的静态库libaudiopolicy_legacy.a都有audio_p ...

  5. 【加解密专辑】对接触到的PGP、RSA、AES加解密算法整理

    先贴代码,有空再整理思路 PGP加密 using System; using System.IO; using Org.BouncyCastle.Bcpg; using Org.BouncyCastl ...

  6. 【HIbernate异常】could not initialize proxy - no Session (已解决)

    异常信息: org.hibernate.LazyInitializationException: could not initialize proxy - no Session 解决方法: 用 get ...

  7. 原生js--应用程序存储和离线web应用

    1.应用程序缓存和其它存储方式的区别: a.不像localStorage和sessionStorage那样只存储web应用程序的数据,它将应用程序自身存储起来. b.不像浏览器缓存一样会过期或者被用户 ...

  8. OGG 3节点级联时 关键参数

    目标架构为: node1-> node2->node3 node1-> node2 已经同步中,只是需要在此基础上做个node2 ->node3 的同步. 部署后发现 node ...

  9. LeetCode 81 Search in Rotated Sorted Array II(循环有序数组中的查找问题)

    题目链接:https://leetcode.com/problems/search-in-rotated-sorted-array-ii/#/description   姊妹篇:http://www. ...

  10. [原]rpm安装rpm-package报错:Header signature NOKEY 和 error: Failed dependencies:

    以前经常遇到这个问题,一直未有记录,今天记录下来: 在安装rpm包的时候报错误如下: Question 1: warning: *.rpm: Header V3 DSA signature: NOKE ...