android像素db转px或者px转dp(有案例)
2015-07-22 13:51
489 查看
源码地址:http://download.csdn.net/detail/a876434758/8922609
截图:
部分源码:
/**
* 转换工具类
*
* @author zihao 2014-09-17
*
*/
public class DensityUtil {
/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*
* @param context
* @param dpValue
* @return
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* 根据手机的分辨率从 px(像素) 的单位 转成为 dp
*
* @param context
* @param pxValue
* @return
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
截图:
部分源码:
/**
* 转换工具类
*
* @author zihao 2014-09-17
*
*/
public class DensityUtil {
/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*
* @param context
* @param dpValue
* @return
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* 根据手机的分辨率从 px(像素) 的单位 转成为 dp
*
* @param context
* @param pxValue
* @return
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
相关文章推荐
- 系统编译 找不到添加的内部资源 com.android.internal.R
- [Android Studio 权威教程]最实用的快捷键
- android代码创建布局
- Android如何设置圆角按钮
- 提高工作效率的16条Android开发小经验
- Android 四大组件学习之Service四
- Android Activity之---(生命周期)
- 人机交互实验:Android开发之人物移动、地图滑动、传感器、触屏的应用
- Caused by: android.view.WindowManager$BadTokenException: Unable to add window -
- Android 四大组件学习之Service三
- Android 事件
- Android之Camera的简单实例
- Android Context上下文,要注意的事项
- Android使用OpenGL ES2.0显示YUV,您的手机上的数据要解决两个方面的坐标
- android dialog 有关token的问题
- android 实现代码混淆
- Android Touch事件传递机制
- android 中屏幕各种分辨率识别
- Android _Smali中插入Toast
- 需要MARK一下,奇怪的ANDROID SDK自带的APK加密功能的问题