Android之px、dp、sp之间相互转换
2015-12-05 20:15
537 查看
在做Android的UI开发时,通常会用到三种度量单位——px、dp和sp。其中:
px:pixels,像素。不同设备显示效果相同。
dp(dip):device independent pixels,设备独立像素。 不同设备有不同的显示效果,和设备硬件有关,不依赖像素。如果设置表示长度、高度等属性时可以使用dp,与密度无关
sp:scaled pixels,放大像素。主要用于字体显示(best for textsize)。设置字体,需要使用sp,sp除了与密度无关外,还与scale无关。
px、dp和sp之间的相互转换如下:
其中在DisplayMetrics类中属性density如下定义:
在DisplayMetrics类中属性scaledDensity如下定义:
px:pixels,像素。不同设备显示效果相同。
dp(dip):device independent pixels,设备独立像素。 不同设备有不同的显示效果,和设备硬件有关,不依赖像素。如果设置表示长度、高度等属性时可以使用dp,与密度无关
sp:scaled pixels,放大像素。主要用于字体显示(best for textsize)。设置字体,需要使用sp,sp除了与密度无关外,还与scale无关。
px、dp和sp之间的相互转换如下:
/** * 将px值转换为dp(dip)值,保证尺寸大小不变 * * @param pxValue * @param scale (DisplayMetrics类中属性density) * @return */ public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); }
/** * 将px值转换为sp值,保证文字大小不变 * * @param pxValue * @param fontScale (DisplayMetrics类中属性scaledDensity) * @return */ public static int px2sp(Context context, float pxValue) { final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (pxValue / fontScale + 0.5f); }
/** * 将dp(dip)值转换为px值,保证尺寸大小不变 * * @param dipValue * @param scale(DisplayMetrics类中属性density) * @return */ public static int dip2px(Context context, float dipValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dipValue * scale + 0.5f); }
/** * 将sp值转换为px值,保证文字大小不变 * * @param spValue * @param fontScale (DisplayMetrics类中属性scaledDensity) * @return */ public static int sp2px(Context context, float spValue) { final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (spValue * fontScale + 0.5f); }
其中在DisplayMetrics类中属性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. * * <p>This value does not exactly follow the real screen size (as given by * {@link #xdpi} and {@link #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). * * @see #DENSITY_DEFAULT */ public float density;
在DisplayMetrics类中属性scaledDensity如下定义:
/** * A scaling factor for fonts displayed on the display. This is the same * as {@link #density}, except that it may be adjusted in smaller * increments at runtime based on a user preference for the font size. */ public float scaledDensity;
相关文章推荐
- Android控件之Spinner
- android adb常用指令
- Android Studio中配置及使用OpenCV示例
- OpenCV4Android释疑: 透析Android以JNI调OpenCV的三种方式(让OpenCVManager永不困扰)
- Android重力感应开发
- 判断Android应用是否安装、运行
- android 屏幕适配核心概念与单位笔记
- Android中3种方法实现back键动作
- Android语音识别——谷歌语音识别与百度语音识别
- Android坐标系与视图坐标系
- 【风马一族_Android】第4章Android常用基本控件
- Android性能优化之Listview(ViewHolder重用机制)
- Android中获取屏幕信息DisplayMetrics的用法
- Android之动画精讲一:从setTranslationX谈属性动画和view动画的区别
- How to add a linked source folder in Android Studio?
- Android实现监听的四种方式
- Android开发 使用jni对字符串加解密实现分析
- android项目浮窗的移动
- Android 你应该知道的学习资源 进阶之路贵在坚持
- Android TabLayout使用注意点。