@@ -24,117 +24,21 @@ public class DensityUtils {
2424
2525 private static final float DOT_FIVE = 0.5f ;
2626
27- /**
28- * dip to px
29- *
30- * @param context
31- * @param dip
32- * @return
33- */
3427 public static int dip2px (Context context , float dip ) {
3528 float density = getDensity (context );
3629 return (int ) (dip * density + DensityUtils .DOT_FIVE );
3730 }
3831
39- /**
40- * px to dip
41- *
42- * @param context
43- * @param px
44- * @return
45- */
46- public static int px2dip (Context context , float px ) {
47- float density = getDensity (context );
48- return (int ) (px / density + DOT_FIVE );
49- }
5032
5133 private static DisplayMetrics sDisplayMetrics ;
5234
53-
54- /**
55- * get screen Size
56- * @param context
57- * @return int[] 长度2
58- */
59- public static final int [] getDisplay (Context context ) {
60- initDisplayMetrics (context );
61-
62- return new int []{sDisplayMetrics .widthPixels , sDisplayMetrics .heightPixels };
63- }
64-
65-
66- /**
67- * get screen width
68- *
69- * @param context
70- * @return
71- */
72- public static int getDisplayWidth (Context context ) {
73- initDisplayMetrics (context );
74- return sDisplayMetrics .widthPixels ;
75- }
76-
77- /**
78- * get screen height
79- *
80- * @param context
81- * @return
82- */
83- public static int getDisplayHeight (Context context ) {
84- initDisplayMetrics (context );
85- return sDisplayMetrics .heightPixels ;
86- }
87-
88- /**
89- * get screen density
90- *
91- * @param context
92- * @return
93- */
9435 public static float getDensity (Context context ) {
9536 initDisplayMetrics (context );
9637 return sDisplayMetrics .density ;
9738 }
9839
99-
100- /**
101- * get screen density dpi
102- *
103- * @param context
104- * @return
105- */
106- public static int getDensityDpi (Context context ) {
107- initDisplayMetrics (context );
108- return sDisplayMetrics .densityDpi ;
109- }
110-
111- /**
112- * init display metrics
113- *
114- * @param context
115- */
11640 private static synchronized void initDisplayMetrics (Context context ) {
11741 sDisplayMetrics = context .getResources ().getDisplayMetrics ();
11842 }
11943
120-
121- /**
122- * is landscape
123- *
124- * @param context
125- * @return
126- */
127- public static boolean isLandscape (Context context ) {
128- return context .getResources ().getConfiguration ().orientation == Configuration .ORIENTATION_LANDSCAPE ;
129- }
130-
131- /**
132- * is portrait
133- *
134- * @param context
135- * @return
136- */
137- public static boolean isPortrait (Context context ) {
138- return context .getResources ().getConfiguration ().orientation == Configuration .ORIENTATION_PORTRAIT ;
139- }
14044}
0 commit comments