具体的转换方法如下,网上都是有的,但是自己看了之后感觉还是有点不明不白的,具体为什么呢,可以继续看后面.
/** * 根据手机的分辨率从 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); }
首先先列出来相关的方法:
Resources.java
public Resources(AssetManager assets, DisplayMetrics metrics, Configuration config, CompatibilityInfo compInfo) { mAssets = assets; mMetrics.setToDefaults(); mCompatibilityInfo = compInfo; updateConfiguration(config, metrics); assets.ensureStringBlocks(); }
DisplayMetrics.java 相关方法
public void setToDefaults() { widthPixels = 0; heightPixels = 0; density = DENSITY_DEVICE / (float) DENSITY_DEFAULT; densityDpi = DENSITY_DEVICE; scaledDensity = density; xdpi = DENSITY_DEVICE; ydpi = DENSITY_DEVICE; noncompatWidthPixels = widthPixels; noncompatHeightPixels = heightPixels; noncompatDensity = density; noncompatDensityDpi = densityDpi; noncompatScaledDensity = scaledDensity; noncompatXdpi = xdpi; noncompatYdpi = ydpi; } public static int DENSITY_DEVICE = getDeviceDensity(); private static int getDeviceDensity() { // qemu.sf.lcd_density can be used to override ro.sf.lcd_density // when running in the emulator, allowing for dynamic configurations. // The reason for this is that ro.sf.lcd_density is write-once and is // set by the init process when it parses build.prop before anything else. return SystemProperties.getInt("qemu.sf.lcd_density", SystemProperties.getInt("ro.sf.lcd_density", DENSITY_DEFAULT)); } /** * The reference density used throughout the system. */ public static final int DENSITY_DEFAULT = DENSITY_MEDIUM; /** * Standard quantized DPI for medium-density screens. */ 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 里面可以设置该值.
相关推荐
Android dp和px/sp等单位相互转换
工具类dp和px转换
Android像素转换dp转px或相反
在android开发中布局文件要用DP进行布局,但是美工在设置的时候一般都是用PX,美工按照800*480分辨率设计完了,程序人员可以直接使用PX进行布局测试,该工具可以再程序完成时将PX直接转换为DP 方便适配
android 里面有个dp和px转换,sp字体的大小,对这些进行了详细的转换分析
自己撸的一个px,dp互转工具,可以选择ldpi,mdpi,hdpi,xhdpi,xxhdpi,xxxhdpi进行转换
这是一个很详细的转换类 可以很直观的实现 你想要的转换
本篇文章是对在Android中dp和px之间进行转换的实现方法进行了分析介绍,需要的朋友参考下
一个用于屏幕适配时必不可少的工具类android dx和dp 像素和dp之间转换的工具类
将px值转换成dp,暂时只支持标准手机设备的转换(hdpi,xhdpi,xxhdpi,520dpi)
在Android开发中dp和px,sp和px之间的转换时必不可少的。下面脚本之家小编给大家带来了android中px和dp,px和sp之间的转换方法,感兴趣的朋友一起看看吧
描述android的dip dp px dp等单位及其转换关系
Android常用共通工具类,包括dp与px转换,获取屏幕密度,获取versionName和versionCode,获取网络连接状态等等。
android布局单位sp,dp,pt,px的用法和区别。
DP.PX.SP转换工具类,目前只有1.0版本, DpSpPxConvertUtils
android开发之dip,dp与px像素之间的转换工具,博文介绍:http://blog.csdn.net/qq_21376985/article/details/51704703
dip dp px sp区别 dip dp px sp区别 dip dp px sp区别