dp px 转换工具
public class DensityUtil {
private final static String TAG = "DensityUtil";
private static float density = 0f;
private static float defaultDensity = 1.5f;// 高分辨率的手机density普遍接近1.5
private DensityUtil() { }
public static void setDensity(float density) {
DensityUtil.density = density;
}
public static float getDensity(Context context) {
return context.getResources().getDisplayMetrics().density;
}
public static int getScreenWidth(Context context){
return context.getResources().getDisplayMetrics().widthPixels;
}
public static int getScreenHeight(Context context){
return context.getResources().getDisplayMetrics().heightPixels;
}
/**
* 根据手机的分辨率 dp 转成px(像素)
*/
public static int dip2px(float dpValue) {
int px;
if (density == 0) {
Log.e(TAG,
"density is invalid, you should execute DensityUtil.getDensity(Context context) first");
px = (int) (dpValue * defaultDensity + 0.5f);
} else {
px = (int) (dpValue * density + 0.5f);
}
XLog.i(TAG, "px = " + px);
return px;
}
/**
* 根据手机的分辨率px(像素) 转成dp
*/
public static int px2dip(float pxValue) {
int dp;
if (density == 0) {
Log.e(TAG,
"density is invalid, you should execute DensityUtil.getDensity(Context context) first");
dp = (int) (pxValue / defaultDensity + 0.5f);
} else {
dp = (int) (pxValue / density + 0.5f);
}
XLog.i(TAG, "dp = " + dp);
return dp;
}
}
dp px 转换工具的更多相关文章
- android屏幕密度规律及dp px转换
px和dp(sp) 之间转化公式: 1 乘以(dp转px)或者除以(px转dp) scal缩放因子,在上浮0.5f /** * 密度转换像素 * */ public static int dip2p ...
- dp,px转换
public static int dip2px(Context context, float dpValue) { final float scale = context.getRes ...
- android px,dp,sp大小转换工具
package com.voole.playerlib.util; import android.content.Context; /** * Android大小单位转换工具类<br/> ...
- DensityUtil【尺寸转换工具类(px、dp互相转换)】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 用于项目中dp.px.sp之间的转换以及指定缩放值下的转换. 效果图 暂不需要 代码分析 常用的方法是px2dip.dip2px: ...
- (转)Android中px与dip,sp与dip等的转换工具类
功能 通常在代码中设置组件或文字大小只能用px,通过这个工具类我们可以把dip(dp)或sp为单位的值转换为以px为单位的值而保证大小不变.方法中的参数请参考http://www.cnblogs.co ...
- 我的Android进阶之旅------>Android关于dp(dip)、sp转px的工具类
下面是一个工具类,提供了dp.sp.px之间相互转化的方法. import android.content.Context; /** * dp.sp 转换为 px 的工具类<br> * & ...
- 在线px转换rem工具
今天推荐一个在线工具,在线px转换rem工具 只要输入1rem = 多少px即可在线转换 和cssrem插件差不多的功能 rem在线转换工具: http://www.ofmonkey.com/fr ...
- [转]Android中dp,px,sp概念梳理以及如何做到屏幕适配
http://blog.csdn.net/jiangwei0910410003/article/details/40509571 今天又开始我的App开发,因为之前一直做的是SDK,所以涉及到界面UI ...
- PixelUtils:像素转换工具
/** 像素转换工具 */ public class PixelUtils { /** * The context. */ private static Context mContext = Cust ...
随机推荐
- 关于odbc的彻底删除问题
最近在装一个软件,由于第一次安装产生了一个错误,于是我尝试在卸载之后,重新进行安装~但是,在安装过程当中出现了一个问题. NEWLRE ODBC data source already exists. ...
- codeforces 742D Arpa's weak amphitheater and Mehrdad's valuable Hoses ——(01背包变形)
题意:给你若干个集合,每个集合内的物品要么选任意一个,要么所有都选,求最后在背包能容纳的范围下最大的价值. 分析:对于每个并查集,从上到下滚动维护即可,其实就是一个01背包= =. 代码如下: #in ...
- CentOS7安装问题及解决方案记录
CentOS7系统已安装好: 一.我想要实现系统桌面化. 需要运行 yum 命令. 出现第一个error: 1.提示错误:can not find a valid baseurl 86_X64.... ...
- eclipse安装svn和maven插件以及m2e-extras
相关介绍: 1.SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS.互联网上很多版本控制服务已从CVS迁移到Su ...
- iOS(视图控制器转场)
转场需要提供转场代理,不使用默认的代理则需要自己实现代理方式,有UINavigationController.UITabBarController.UIViewController三种代理,实现以下三 ...
- 模板函数 使用的默认void 模板报错
You must specify the compiler argument -std=c++0x to avoid the following error: test.cpp:5:13: error ...
- Redis(三)节省内部空间优化
总体原则:key的名称不易过长,剩下的所有 能用纯数字表示的尽量用 Redis的每一个键值都是用一个redisObject结构体表示的结构体中有: 键值的类型(string/list/hash/ ...
- 纯JS实现中国行政区域上下联动选择地址
一.实现目的: 如标题所述,通过JS来实现地址的选取,上一篇博客介绍的方式是通过java读取txt资源文件来实现地址的选择,通过ajax方式访问服务器实现省市区联动.此篇中将介绍如何使用JS实现相同功 ...
- fastjson将json字符串转化成bean对象解析出错的检查方法
我的情况是:解析第一层数据成功,解析第二层嵌套的数据失败.如: { "response": { "resultcode": "0", &qu ...
- Spring HTTP下载
(1)HTTP 协议可以在客户端和服务器之间传递任何类型的文件. HTTP协议下载文档到客户端时候, 必须通过响应头Content-Type设置文件类型. 例如: contentType=text/h ...