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 转换工具的更多相关文章

  1. android屏幕密度规律及dp px转换

    px和dp(sp) 之间转化公式: 1  乘以(dp转px)或者除以(px转dp) scal缩放因子,在上浮0.5f /** * 密度转换像素 * */ public static int dip2p ...

  2. dp,px转换

    public static int dip2px(Context context, float dpValue) {        final float scale = context.getRes ...

  3. android px,dp,sp大小转换工具

    package com.voole.playerlib.util; import android.content.Context; /** * Android大小单位转换工具类<br/> ...

  4. DensityUtil【尺寸转换工具类(px、dp互相转换)】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 用于项目中dp.px.sp之间的转换以及指定缩放值下的转换. 效果图 暂不需要 代码分析 常用的方法是px2dip.dip2px: ...

  5. (转)Android中px与dip,sp与dip等的转换工具类

    功能 通常在代码中设置组件或文字大小只能用px,通过这个工具类我们可以把dip(dp)或sp为单位的值转换为以px为单位的值而保证大小不变.方法中的参数请参考http://www.cnblogs.co ...

  6. 我的Android进阶之旅------>Android关于dp(dip)、sp转px的工具类

    下面是一个工具类,提供了dp.sp.px之间相互转化的方法. import android.content.Context; /** * dp.sp 转换为 px 的工具类<br> * & ...

  7. 在线px转换rem工具

    今天推荐一个在线工具,在线px转换rem工具 只要输入1rem = 多少px即可在线转换 和cssrem插件差不多的功能   rem在线转换工具: http://www.ofmonkey.com/fr ...

  8. [转]Android中dp,px,sp概念梳理以及如何做到屏幕适配

    http://blog.csdn.net/jiangwei0910410003/article/details/40509571 今天又开始我的App开发,因为之前一直做的是SDK,所以涉及到界面UI ...

  9. PixelUtils:像素转换工具

    /** 像素转换工具 */ public class PixelUtils { /** * The context. */ private static Context mContext = Cust ...

随机推荐

  1. WCF 服务调用 QueryRun

    通过AX2012的WCF服务调用AX2012的方法时,如果方法里调用了QueryRun对象时,会报错,报错信息如下:System.ServiceModel.FaultException: 无法将类型为 ...

  2. 安装Nexus

  3. crm 2013邮箱设置 “允许使用凭据进行电子邮件处理” 被禁用的解决

    记录一下: 在CRM 2013/2015的邮箱设置时发现“允许使用凭据进行电子邮件处理” 选项被禁用而且无法输入凭证(如下图): 查阅官方说明得知考虑邮件安全性只能在IFD部署或https访问模式下才 ...

  4. 非正常关闭myeclicps后,出现错误Errors occurred during the build.的解决方法

    我的myeclicps是10.7版本由于非正常关闭,在启动tomcat时候出现了问题. 解决法案: 1.关闭myeclicps. 2.打开你的myeclicps的工作空间(workspace自己设置的 ...

  5. C6000代码层面优化(一)

    2014年8月7日,看了一片很长见识的博文,关于DSP如何优化的,有一个问题没有搞通,“百度”一下关键字,居然搜查了一模一样的博文N片,现在也搞不懂这篇博文的原创作者是谁了.反正我感觉直接转摘过去,要 ...

  6. 操作素有的ul的元素

    1 $(function() { var all = $(".test"); $(".test").each(function() { var y = $(th ...

  7. sql一个表中两个字段合并求和

    sql一个表中两个字段,合并求和 SELECT SUM(字段a+'.'+字段b) as total  from TABLE

  8. XdbxAnalysis

    Tree: TXdbxAnalysis; FDataStream: TMemoryStream; {FDataStream:= TMemoryStream.Create;    FDataStream ...

  9. linux命令(3):pwd命令

    Linux中用 pwd 命令来查看”当前工作目录“的完整路径. 简单得说,每当你在终端进行操作时,你都会有一个当前工作目录. 在不太确定当前位置时,就会使用pwd来判定当前目录在文件系统内的确切位置. ...

  10. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

    控制台包空指针后跟这个异常,是因为控制层调用service时的失败,无法读到sql,问题在于controller在引入的service没有自动装配,在引入多个service时,每个service都要自 ...