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条速度优 ...
随机推荐
- 【WIP】Rails devise导入与使用方法
创建: 2017/09/07 更新: 2017/10/14 标题加上[WIP] 源代码: https://github.com/plataformatec/devise 命令行内容总结 安 ...
- GYM 100741A Queries(树状数组)
A. Queries time limit per test 0.25 seconds memory limit per test 64 megabytes input standard input ...
- 【LuoguP5004】 专心OI - 跳房子
首先这是一道计数类DP,那我们得先推式子,经过瞎掰乱凑,经过认真分析,我们可以得到这样的方程 F(N)=F(0)+F(1)+....+F(N-M-1) 所有F初值为1,F(1)=2 ANS=F(N+M ...
- 第一课trie 树 POJ 2001
最短前缀(Openjudge上抄的) 总时间限制: 1000ms 内存限制: 65536kB 描述 一个字符串的前缀是从该字符串的第一个字符起始的一个子串.例如 "carbon"的 ...
- C - Arrival of the General
Problem description A Ministry for Defense sent a general to inspect the Super Secret Military Squad ...
- SQLServer2008 关于数据转换
全进位 select cast(ceiling(2.1111) as dec(18,0)) 结果:3
- 上传文件到linux系统方法
linux,windows filezilla 方法/步骤 下载 Filezilla client工具,此客户端为免费软件,下载完成后安装,安装一路下一步在这里就不过多阐述了,下载地址 htt ...
- Java关于反射的用法
一. 首先是准备一个需要反射的类 public class Person { private String name; private int age; public String sex; publ ...
- 努比亚(nubia) M2青春版 NX573J 解锁BootLoader 并进入临时recovery ROOT
努比亚(nubia) M2青春版 NX573J 解锁BootLoader 并进入临时recovery ROOT 工具下载链接:https://pan.baidu.com/s/1NfRTdXtdAZRi ...
- Christopher G. Atkeson 简介
有一个事实:双足机器人的稳定性问题单靠算法是搞不定的!!! 在2015 DARPA 机器人挑战赛中,许多参赛团队的机器人使用了Atlas,他们通过安装他们自己的软件并修改来让机器人保持平衡.来自WPI ...