尺寸单位转换
2016-05-04 09:41
197 查看
//转换dip为px public static int convertDipOrPx(Context context, int dip) { float scale = context.getResources().getDisplayMetrics().density; return (int)(dip*scale + 0.5f*(dip>=0?1:-1)); } //转换px为dip public static int convertPxOrDip(Context context, int px) { float scale = context.getResources().getDisplayMetrics().density; return (int)(px/scale + 0.5f*(px>=0?1:-1)); } public static int sp2px(Context context, float spValue) { float fontScale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (spValue * fontScale + 0.5f); } public static int px2sp(Context context, float pxValue) { float fontScale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (pxValue / fontScale + 0.5f); }
相关文章推荐
- [leetcode] 218. The Skyline Problem 解题报告
- 逻辑(logistic)回归
- 第十二章 springboot + mongodb(复杂查询)
- iOS 短信验证码倒计时按钮的实现
- 蓝牙BLE与设备交互研发录一
- Xcode7中你一定要知道的炸裂调试神技
- VC读写注册表类及用法
- Ativity、Fragment、Loader生命周期的关系
- IOS9 适配
- ASP.NET 2.0中的数据操作之八:使用两个DropDownList过滤的主/从报表
- UVA 12825 dp
- iOS UICollectionView基本属性 和 代理方法
- Eclipse将项目共享至新的SVN资源库
- c++ primer(第五版)学习笔记及习题答案代码版(第四章)表达式
- WebView的简单使用
- Apache Shiro权限管理框架
- git status对应中文名称
- 发生android.view.ViewRoot$CalledFromWrongThreadException异常的解决方案
- Junit代码
- used by istringstream UVA 12936