提交 2679130d 编写于 作者: B Blankj

see 03/18 log

上级 18395d74
* `19/03/17` [fix] The ugly UI.
* `19/03/14` [fix] AdaptScreenUtils didn't work on some HaWei tablet.
* `19/03/08` [add] LogUtils support multi process. Publish v1.23.7.
* `19/03/02` [fix] LogUtils#file.
* `19/02/28` [fix] ImageUtils#calculateInSampleSize. Publish v1.23.6.
......
......@@ -5,30 +5,29 @@ import android.util.DisplayMetrics;
import android.util.Log;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
public final class AdaptScreenUtils {
private static boolean sIsInit;
private static Field sMetricsField;
private static List<Field> sMetricsFields;
/**
* Adapt for the horizontal screen, and call it in [android.app.Activity.getResources].
*/
public static Resources adaptWidth(Resources resources, int designWidth) {
DisplayMetrics dm = getDisplayMetrics(resources);
float newXdpi = dm.xdpi = (dm.widthPixels * 72f) / designWidth;
setAppDmXdpi(newXdpi);
public static Resources adaptWidth(final Resources resources, final int designWidth) {
float newXdpi = (Resources.getSystem().getDisplayMetrics().widthPixels * 72f) / designWidth;
applyDisplayMetrics(resources, newXdpi);
return resources;
}
/**
* Adapt for the vertical screen, and call it in [android.app.Activity.getResources].
*/
public static Resources adaptHeight(Resources resources, int designHeight) {
DisplayMetrics dm = getDisplayMetrics(resources);
float newXdpi = dm.xdpi = (dm.heightPixels * 72f) / designHeight;
setAppDmXdpi(newXdpi);
public static Resources adaptHeight(final Resources resources, final int designHeight) {
float newXdpi = (Resources.getSystem().getDisplayMetrics().heightPixels * 72f) / designHeight;
applyDisplayMetrics(resources, newXdpi);
return resources;
}
......@@ -36,10 +35,9 @@ public final class AdaptScreenUtils {
* @param resources The resources.
* @return the resource
*/
public static Resources closeAdapt(Resources resources) {
DisplayMetrics dm = getDisplayMetrics(resources);
float newXdpi = dm.xdpi = dm.density * 72;
setAppDmXdpi(newXdpi);
public static Resources closeAdapt(final Resources resources) {
float newXdpi = Resources.getSystem().getDisplayMetrics().density * 72f;
applyDisplayMetrics(resources, newXdpi);
return resources;
}
......@@ -49,7 +47,7 @@ public final class AdaptScreenUtils {
* @param ptValue The value of pt.
* @return value of px
*/
public static int pt2Px(float ptValue) {
public static int pt2Px(final float ptValue) {
DisplayMetrics metrics = Utils.getApp().getResources().getDisplayMetrics();
return (int) (ptValue * metrics.xdpi / 72f + 0.5);
}
......@@ -60,33 +58,31 @@ public final class AdaptScreenUtils {
* @param pxValue The value of px.
* @return value of pt
*/
public static int px2Pt(float pxValue) {
public static int px2Pt(final float pxValue) {
DisplayMetrics metrics = Utils.getApp().getResources().getDisplayMetrics();
return (int) (pxValue * 72 / metrics.xdpi + 0.5);
}
private static void setAppDmXdpi(final float xdpi) {
Utils.getApp().getResources().getDisplayMetrics().xdpi = xdpi;
private static void applyDisplayMetrics(final Resources resources, final float newXdpi) {
resources.getDisplayMetrics().xdpi = newXdpi;
Utils.getApp().getResources().getDisplayMetrics().xdpi = newXdpi;
applyOtherDisplayMetrics(resources, newXdpi);
}
private static DisplayMetrics getDisplayMetrics(Resources resources) {
DisplayMetrics realMetrics = getRealMetrics(resources);
if (realMetrics != null) return realMetrics;
return resources.getDisplayMetrics();
}
private static DisplayMetrics getRealMetrics(final Resources resources) {
if (!sIsInit) {
DisplayMetrics ret = null;
private static void applyOtherDisplayMetrics(final Resources resources, final float newXdpi) {
if (sMetricsFields == null) {
sMetricsFields = new ArrayList<>();
Class resCls = resources.getClass();
Field[] declaredFields = resCls.getDeclaredFields();
while (ret == null && declaredFields != null && declaredFields.length > 0) {
while (declaredFields != null && declaredFields.length > 0) {
for (Field field : declaredFields) {
if (field.getType().isAssignableFrom(DisplayMetrics.class)) {
field.setAccessible(true);
sMetricsField = field;
ret = getMetricsFromField(resources);
if (ret != null) break;
DisplayMetrics tmpDm = getMetricsFromField(resources, field);
if (tmpDm != null) {
sMetricsFields.add(field);
tmpDm.xdpi = newXdpi;
}
}
}
resCls = resCls.getSuperclass();
......@@ -96,16 +92,25 @@ public final class AdaptScreenUtils {
break;
}
}
sIsInit = true;
return ret;
} else {
applyMetricsFields(resources, newXdpi);
}
}
private static void applyMetricsFields(final Resources resources, final float newXdpi) {
for (Field metricsField : sMetricsFields) {
try {
DisplayMetrics dm = (DisplayMetrics) metricsField.get(resources);
if (dm != null) dm.xdpi = newXdpi;
} catch (Exception e) {
Log.e("AdaptScreenUtils", "applyMetricsFields: " + e);
}
}
if (sMetricsField == null) return null;
return getMetricsFromField(resources);
}
private static DisplayMetrics getMetricsFromField(final Resources resources) {
private static DisplayMetrics getMetricsFromField(final Resources resources, final Field field) {
try {
return (DisplayMetrics) sMetricsField.get(resources);
return (DisplayMetrics) field.get(resources);
} catch (Exception e) {
Log.e("AdaptScreenUtils", "getMetricsFromField: " + e);
return null;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册