4
4
5
5
public class DPIUtils {
6
6
public final static float BASE_DPI = 160.0f ;
7
-
7
+
8
8
/**
9
9
* Current DPI
10
10
*/
11
- public final static float DPI = BASE_DPI * Gdx . graphics . getDensity ();
11
+ public final static float DPI = BASE_DPI * getLogicalDensity ();
12
12
13
13
/**
14
14
* The Google recommendations are 48 dp -> 9mm for touchable elements
15
15
*/
16
- public final static float TOUCH_MIN_SIZE = 48 * Gdx . graphics . getDensity ();
16
+ public final static float TOUCH_MIN_SIZE = 48 * getLogicalDensity ();
17
17
18
18
/**
19
19
* The Google recommendations of space between UI objects is 8 dp
20
20
*/
21
- public final static float UI_SPACE = 8 * Gdx . graphics . getDensity ();
21
+ public final static float UI_SPACE = 8 * getLogicalDensity ();
22
22
23
23
/**
24
24
* The Google recommendations of space from bottom or top is 16 dp
25
25
*/
26
- public final static float MARGIN_SIZE = 16 * Gdx . graphics . getDensity ();
26
+ public final static float MARGIN_SIZE = 16 * getLogicalDensity ();
27
27
28
28
/**
29
29
* The Google recommendations are 56 dp for action buttons
30
30
*/
31
- public final static float BUTTON_SIZE = 56 * Gdx . graphics . getDensity ();
32
-
31
+ public final static float BUTTON_SIZE = 56 * getLogicalDensity ();
32
+
33
33
/**
34
34
* The Google recommendations are 24 dp for icons inside action buttons
35
35
*/
36
- public final static float ICON_SIZE = 24 * Gdx . graphics . getDensity ();
36
+ public final static float ICON_SIZE = 24 * getLogicalDensity ();
37
37
38
-
39
38
/**
40
39
* The Google recommendations are 8 dp for space between ui elements
41
40
*/
42
- public final static float SPACING = 8 * Gdx . graphics . getDensity ();
41
+ public final static float SPACING = 8 * getLogicalDensity ();
43
42
44
43
/**
45
44
* The screen height in DP
46
45
*/
47
- public final static float SCREEN_HEIGHT_DP = Gdx .graphics .getHeight ()
48
- / Gdx .graphics .getDensity ();
46
+ public final static float SCREEN_HEIGHT_DP = Gdx .graphics .getHeight () / getLogicalDensity ();
49
47
50
48
public final static float NORMAL_MULTIPLIER = 1.0f ; // 3-5"
51
49
public final static float LARGE_MULTIPLIER = 1.5f ; // 5-7"
52
50
public final static float XLARGE_MULTIPLIER = 2f ; // 8-10"
53
51
public final static float XXLARGE_MULTIPLIER = 2.5f ; // > 10"
54
52
53
+ public static final float getLogicalDensity () {
54
+ return Gdx .graphics .getDensity () / Gdx .graphics .getBackBufferScale ();
55
+ }
56
+
55
57
/**
56
58
* Calcs the button size based in screen size
57
59
*
@@ -60,7 +62,7 @@ public class DPIUtils {
60
62
public static float getPrefButtonSize () {
61
63
return getSizeMultiplier () * BUTTON_SIZE ;
62
64
}
63
-
65
+
64
66
/**
65
67
* Calcs the minimum size based in screen size
66
68
*
@@ -69,7 +71,7 @@ public static float getPrefButtonSize() {
69
71
public static float getTouchMinSize () {
70
72
return getSizeMultiplier () * TOUCH_MIN_SIZE ;
71
73
}
72
-
74
+
73
75
/**
74
76
* Calcs the margin size based in screen size
75
77
*
@@ -78,7 +80,7 @@ public static float getTouchMinSize() {
78
80
public static float getMarginSize () {
79
81
return getSizeMultiplier () * MARGIN_SIZE ;
80
82
}
81
-
83
+
82
84
/**
83
85
* Calcs the space between ui elements based in screen size
84
86
*
@@ -103,7 +105,7 @@ public static float getSpacing() {
103
105
// return NORMAL_MULTIPLIER;
104
106
//
105
107
// }
106
-
108
+
107
109
public static float getSizeMultiplier () {
108
110
float inches = pixelsToInches (Gdx .graphics .getWidth ());
109
111
float s = inches / 6f ;
@@ -113,17 +115,17 @@ public static float getSizeMultiplier() {
113
115
}
114
116
115
117
public static int dpToPixels (int dp ) {
116
- return (int ) (dp * Gdx . graphics . getDensity ());
118
+ return (int ) (dp * getLogicalDensity ());
117
119
}
118
-
120
+
119
121
public static int pixelsToDP (int pixels ) {
120
- return (int ) (pixels / Gdx . graphics . getDensity ());
122
+ return (int ) (pixels / getLogicalDensity ());
121
123
}
122
-
124
+
123
125
public static float pixelsToInches (int pixels ) {
124
- return ( float ) pixels / DPI ;
126
+ return pixels / DPI ;
125
127
}
126
-
128
+
127
129
public static float ptToPixels (float pts ) {
128
130
return pts * 72 / DPI ;
129
131
}
0 commit comments