-
Notifications
You must be signed in to change notification settings - Fork 4
/
SizeUtils.java
executable file
·63 lines (56 loc) · 1.67 KB
/
SizeUtils.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
56
57
58
59
60
61
62
63
package com.bytesizebit.androidutils;
import android.content.Context;
/***********
* Android Utils
* Created by Shahar Barsheshet on 17/10/2015.
* bytesizebit@gmail.com
* www.bytesizebit.com
***********/
public class SizeUtils {
private SizeUtils() {
}
/**
* Convert DP to PX
*
* @param context the context
* @param dpValue size in dp
* @return size in pixels
*/
public static int dpToPx(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* Convert PX to DP
*
* @param context the context
* @param pxValue size in pixels
* @return size in dp
*/
public static int pxToDp(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
/**
* Convert SP to PX
*
* @param context the context
* @param spValue size in sp
* @return
*/
public static int spToPx(Context context, float spValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue * fontScale + 0.5f);
}
/**
* Convert PX to SP
*
* @param context the context
* @param pxValue size in px
* @return size in sp
*/
public static int pxToSp(Context context, float pxValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (pxValue / fontScale + 0.5f);
}
}