PixelUtils:像素转换工具
/** 像素转换工具
*/
public class PixelUtils { /**
* The context.
*/
private static Context mContext = CustomApplcation.getInstance(); /**
* dp转 px.
*
* @param value the value
* @return the int
*/
public static int dp2px(float value) {
final float scale = mContext.getResources().getDisplayMetrics().densityDpi;
return (int) (value * (scale / 160) + 0.5f);
} /**
* dp转 px.
*
* @param value the value
* @param context the context
* @return the int
*/
public static int dp2px(float value, Context context) {
final float scale = context.getResources().getDisplayMetrics().densityDpi;
return (int) (value * (scale / 160) + 0.5f);
} /**
* px转dp.
*
* @param value the value
* @return the int
*/
public static int px2dp(float value) {
final float scale = mContext.getResources().getDisplayMetrics().densityDpi;
return (int) ((value * 160) / scale + 0.5f);
} /**
* px转dp.
*
* @param value the value
* @param context the context
* @return the int
*/
public static int px2dp(float value, Context context) {
final float scale = context.getResources().getDisplayMetrics().densityDpi;
return (int) ((value * 160) / scale + 0.5f);
} /**
* sp转px.
*
* @param value the value
* @return the int
*/
public static int sp2px(float value) {
Resources r;
if (mContext == null) {
r = Resources.getSystem();
} else {
r = mContext.getResources();
}
float spvalue = value * r.getDisplayMetrics().scaledDensity;
return (int) (spvalue + 0.5f);
} /**
* sp转px.
*
* @param value the value
* @param context the context
* @return the int
*/
public static int sp2px(float value, Context context) {
Resources r;
if (context == null) {
r = Resources.getSystem();
} else {
r = context.getResources();
}
float spvalue = value * r.getDisplayMetrics().scaledDensity;
return (int) (spvalue + 0.5f);
} /**
* px转sp.
*
* @param value the value
* @return the int
*/
public static int px2sp(float value) {
final float scale = mContext.getResources().getDisplayMetrics().scaledDensity;
return (int) (value / scale + 0.5f);
} /**
* px转sp.
*
* @param value the value
* @param context the context
* @return the int
*/
public static int px2sp(float value, Context context) {
final float scale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (value / scale + 0.5f);
} }
PixelUtils:像素转换工具的更多相关文章
- DensityUtil【尺寸转换工具类(px、dp互相转换)】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 用于项目中dp.px.sp之间的转换以及指定缩放值下的转换. 效果图 暂不需要 代码分析 常用的方法是px2dip.dip2px: ...
- Unicode编码解码在线转换工具
// Unicode编码解码在线转换工具 Unicode 是基于通用字符集(Universal Character Set)的标准来发展,并且同时也以书本的形式(The Unicode Standar ...
- android px,dp,sp大小转换工具
package com.voole.playerlib.util; import android.content.Context; /** * Android大小单位转换工具类<br/> ...
- Json与javaBean之间的转换工具类
/** * Json与javaBean之间的转换工具类 * * {@code 现使用json-lib组件实现 * 需要 * json-lib-2.4-jdk15.jar * ...
- Rsa加解密Java、C#、php通用代码 密钥转换工具
之前发了一篇"TripleDes的加解密Java.C#.php通用代码",后面又有项目用到了Rsa加解密,还是在不同系统之间进行交互,Rsa在不同语言的密钥格式不一样,所以过程中主 ...
- ubuntu下编码转换工具
ubuntu打开windows下的txt或者代码文件,经常会出现乱码, ubuntu自带一种转换工具,是命令行的,下面提供一种最简单的方法进行转换 比如要转换的文件为1.txt,进入1.txt的目录 ...
- 日期转换工具类 CommUtil.java
package com.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.ut ...
- 视频转换工具 Transmageddon
点这里 Transmageddon 是一个采用 Python 语言开发的视频转换工具,支持输出几乎所有的视频格式,同时也可以生成指定平台下的视频格式. 软件界面如下图所示
- Base64 图片转换工具
以前在写asp的后台的时候,有一个上传功能是必须的,那时候进行的图片预览(未上传前)其实就是获取本地的图片路径来显示图片,但是随着HTML5的出现,可以把图片通过编码来实现预览. 在雅虎的36条速度优 ...
随机推荐
- windows php文件下载地址
http://windows.php.net/downloads/releases/archives/
- 关于网站图片格式 png,jpg,
小图标用 png 采用无损压缩.可存储透明图片. 适合存储icon, logo 等颜色对比明显,又小的图片. 劣势:索引色数量有限,不适合大图片,颜色层次丰富. 大图片用 jpg 采用了压缩算法,会有 ...
- CABasicAnimation - 上下滑动动画
#import <UIKit/UIKit.h> @interface TJProgressView : UIView @property(nonatomic,assign)CGFloat ...
- checked、disabled在原生、jquery、vue下不同写法
以下是原生和jquery <!DOCTYPE html> <html> <head> <meta http-equiv="Content ...
- Android 清空缓存
APP开发中常有计算缓存大小和清空缓存的功能,此功能很常见,几乎每个应用都能看到,下面就用代码来实现此功能: 步骤为: 1.获取缓存路径 获取长时间保存的文件,Context.getExternalF ...
- 出现“ORA-28000:the account is locked”的解决办法
在Oracle 11g版本中,出于安全的考虑,所有Oracle的默认用户,包括SCOTT用户都被锁定.输入用户名和口令之后,会出现错误“ORA-28000:the account is locked” ...
- L4课程_Firebase_笔记分享_StudyJams_2017
最近才发现Study Jams China的官方论坛也支持MarkDown,所以就直接把笔记发在了那儿. http://www.studyjamscn.com/thread-21855-1-1.htm ...
- electron 学习
index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- selenium选错弹出层的下拉框
要先选中这个弹出层的form元素,再找下拉框 public void downSelectBox(){ driver.get("https://www.imooc.com/user/setp ...
- Docker 数据卷重复挂载测试
没想到一年没写博客了,这中间都是记在自己的笔记本上,大部分网上都有,这个好像没有,所以发上来吧! 本文是测试Docker容器(相同目录/父子目录)同时挂载到宿主机(同目录/不同目录)时的情况,废话少说 ...