Android 中dp 和px 转换及原理分析

具体的转换方法如下,网上都是有的,但是自己看了之后感觉还是有点不明不白的,具体为什么呢,可以继续看后面.

Java代码   收藏代码
  1. /** 
  2.      * 根据手机的分辨率从 dp 的单位 转成为 px(像素) 
  3.      */  
  4.     public static int dip2px(Context context, float dpValue) {  
  5.         final float scale = context.getResources().getDisplayMetrics().density;  
  6.         return (int) (dpValue * scale + 0.5f);  
  7.     }  
  8.   
  9.     /** 
  10.      * 根据手机的分辨率从 px(像素) 的单位 转成为 dp 
  11.      */  
  12.     public static int px2dip(Context context, float pxValue) {  
  13.         final float scale = context.getResources().getDisplayMetrics().density;  
  14.         return (int) (pxValue / scale + 0.5f);  
  15.     }  

 

首先先列出来相关的方法:

Resources.java

Java代码   收藏代码
  1. public Resources(AssetManager assets, DisplayMetrics metrics,  
  2.             Configuration config, CompatibilityInfo compInfo) {  
  3.         mAssets = assets;  
  4.         mMetrics.setToDefaults();  
  5.         mCompatibilityInfo = compInfo;  
  6.         updateConfiguration(config, metrics);  
  7.         assets.ensureStringBlocks();  
  8. }  

 

DisplayMetrics.java 相关方法

Java代码   收藏代码
  1. public void setToDefaults() {  
  2.         widthPixels = 0;  
  3.         heightPixels = 0;  
  4.         density =  DENSITY_DEVICE / (float) DENSITY_DEFAULT;  
  5.         densityDpi =  DENSITY_DEVICE;  
  6.         scaledDensity = density;  
  7.         xdpi = DENSITY_DEVICE;  
  8.         ydpi = DENSITY_DEVICE;  
  9.         noncompatWidthPixels = widthPixels;  
  10.         noncompatHeightPixels = heightPixels;  
  11.         noncompatDensity = density;  
  12.         noncompatDensityDpi = densityDpi;  
  13.         noncompatScaledDensity = scaledDensity;  
  14.         noncompatXdpi = xdpi;  
  15.         noncompatYdpi = ydpi;  
  16.     }  
  17.       
  18.       
  19.      public static int DENSITY_DEVICE = getDeviceDensity();  
  20.        
  21.        
  22.       private static int getDeviceDensity() {  
  23.         // qemu.sf.lcd_density can be used to override ro.sf.lcd_density  
  24.         // when running in the emulator, allowing for dynamic configurations.  
  25.         // The reason for this is that ro.sf.lcd_density is write-once and is  
  26.         // set by the init process when it parses build.prop before anything else.  
  27.         return SystemProperties.getInt("qemu.sf.lcd_density",  
  28.                 SystemProperties.getInt("ro.sf.lcd_density", DENSITY_DEFAULT));  
  29.     }  
  30.       
  31.     /** 
  32.      * The reference density used throughout the system. 
  33.      */  
  34.     public static final int DENSITY_DEFAULT = DENSITY_MEDIUM;  
  35.       
  36.     /** 
  37.      * Standard quantized DPI for medium-density screens. 
  38.      */  
  39.     public static final int DENSITY_MEDIUM = 160;  

 

在 Resources.java 的构造函数里面调用DisplayMetrics 的setToDefaults 方法,使density 取值为DENSITY_DEVICE / (float) DENSITY_DEFAULT;

使density 最终的值和key:qemu.sf.lcd_density 和default_value:ro.sf.lcd_density 有关系,

系统里面一般并没有额外对key:qemu.sf.lcd_density 赋值,故取值ro.sf.lcd_density,而ro.sf.lcd_density 的值一般是在系统源码里面的设置好的了,比如:system.prop 里面可以设置该值.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值