Skip to content

Commit 2679130

Browse files
committed
see 03/18 log
1 parent 18395d7 commit 2679130

File tree

2 files changed

+44
-37
lines changed

2 files changed

+44
-37
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* `19/03/17` [fix] The ugly UI.
2+
* `19/03/14` [fix] AdaptScreenUtils didn't work on some HaWei tablet.
13
* `19/03/08` [add] LogUtils support multi process. Publish v1.23.7.
24
* `19/03/02` [fix] LogUtils#file.
35
* `19/02/28` [fix] ImageUtils#calculateInSampleSize. Publish v1.23.6.

utilcode/lib/src/main/java/com/blankj/utilcode/util/AdaptScreenUtils.java

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,39 @@
55
import android.util.Log;
66

77
import java.lang.reflect.Field;
8+
import java.util.ArrayList;
9+
import java.util.List;
810

911

1012
public final class AdaptScreenUtils {
1113

12-
private static boolean sIsInit;
13-
private static Field sMetricsField;
14+
private static List<Field> sMetricsFields;
1415

1516
/**
1617
* Adapt for the horizontal screen, and call it in [android.app.Activity.getResources].
1718
*/
18-
public static Resources adaptWidth(Resources resources, int designWidth) {
19-
DisplayMetrics dm = getDisplayMetrics(resources);
20-
float newXdpi = dm.xdpi = (dm.widthPixels * 72f) / designWidth;
21-
setAppDmXdpi(newXdpi);
19+
public static Resources adaptWidth(final Resources resources, final int designWidth) {
20+
float newXdpi = (Resources.getSystem().getDisplayMetrics().widthPixels * 72f) / designWidth;
21+
applyDisplayMetrics(resources, newXdpi);
2222
return resources;
2323
}
2424

2525
/**
2626
* Adapt for the vertical screen, and call it in [android.app.Activity.getResources].
2727
*/
28-
public static Resources adaptHeight(Resources resources, int designHeight) {
29-
DisplayMetrics dm = getDisplayMetrics(resources);
30-
float newXdpi = dm.xdpi = (dm.heightPixels * 72f) / designHeight;
31-
setAppDmXdpi(newXdpi);
28+
public static Resources adaptHeight(final Resources resources, final int designHeight) {
29+
float newXdpi = (Resources.getSystem().getDisplayMetrics().heightPixels * 72f) / designHeight;
30+
applyDisplayMetrics(resources, newXdpi);
3231
return resources;
3332
}
3433

3534
/**
3635
* @param resources The resources.
3736
* @return the resource
3837
*/
39-
public static Resources closeAdapt(Resources resources) {
40-
DisplayMetrics dm = getDisplayMetrics(resources);
41-
float newXdpi = dm.xdpi = dm.density * 72;
42-
setAppDmXdpi(newXdpi);
38+
public static Resources closeAdapt(final Resources resources) {
39+
float newXdpi = Resources.getSystem().getDisplayMetrics().density * 72f;
40+
applyDisplayMetrics(resources, newXdpi);
4341
return resources;
4442
}
4543

@@ -49,7 +47,7 @@ public static Resources closeAdapt(Resources resources) {
4947
* @param ptValue The value of pt.
5048
* @return value of px
5149
*/
52-
public static int pt2Px(float ptValue) {
50+
public static int pt2Px(final float ptValue) {
5351
DisplayMetrics metrics = Utils.getApp().getResources().getDisplayMetrics();
5452
return (int) (ptValue * metrics.xdpi / 72f + 0.5);
5553
}
@@ -60,33 +58,31 @@ public static int pt2Px(float ptValue) {
6058
* @param pxValue The value of px.
6159
* @return value of pt
6260
*/
63-
public static int px2Pt(float pxValue) {
61+
public static int px2Pt(final float pxValue) {
6462
DisplayMetrics metrics = Utils.getApp().getResources().getDisplayMetrics();
6563
return (int) (pxValue * 72 / metrics.xdpi + 0.5);
6664
}
6765

68-
private static void setAppDmXdpi(final float xdpi) {
69-
Utils.getApp().getResources().getDisplayMetrics().xdpi = xdpi;
66+
private static void applyDisplayMetrics(final Resources resources, final float newXdpi) {
67+
resources.getDisplayMetrics().xdpi = newXdpi;
68+
Utils.getApp().getResources().getDisplayMetrics().xdpi = newXdpi;
69+
applyOtherDisplayMetrics(resources, newXdpi);
7070
}
7171

72-
private static DisplayMetrics getDisplayMetrics(Resources resources) {
73-
DisplayMetrics realMetrics = getRealMetrics(resources);
74-
if (realMetrics != null) return realMetrics;
75-
return resources.getDisplayMetrics();
76-
}
77-
78-
private static DisplayMetrics getRealMetrics(final Resources resources) {
79-
if (!sIsInit) {
80-
DisplayMetrics ret = null;
72+
private static void applyOtherDisplayMetrics(final Resources resources, final float newXdpi) {
73+
if (sMetricsFields == null) {
74+
sMetricsFields = new ArrayList<>();
8175
Class resCls = resources.getClass();
8276
Field[] declaredFields = resCls.getDeclaredFields();
83-
while (ret == null && declaredFields != null && declaredFields.length > 0) {
77+
while (declaredFields != null && declaredFields.length > 0) {
8478
for (Field field : declaredFields) {
8579
if (field.getType().isAssignableFrom(DisplayMetrics.class)) {
8680
field.setAccessible(true);
87-
sMetricsField = field;
88-
ret = getMetricsFromField(resources);
89-
if (ret != null) break;
81+
DisplayMetrics tmpDm = getMetricsFromField(resources, field);
82+
if (tmpDm != null) {
83+
sMetricsFields.add(field);
84+
tmpDm.xdpi = newXdpi;
85+
}
9086
}
9187
}
9288
resCls = resCls.getSuperclass();
@@ -96,16 +92,25 @@ private static DisplayMetrics getRealMetrics(final Resources resources) {
9692
break;
9793
}
9894
}
99-
sIsInit = true;
100-
return ret;
95+
} else {
96+
applyMetricsFields(resources, newXdpi);
97+
}
98+
}
99+
100+
private static void applyMetricsFields(final Resources resources, final float newXdpi) {
101+
for (Field metricsField : sMetricsFields) {
102+
try {
103+
DisplayMetrics dm = (DisplayMetrics) metricsField.get(resources);
104+
if (dm != null) dm.xdpi = newXdpi;
105+
} catch (Exception e) {
106+
Log.e("AdaptScreenUtils", "applyMetricsFields: " + e);
107+
}
101108
}
102-
if (sMetricsField == null) return null;
103-
return getMetricsFromField(resources);
104109
}
105110

106-
private static DisplayMetrics getMetricsFromField(final Resources resources) {
111+
private static DisplayMetrics getMetricsFromField(final Resources resources, final Field field) {
107112
try {
108-
return (DisplayMetrics) sMetricsField.get(resources);
113+
return (DisplayMetrics) field.get(resources);
109114
} catch (Exception e) {
110115
Log.e("AdaptScreenUtils", "getMetricsFromField: " + e);
111116
return null;

0 commit comments

Comments
 (0)