您的位置:首页 > 其它

getResources().getDisplayMetrics().density 的理解

2016-07-03 19:19 429 查看
转载自:http://www.07net01.com/2015/04/814281.html


网上别人的代码 用于px和dp相互转换

import android.content.Context;

public class DensityUtil {

/**
* 根据手机分辨率从 dp 的单位 转成为 px(像素)
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}

/**
* 根据手机的分辨率从 px(像素) 的单位 转成为 dp
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
}


重点,对density的理解

float scale = getResources().getDisplayMetrics().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).
*/

/**
* 显示器的逻辑密度,这是【独立的像素密度单位(首先明白dip是个单位)】的一个缩放因子,
* 在屏幕密度大约为160dpi的屏幕上,一个dip等于一个px,这个提供了系统显示器的一个基线(这句我实在翻译不了)。
* 例如:屏幕为240*320的手机屏幕,其尺寸为 1.5"*2"  也就是1.5英寸乘2英寸的屏幕
* 它的dpi(屏幕像素密度,也就是每英寸的像素数,dpi是dot per inch的缩写)大约就为160dpi,
* 所以在这个手机上dp和px的长度(可以说是长度,最起码从你的视觉感官上来说是这样的)是相等的。
* 因此在一个屏幕密度为160dpi的手机屏幕上density的值为1,而在120dpi的手机上为0.75等等
* (这里有一句话没翻译,实在读不通顺,不过通过下面的举例应该能看懂)
* 例如:一个240*320的屏幕尽管他的屏幕尺寸为1.8"*1.3",(我算了下这个的dpi大约为180dpi多点)
* 但是它的density还是1(也就是说取了近似值)
* 然而,如果屏幕分辨率增加到320*480 但是屏幕尺寸仍然保持1.5"*2" 的时候(和最开始的例子比较)
* 这个手机的density将会增加(可能会增加到1.5)
*/

转载请注明:Itroadmap » getResources().getDisplayMetrics().density 的理解(px和dp相互转换)
关键词:
赞 (0) 评论 分享 (0)

相关阅读

AMQP基本概念
实用模式之中介者模式
在 Swift 中编写 watchOS 2 Hello World 程序
如何恢复Windows8下IE的跳转列表功能
Installation Oracle11gR2 RAC---常见报错处理
Android中使用log4j
Installation Oracle11gR2 RAC---创建数据库
Installation Oracle11gR2 RAC---安装database

最新最早最热0条评论
还没有评论,沙发等你来抢
社交帐号登录:微信
微博
QQ
人人
更多»


发布电脑玩物正在使用多说
换热器
诺顿
结构主义
2011新款
鞋店
施密特
酷睿系列
招聘销售
国内长途
车臣共和国
塑料管
货运费用
动态主题
艺术类院校
网易163
应用中心
妈妈的菜
小钢炮
开瓶器
域名交易
Sergio
展厅设计
拾音器
思卡尔顿
中华小当家
在线播放器
雪佛兰
最小公倍数
Splash
线电压

<iframe id="iframeu2499984_0" src="http://pos.baidu.com/gcwm?rdid=2499984&dc=2&di=u2499984&dri=0&dis=0&dai=1&ps=599x960&dcb=BAIDU_SSP_define&dtm=BAIDU_DUP_SETJSONADSLOT&dvi=0.0&dci=-1&dpt=none&tsr=0&tpr=1467544569279&ti=getResources().getDisplayMetrics().density%20%E7%9A%84%E7%90%86%E8%A7%A3%EF%BC%88px_%E3%80%8C%E7%94%B5%E8%84%91%E7%8E%A9%E7%89%A9%E3%80%8D%E4%B8%AD%E6%96%87%E7%BD%91%E6%88%91&ari=1&dbv=2&drs=1&pcs=1281x645&pss=1281x2771&cfv=0&cpl=34&chi=1&cce=true&cec=UTF-8&tlm=1453399100&rw=645&ltu=http%3A%2F%2Fwww.07net01.com%2F2015%2F04%2F814281.html&ltr=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3Dhqyv31tj7O4yxkPfljzN64D1XX9TKK9QXnFZwsND5iloLsJAwJvp---2W-WEkFqNNH12Lqo4z6YUjUhTE0cVRa%26wd%3D%26eqid%3D8acb61a7003de7fc000000055778f351&ecd=1&psr=1360x768&par=1298x768&pis=-1x-1&ccd=24&cja=true&cmi=60&col=zh-CN&cdo=-1&tcn=1467544569&qn=fd8cce91e9c6890e&tt=1467544569144.139.450.454" width="300" height="250" align="center,center" vspace="0" hspace="0" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true" style="box-sizing: border-box; border-width: 0px; border-style: initial; vertical-align: bottom; margin: 0px;"></iframe>


最新文章

Photoshop
清晰开阔的海景婚片阅读(81)
Photoshop为美女风景照变换景色的教程阅读(147)
Photoshop让你的脸更美脸部祛痘处理阅读(162)
Photoshop
简单的光影叠加字阅读(162)
Photoshop
打造怀旧的古典淡黄色婚片阅读(1)
Photoshop
流水图片加上奇幻流动的迷雾阅读(7)
Photoshop黄色皮肤变白里透红教程阅读(191)
photoshop
合成颓废绚丽的舞者阅读(125)
Photoshop
打造高清的阳光吉他手阅读(8)
photoshop
三步调出人物照片暗灰的中性色阅读(71)

<iframe id="iframeu2500053_0" src="http://pos.baidu.com/gcwm?rdid=2500053&dc=2&di=u2500053&dri=0&dis=0&dai=2&ps=1445x960&dcb=BAIDU_SSP_define&dtm=BAIDU_DUP_SETJSONADSLOT&dvi=0.0&dci=-1&dpt=none&tsr=0&tpr=1467544569279&ti=getResources().getDisplayMetrics().density%20%E7%9A%84%E7%90%86%E8%A7%A3%EF%BC%88px_%E3%80%8C%E7%94%B5%E8%84%91%E7%8E%A9%E7%89%A9%E3%80%8D%E4%B8%AD%E6%96%87%E7%BD%91%E6%88%91&ari=1&dbv=2&drs=1&pcs=1281x645&pss=1281x2771&cfv=0&cpl=34&chi=1&cce=true&cec=UTF-8&tlm=1453399100&rw=645&ltu=http%3A%2F%2Fwww.07net01.com%2F2015%2F04%2F814281.html&ltr=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3Dhqyv31tj7O4yxkPfljzN64D1XX9TKK9QXnFZwsND5iloLsJAwJvp---2W-WEkFqNNH12Lqo4z6YUjUhTE0cVRa%26wd%3D%26eqid%3D8acb61a7003de7fc000000055778f351&ecd=1&psr=1360x768&par=1298x768&pis=-1x-1&ccd=24&cja=true&cmi=60&col=zh-CN&cdo=-1&tcn=1467544569&qn=7ca9718650667a5b&tt=1467544569144.186.459.460" width="300" height="250" align="center,center" vspace="0" hspace="0" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true" style="box-sizing: border-box; border-width: 0px; border-style: initial; vertical-align: bottom; margin: 0px;"></iframe>

实用技巧
免费资源
超好玩的游戏
软件推荐
IT技术
资讯
其它
存储
IOS
招聘
wlan
cisco
R_S
交换
语音
路由
编程
建站
Linux
CCNA
前景
无线
QoS
下载
安全
博文
模拟器
CCIE
知识

© 2014 「电脑玩物」中文网我们只是「电脑玩物」     c7cc  聊城水处理  微粒子  汽车887  站长统计
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: