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. HMC破解控制台密码

    一般我们装完HMC以后默认的登录控制台的账户是:admin 密码:abc123当我们可以自创建登录账户后,忘记密码了怎么办? 这里有两种方法: 1 由于HMC是suse系统,基于linux内核的,所以 ...

  2. Spring文件上传配置

    增加依赖jar包 <dependency> <groupId>commons-fileupload</groupId> <artifactId>comm ...

  3. 转: unix实际用户ID和有效用户ID解析

    今天在看APUE,这两个问题很难理解,GOOGLE一下,有篇文章总结的不错,看了一下才明白透彻了. 由于用户在UNIX下经常会遇到 SUID.SGID的概念,而且SUID和SGID涉及到系统安全,所以 ...

  4. Android_Activity生命周期

    通过前面一段时间的学习,我们很清楚我们的一系列操作都离不开的一个东西,就是我们的activity .接下来我们对 Activity 进行系统的总结. Activity 的四种基本状态 1.运行态(Ru ...

  5. 循序渐进Python3(十一) --1-- web之css

    css样式: css是英文Cascading Style Sheets的缩写,称为层叠样式表,用于对页面进行美化,CSS的可以使页面更加的美观. 基本上所有的html页面都或多或少的使用css.   ...

  6. ios调用系统相册、相机 显示中文标题、本地化多语言支持

    因为调用系统相册.相机需要显示中文,所以搞了半天才知道是在Project->info->Custom ios Target Properties 添加 Localizations 并加入C ...

  7. android中string.xml中%1$s、%1$d等的用法

    今天在研究前辈写的代码的时候,突然发现string里面出现了<stringname="item_recent_photo">最近拍摄%1$s</string> ...

  8. Winfrom实现圆角设计

    主要代码 public partial class Form1 : Form    {        public Form1()        {            InitializeComp ...

  9. start

    ------siwuxie095 start 启动另一个窗口运行指定的程序或命令 语法: START ["title"] [/D path] [/I] [/MIN] [/MAX] ...

  10. android studio 注释模板

    File->Settings->Editor->File and Code Templates-> Templates->class Includes->File ...