Skip to content

Commit 00af516

Browse files
committed
修改 helper 的 mode 参数传递
1 parent b05e064 commit 00af516

File tree

4 files changed

+28
-19
lines changed

4 files changed

+28
-19
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ dependencies {
4040
testImplementation 'junit:junit:4.12'
4141
androidTestImplementation 'com.android.support.test:runner:1.0.2'
4242
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
43-
// implementation project(path: ':screen-helper')
44-
implementation 'vip.ruoyun.helper:screen-helper:1.0.1'
43+
implementation project(path: ':screen-helper')
44+
// implementation 'vip.ruoyun.helper:screen-helper:1.0.1'
4545

4646
}
4747

app/src/main/java/vip/ruoyun/screenlayout/Helper.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Helper {
1919
@JvmStatic
2020
fun apply(resources: Resources): Resources {
2121
if (isOpenScreen) {
22-
return ScreenHelper.applyAdapt(resources, 480f, ScreenHelper.ScreenMode.WIDTH_DP)
22+
return ScreenHelper.applyAdapt(resources, 480f, ScreenHelper.WIDTH_DP)
2323
} else {
2424
return ScreenHelper.closeAdapt(resources)
2525
}

screen-helper/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,8 @@ android {
1717
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
1818
}
1919
}
20+
}
2021

22+
dependencies {
23+
implementation 'com.android.support:support-annotations:28.0.0'
2124
}

screen-helper/src/main/java/vip/ruoyun/screen/ScreenHelper.java

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,24 @@
33
import android.content.Context;
44
import android.content.res.Configuration;
55
import android.content.res.Resources;
6+
import android.support.annotation.IntDef;
67
import android.util.DisplayMetrics;
78

9+
import java.lang.annotation.Retention;
10+
import java.lang.annotation.RetentionPolicy;
811
import java.lang.reflect.Field;
912
import java.util.ArrayList;
1013
import java.util.List;
1114

1215
public 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

Comments
 (0)