33import android .content .Context ;
44import android .content .res .Configuration ;
55import android .content .res .Resources ;
6+ import android .support .annotation .IntDef ;
67import android .util .DisplayMetrics ;
78
9+ import java .lang .annotation .Retention ;
10+ import java .lang .annotation .RetentionPolicy ;
811import java .lang .reflect .Field ;
912import java .util .ArrayList ;
1013import java .util .List ;
1114
1215public class ScreenHelper {
1316
14- public interface ScreenMode {
15- int WIDTH_DP = 0x25 ;
16- int WIDTH_PT = 0x35 ;
17- int HEIGHT_PT = 0x45 ;
17+ public static final int WIDTH_DP = 0x25 ;
18+ public static final int WIDTH_PT = 0x35 ;
19+ public static final int HEIGHT_PT = 0x45 ;
20+
21+ @ IntDef ({WIDTH_DP , WIDTH_PT , HEIGHT_PT })
22+ @ Retention (RetentionPolicy .CLASS )
23+ @interface ScreenMode {
1824 }
1925
2026 private ScreenHelper () {
@@ -23,7 +29,7 @@ private ScreenHelper() {
2329 private static List <Field > sMetricsFields ;
2430 private static DisplayMetrics systemDm ;
2531
26- public static Resources applyAdapt (final Resources resources , float size , int screenMode ) {
32+ public static Resources applyAdapt (final Resources resources , float size , @ ScreenMode int screenMode ) {
2733 DisplayMetrics activityDm = resources .getDisplayMetrics ();
2834 if (null == systemDm ) {
2935 systemDm = Resources .getSystem ().getDisplayMetrics ();
@@ -70,15 +76,15 @@ public static Resources applyAdapt(final Resources resources, float size, int sc
7076 }
7177
7278
73- private static void change (int screenMode , final Resources resources , DisplayMetrics activityDm , DisplayMetrics systemDm , float size ) {
79+ private static void change (@ ScreenMode int screenMode , final Resources resources , DisplayMetrics activityDm , DisplayMetrics systemDm , float size ) {
7480 switch (screenMode ) {
75- case ScreenMode . WIDTH_DP :
81+ case WIDTH_DP :
7682 adaptWidthPixels (resources , activityDm , systemDm , size );
7783 break ;
78- case ScreenMode . HEIGHT_PT :
84+ case HEIGHT_PT :
7985 adaptHeightXdpi (resources , size , systemDm );
8086 break ;
81- case ScreenMode . WIDTH_PT :
87+ case WIDTH_PT :
8288 adaptWidthXdpi (resources , size , systemDm );
8389 break ;
8490 }
@@ -176,11 +182,11 @@ private static void resetResources(DisplayMetrics activityDm, DisplayMetrics sys
176182 public static int value2px (Context context , float value , int screenMode ) {
177183 DisplayMetrics metrics = context .getResources ().getDisplayMetrics ();
178184 switch (screenMode ) {
179- case ScreenMode . WIDTH_DP :
185+ case WIDTH_DP :
180186 default :
181187 return (int ) (value * metrics .density );
182- case ScreenMode . HEIGHT_PT :
183- case ScreenMode . WIDTH_PT :
188+ case HEIGHT_PT :
189+ case WIDTH_PT :
184190 return (int ) (value * metrics .xdpi / 72f + 0.5 );
185191 }
186192 }
@@ -191,14 +197,14 @@ public static int value2px(Context context, float value, int screenMode) {
191197 * @param pxValue The value of px.
192198 * @return value of pt
193199 */
194- public static int px2Value (Context context , float pxValue , int screenMode ) {
200+ public static int px2Value (Context context , float pxValue , @ ScreenMode int screenMode ) {
195201 DisplayMetrics metrics = context .getResources ().getDisplayMetrics ();
196202 switch (screenMode ) {
197- case ScreenMode . WIDTH_DP :
203+ case WIDTH_DP :
198204 default :
199205 return (int ) (pxValue / metrics .density );
200- case ScreenMode . HEIGHT_PT :
201- case ScreenMode . WIDTH_PT :
206+ case HEIGHT_PT :
207+ case WIDTH_PT :
202208 return (int ) (pxValue * 72 / metrics .xdpi + 0.5 );
203209 }
204210 }
0 commit comments