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 ...
随机推荐
- AngularJS 1.2.x 学习笔记(表单校验篇)
https://my.oschina.net/cokolin/blog/526911 摘要: 本文首发于 blog.csdn.net/vipshop_ebs/article/details/39472 ...
- 那些可能被你忽略的MySQL优化技巧
说明:本文中的内容适用于MySQL5.1-5.6版本,不保证新的版本中仍然适用; 且只针对于大部分常见应用场景,是否有效果应以基于实际业务数据的测试为准. 1 优先把列设置为NOT NULL 允许NU ...
- 提高mysql千万级大数据SQL查询优化30条经验(Mysql索引优化注意)
转自http://blog.163.com/zhangjie_0303/blog/static/9908270620146951355834/ 1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 w ...
- java:关于文件下载
开发中遇到的问题 一. 场景描述:用户先指定下载路径,服务器将资源文件下载到指定的路径. 首先:该系统采用的是B/S架构,即浏览器.服务器(.数据库). 单纯通过B/S架构,就想要在服务器中去操 ...
- MyEclipse/Eclipse新建项目并且部署到服务器(tomcat)下
1新建项目:file/new/web project/项目名/ok 2部署到tomcat下:tomcat右单机>>add/remove/选择项目/添加/完成(tomcat下有新建的项目) ...
- 自定义ViewGroup
ViewGroup --->A ViewGroup is a special view that can contain other views (called children.) The v ...
- gcd 最小公约数
int gcd(int a, int b) { ? a : gcd(b, a % b); }
- DUILIB圆形头象
#ifndef __UIHEADICON_H__#define __UIHEADICON_H__ /* 名称:圆形头像控件(派生CButtonUI类)*/ class CHeadUI: public ...
- js面向对象编程:if中可以使用那些作为判断条件呢?
作者来源http://www.2cto.com/kf/201407/314978.html搬运 在所有编程语言中if是最长用的判断之一,但在js中到底哪些东西可以在if中式作为判断表达式呢? 例如如何 ...
- JQuery源码解析-- 对象的创建
使用 $("a") 返回的对象就不再是一个简单的DOM对象了,而是一个复杂的JQuery对象. 那么JQuery是怎么创建对象的. 为了便于分析,我将JQuery中复杂的代码简化了 ...