dp、sp 转换为 px 的工具类
public class DisplayUtil {
/**
* 将px值转换为dip或dp值,保证尺寸大小不变
*
* @param pxValue (DisplayMetrics类中属性density)
* @return
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
/**
* 将dip或dp值转换为px值,保证尺寸大小不变
*
* @param dipValue (DisplayMetrics类中属性density)
* @return
*/
public static int dip2px(Context context, float dipValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}
/**
* 将px值转换为sp值,保证文字大小不变
*
* @param pxValue (DisplayMetrics类中属性scaledDensity)
* @return
*/
public static int px2sp(Context context, float pxValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (pxValue / fontScale + 0.5f);
}
/**
* 将sp值转换为px值,保证文字大小不变
*
* @param spValue (DisplayMetrics类中属性scaledDensity)
* @return
*/
public static int sp2px(Context context, float spValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue * fontScale + 0.5f);
}
}
dp、sp 转换为 px 的工具类的更多相关文章
- 我的Android进阶之旅------>Android关于dp(dip)、sp转px的工具类
下面是一个工具类,提供了dp.sp.px之间相互转化的方法. import android.content.Context; /** * dp.sp 转换为 px 的工具类<br> * & ...
- Android单位转换 (px、dp、sp之间的转换工具类)
在Android开发中,涉及到屏幕视频问题的时候,px.dp.sp之间的转换比较重要的一部分,所以杨哥整理了一个工具类给大伙用. package com.zw.express.tool; import ...
- 【转载】[C#]枚举操作(从枚举中获取Description,根据Description获取枚举,将枚举转换为ArrayList)工具类
关键代码: using System; using System.Collections; using System.Collections.Generic; using System.Compone ...
- 数据持久化之SP的优化—送工具类
第一点:sp存储的是键值对 getSharedPreferences 第一个參数是你保存文件的名字,第个是保存的模式一般能够默觉得0 先看普通 使用SP 存储String类型字符串吧 SharedPr ...
- Android textView 动态设置代码字号大小,支持单位选项 dp,sp or px
setTextSize(TypedValue.COMPLEX_UNIT_PX,22); //22像素 setTextSize(TypedValue.COMPLEX_UNIT_SP,22); //22S ...
- c# dateTime格式转换为Unix时间戳工具类
using System; using System.Collections.Generic; using System.Text; namespace TJCFinanceWriteOff.BizL ...
- 将文本转换为json的工具类
JSONObject jsonObj = JSONObject.fromObject("文本"); 参考:https://www.cnblogs.com/joahyau/p/ ...
- java将小写金额转换为大写的工具类
public class Tool { private static final String UNIT = "万千佰拾亿千佰拾万千佰拾元角分"; ...
- 关于android setTextSize() 以及 px dip/dp sp的说明。。。。
Paint.setTextSize()单位为px,Android系统中,默认的单位是像素(px).也就是说,在没有明确说明的情况下,所有的大小设置都是以像素为单位.Paint.setTextSize传 ...
随机推荐
- LeetCode Maximum Product Subarray(枚举)
LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...
- Scut:参数导入方式(有遗留疑问)
先上一段代码: public EnvironmentSetting() { var appServer = GetServerSection(); var protocol = GetProtocol ...
- Spring Test 整合 JUnit 4 使用
这两天做Web开发,发现通过spring进行对象管理之后,做测试变得复杂了.因为所有的Bean都需要在applicationContext.xml中加载好,之后再通过@Resource去取得.如果每次 ...
- PS5穿越云层3D文字
妈的,搜狗浏览器有时候会出问题,保存的内容找不到了…… 视图--显示参考线或者“显示额外内容”会取消或者加上参考线,后者功能更强一些,ctrl+D有时可以代替后者的功能,后者可以去掉蒙版的参考线,前者 ...
- hdu 5100 Chessboard
http://acm.hdu.edu.cn/showproblem.php?pid=5100 在比赛时没看懂题就没看,结束之后,看了解题报告才知道怎么做. 解题报告: 首先,若n<k,则棋盘连一 ...
- mysql INNODB_TRX 事务表
demo:/root# mysql -uroot -pkjk7787czcb --socket=/data01/mysql/mysql.sock -e"show processlist&qu ...
- Best Time to Buy and Sell Stock——LeetCode
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- HDOJ 2200 Eddy's AC难题(数学组合概率题)
Problem Description Eddy是个ACMer,他不仅喜欢做ACM题,而且对于Ranklist中每个人的ac数量也有一定的研究,他在无聊时经常在纸上把Ranklist上每个人的ac题目 ...
- has-a关系——包含对象成员的类
#ifndef _STUDENT_H_ #define _STUDENT_H_ #include <iostream> #include <string> #include & ...
- java的内部类及匿名内部类
在Java中,允许一个类的定义位于另一个类的内部,前者称为内部类 内部类和外层封装它的类之间存在逻辑上的所属关系 Inner class一般用在定义它的类或语句块之内,在外部引用它时必须给出完整的名称 ...