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. servlet service() for servlet jsp throws null pointer exception

    这么一件小事折腾了一个多小时,google也上不去,对百度我就不吐槽什么了,最后用bing查出了满意的结果.一般程序中不要显式把jsp.jar和servlet.jar放在Lib目录下,因为tomcat ...

  2. yum源使用(转)

    一.yum 简介 yum,是Yellow dog Updater, Modified 的简称,是杜克大学为了提高RPM 软件包安装性而开发的一种软件包管理器.起初是由yellow dog 这一发行版的 ...

  3. docker安装

    系统要求:需要一个64位的centos7操作系统和版本3.10或更高版本的Linux内核 开始安装: uname -r   //查看内核版本yum -y update //更新系统更新到最新 #安装d ...

  4. tesseract配置过程

    tesseract配置过程: 1. 为了避免配置环境变量,可以先下载一个 tesseract-ocr-setup-3.02.02.exe(tesseract配置文件夹里有),然后安装(假设安装目录为D ...

  5. Window10+VS2015+DevExpress.net 15.1.7完美破解(图)

    终于找到一个可用的破解工具了,并更新到最新的组件包DevExpressComponents-15.1.7.15288.exe,先看图 破解方法: 先安装DevExpressUniversalTrial ...

  6. 【Android端APP 安装包检查】安装包检查具体内容及实现方法

    一.安装包检查的具体包含内容有哪些? 1.安装包检查的一般内容包括: 安装包基本信息检查: 文件大小: xx MB 包名: com.xx 名称:  xx 本次安装包证书与外网证书对比一致性:是 版本号 ...

  7. 关于JS 事件冒泡和onclick,click,on()事件触发顺序

    今天在给JQgrid中的标签添加click事件的时候,发现一个问题. JQgrid的table中,点击任何位置,都会勾选点击行的checkbox,而我希望在点击我的标签的时候,不要勾选checkbox ...

  8. 利用matlab摄像机标定

    (1)输入图像 "Image names"键 Matlab的图形窗口显示出20幅靶标图像 (2) 提取角点 "Extract grid corners"键. 输 ...

  9. 使用re-sign.jar对apk进行重签名

    准备: ① re-sign.jar重签名工具:(下载地址为:http://troido.de/downloads/category/1): ② 待重签名的apk:      重签名步骤: ① 右键re ...

  10. ORA-20000:ORU-10027:buffer overflow,limit of 2000 bytes.

     ORA-20000:ORU-10027:buffer overflow,limit of 2000 bytes.  这是因为在过程中用到了dbms_output.put_line()在服务器端输出信 ...