其实都是以前保存的代码,最近发现自己的资料库很混乱,索性都整理成博客,方便以后自己要用的时候快速找到。
DisplayUtil.java
复制代码
DisplayParams.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55/** * 单位转换工具 * * @author carrey * */ public class DisplayUtil { /** * 将px值转换为dip或dp值,保证尺寸大小不变 * * @param pxValue * @param scale * (DisplayMetrics类中属性density) * @return */ public static int px2dip(float pxValue, float scale) { return (int) (pxValue / scale + 0.5f); } /** * 将dip或dp值转换为px值,保证尺寸大小不变 * * @param dipValue * @param scale * (DisplayMetrics类中属性density) * @return */ public static int dip2px(float dipValue, float scale) { return (int) (dipValue * scale + 0.5f); } /** * 将px值转换为sp值,保证文字大小不变 * * @param pxValue * @param fontScale * (DisplayMetrics类中属性scaledDensity) * @return */ public static int px2sp(float pxValue, float fontScale) { return (int) (pxValue / fontScale + 0.5f); } /** * 将sp值转换为px值,保证文字大小不变 * * @param spValue * @param fontScale * (DisplayMetrics类中属性scaledDensity) * @return */ public static int sp2px(float spValue, float fontScale) { return (int) (spValue * fontScale + 0.5f); } }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69/** * 屏幕参数工具 * * @author carrey * */ public class DisplayParams { /** 屏幕宽度——px */ public int screenWidth; /** 屏幕高度——px */ public int screenHeight; /** 屏幕密度——dpi */ public int densityDpi; /** 缩放系数——densityDpi/160 */ public float scale; /** 文字缩放系数 */ public float fontScale; /** 屏幕朝向 */ public int screenOrientation; /** 表示屏幕朝向垂直 */ public final static int SCREEN_ORIENTATION_VERTICAL = 1; /** 表示屏幕朝向水平 */ public final static int SCREEN_ORIENTATION_HORIZONTAL = 2; private static DisplayParams singleInstance; /** * 私有构造方法 * * @param context */ private DisplayParams(Context context) { DisplayMetrics dm = context.getResources().getDisplayMetrics(); screenWidth = dm.widthPixels; screenHeight = dm.heightPixels; densityDpi = dm.densityDpi; scale = dm.density; fontScale = dm.scaledDensity; screenOrientation = screenHeight > screenWidth ? SCREEN_ORIENTATION_VERTICAL : SCREEN_ORIENTATION_HORIZONTAL; } /** * 获取实例 * * @param context * @return */ public static DisplayParams getInstance(Context context) { if (singleInstance == null) { singleInstance = new DisplayParams(context); } return singleInstance; } /** * 获取新的实例 * * @param context * @return */ public static DisplayParams getNewInstance(Context context) { if (singleInstance != null) { singleInstance = null; } return getInstance(context); } }
使用示例:
复制代码
1
2
3
4
5
6
7
8
9/** * 设置文本大小 * @param textSizeSp 文本大小,单位是sp */ public void setTextSize(int textSizeSp) { DisplayParams displayParams = DisplayParams.getInstance(context); this.textSize = DisplayUtil.sp2px(textSizeSp, displayParams.fontScale); invalidate(); }
最后
以上就是辛勤保温杯最近收集整理的关于在java代码中进行px与dip(dp)、px与sp单位值的转换的全部内容,更多相关在java代码中进行px与dip(dp)、px与sp单位值内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复