Skip to content

Commit dbe1813

Browse files
committed
Improve ScreenUtils
1 parent d2a09b4 commit dbe1813

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

autosize/src/main/java/me/jessyan/autosize/AutoSizeConfig.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ public final class AutoSizeConfig {
8484
private int mScreenWidth;
8585
/**
8686
* 设备的屏幕总高度, 单位 px, 如果 {@link #isUseDeviceSize} 为 {@code false}, 屏幕总高度会减去状态栏的高度
87-
* 如果有导航栏也会减去导航栏的高度
8887
*/
8988
private int mScreenHeight;
9089
/**
@@ -96,8 +95,8 @@ public final class AutoSizeConfig {
9695
private boolean isBaseOnWidth = true;
9796
/**
9897
* 此字段表示是否使用设备的实际尺寸做适配
99-
* {@link #isUseDeviceSize} 为 {@code true} 表示屏幕高度 {@link #mScreenHeight} 包含状态栏的高度, 如果有导航栏也会包含导航栏的高度
100-
* {@link #isUseDeviceSize} 为 {@code false} 表示 {@link #mScreenHeight} 会减去状态栏的高度, 如果有导航栏也会减去导航栏的高度, 默认为 {@code true}
98+
* {@link #isUseDeviceSize} 为 {@code true} 表示屏幕高度 {@link #mScreenHeight} 包含状态栏的高度
99+
* {@link #isUseDeviceSize} 为 {@code false} 表示 {@link #mScreenHeight} 会减去状态栏的高度, 默认为 {@code true}
101100
*/
102101
private boolean isUseDeviceSize = true;
103102
/**
@@ -283,7 +282,7 @@ public AutoSizeConfig setBaseOnWidth(boolean baseOnWidth) {
283282
/**
284283
* 是否使用设备的实际尺寸做适配
285284
*
286-
* @param useDeviceSize {@code true} 为使用设备的实际尺寸 (包含状态栏, 导航栏), {@code false} 为不使用 (不包含状态栏, 导航栏)
285+
* @param useDeviceSize {@code true} 为使用设备的实际尺寸 (包含状态栏), {@code false} 为不使用设备的实际尺寸 (不包含状态栏)
287286
* @see #isUseDeviceSize 详情请查看这个字段的注释
288287
*/
289288
public AutoSizeConfig setUseDeviceSize(boolean useDeviceSize) {
@@ -389,7 +388,7 @@ public int getScreenWidth() {
389388
* @return {@link #mScreenHeight}
390389
*/
391390
public int getScreenHeight() {
392-
return isUseDeviceSize() ? mScreenHeight : mScreenHeight - ScreenUtils.getStatusBarHeight() - ScreenUtils.getHeightOfNavigationBar(getApplication());
391+
return isUseDeviceSize() ? mScreenHeight : mScreenHeight - ScreenUtils.getStatusBarHeight();
393392
}
394393

395394
/**
@@ -479,7 +478,7 @@ public void setScreenWidth(int screenWidth) {
479478
/**
480479
* 设置屏幕高度
481480
*
482-
* @param screenHeight 屏幕高度 (包含状态栏和导航栏)
481+
* @param screenHeight 屏幕高度 (需要包含状态栏)
483482
*/
484483
public void setScreenHeight(int screenHeight) {
485484
Preconditions.checkArgument(screenHeight > 0, "screenHeight must be > 0");

autosize/src/main/java/me/jessyan/autosize/utils/ScreenUtils.java

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,34 @@ public static int getStatusBarHeight() {
5050
return result;
5151
}
5252

53+
/**
54+
* 获取当前的屏幕尺寸
55+
*
56+
* @param context {@link Context}
57+
* @return 屏幕尺寸
58+
*/
5359
public static int[] getScreenSize(Context context) {
5460
int[] size = new int[2];
5561

62+
WindowManager w = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
63+
Display d = w.getDefaultDisplay();
64+
DisplayMetrics metrics = new DisplayMetrics();
65+
d.getMetrics(metrics);
66+
67+
size[0] = metrics.widthPixels;
68+
size[1] = metrics.heightPixels;
69+
return size;
70+
}
71+
72+
/**
73+
* 获取原始的屏幕尺寸
74+
*
75+
* @param context {@link Context}
76+
* @return 屏幕尺寸
77+
*/
78+
public static int[] getRawScreenSize(Context context) {
79+
int[] size = new int[2];
80+
5681
WindowManager w = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
5782
Display d = w.getDefaultDisplay();
5883
DisplayMetrics metrics = new DisplayMetrics();
@@ -90,15 +115,8 @@ public static int getHeightOfNavigationBar(Context context) {
90115
}
91116
}
92117

93-
Display d = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
94-
95-
int realHeight = getScreenSize(context)[1];
96-
97-
DisplayMetrics displayMetrics = new DisplayMetrics();
98-
d.getMetrics(displayMetrics);
99-
100-
int displayHeight = displayMetrics.heightPixels;
101-
118+
int realHeight = getRawScreenSize(context)[1];
119+
int displayHeight = getScreenSize(context)[1];
102120
return realHeight - displayHeight;
103121
}
104122
}

0 commit comments

Comments
 (0)