1919import android .app .Application ;
2020import android .app .Dialog ;
2121import android .content .Context ;
22+ import android .content .res .Configuration ;
2223import android .content .res .Resources ;
2324import android .net .Uri ;
2425import android .util .DisplayMetrics ;
@@ -163,6 +164,8 @@ public static void autoConvertDensity(Activity activity, float sizeInDp, boolean
163164 int targetDensityDpi = 0 ;
164165 float targetScaledDensity = 0 ;
165166 float targetXdpi = 0 ;
167+ int targetScreenWidthDp ;
168+ int targetScreenHeightDp ;
166169
167170 if (displayMetricsInfo == null ) {
168171 if (isBaseOnWidth ) {
@@ -175,26 +178,32 @@ public static void autoConvertDensity(Activity activity, float sizeInDp, boolean
175178 targetScaledDensity = targetDensity * scale ;
176179 targetDensityDpi = (int ) (targetDensity * 160 );
177180
181+ targetScreenWidthDp = (int ) (AutoSizeConfig .getInstance ().getScreenWidth () / targetDensity );
182+ targetScreenHeightDp = (int ) (AutoSizeConfig .getInstance ().getScreenHeight () / targetDensity );
183+
178184 if (isBaseOnWidth ) {
179185 targetXdpi = AutoSizeConfig .getInstance ().getScreenWidth () * 1.0f / subunitsDesignSize ;
180186 } else {
181187 targetXdpi = AutoSizeConfig .getInstance ().getScreenHeight () * 1.0f / subunitsDesignSize ;
182188 }
183189
184- mCache .put (key , new DisplayMetricsInfo (targetDensity , targetDensityDpi , targetScaledDensity , targetXdpi ));
190+ mCache .put (key , new DisplayMetricsInfo (targetDensity , targetDensityDpi , targetScaledDensity , targetXdpi , targetScreenWidthDp , targetScreenHeightDp ));
185191 } else {
186192 targetDensity = displayMetricsInfo .getDensity ();
187193 targetDensityDpi = displayMetricsInfo .getDensityDpi ();
188194 targetScaledDensity = displayMetricsInfo .getScaledDensity ();
189195 targetXdpi = displayMetricsInfo .getXdpi ();
196+ targetScreenWidthDp = displayMetricsInfo .getScreenWidthDp ();
197+ targetScreenHeightDp = displayMetricsInfo .getScreenHeightDp ();
190198 }
191199
192200 setDensity (activity , targetDensity , targetDensityDpi , targetScaledDensity , targetXdpi );
201+ setScreenSizeDp (activity , targetScreenWidthDp , targetScreenHeightDp );
193202
194- LogUtils .d (String .format (Locale .ENGLISH , "The %s has been adapted! \n %s Info: isBaseOnWidth = %s, %s = %f, %s = %f, targetDensity = %f, targetScaledDensity = %f, targetDensityDpi = %d, targetXdpi = %f"
203+ LogUtils .d (String .format (Locale .ENGLISH , "The %s has been adapted! \n %s Info: isBaseOnWidth = %s, %s = %f, %s = %f, targetDensity = %f, targetScaledDensity = %f, targetDensityDpi = %d, targetXdpi = %f, targetScreenWidthDp = %d, targetScreenHeightDp = %d "
195204 , activity .getClass ().getName (), activity .getClass ().getSimpleName (), isBaseOnWidth , isBaseOnWidth ? "designWidthInDp"
196205 : "designHeightInDp" , sizeInDp , isBaseOnWidth ? "designWidthInSubunits" : "designHeightInSubunits" , subunitsDesignSize
197- , targetDensity , targetScaledDensity , targetDensityDpi , targetXdpi ));
206+ , targetDensity , targetScaledDensity , targetDensityDpi , targetXdpi , targetScreenWidthDp , targetScreenHeightDp ));
198207 }
199208
200209 /**
@@ -217,6 +226,9 @@ public static void cancelAdapt(Activity activity) {
217226 , AutoSizeConfig .getInstance ().getInitDensityDpi ()
218227 , AutoSizeConfig .getInstance ().getInitScaledDensity ()
219228 , initXdpi );
229+ setScreenSizeDp (activity
230+ , AutoSizeConfig .getInstance ().getInitScreenWidthDp ()
231+ , AutoSizeConfig .getInstance ().getInitScreenHeightDp ());
220232 }
221233
222234 /**
@@ -291,6 +303,35 @@ private static void setDensity(DisplayMetrics displayMetrics, float density, int
291303 }
292304 }
293305
306+ /**
307+ * 给 {@link Configuration} 赋值
308+ *
309+ * @param activity {@link Activity}
310+ * @param screenWidthDp {@link Configuration#screenWidthDp}
311+ * @param screenHeightDp {@link Configuration#screenHeightDp}
312+ */
313+ private static void setScreenSizeDp (Activity activity , int screenWidthDp , int screenHeightDp ) {
314+ if (AutoSizeConfig .getInstance ().getUnitsManager ().isSupportDP () && AutoSizeConfig .getInstance ().getUnitsManager ().isSupportScreenSizeDP ()) {
315+ Configuration activityConfiguration = activity .getResources ().getConfiguration ();
316+ setScreenSizeDp (activityConfiguration , screenWidthDp , screenHeightDp );
317+
318+ Configuration appConfiguration = AutoSizeConfig .getInstance ().getApplication ().getResources ().getConfiguration ();
319+ setScreenSizeDp (appConfiguration , screenWidthDp , screenHeightDp );
320+ }
321+ }
322+
323+ /**
324+ * Configuration赋值
325+ *
326+ * @param configuration {@link Configuration}
327+ * @param screenWidthDp {@link Configuration#screenWidthDp}
328+ * @param screenHeightDp {@link Configuration#screenHeightDp}
329+ */
330+ private static void setScreenSizeDp (Configuration configuration , int screenWidthDp , int screenHeightDp ) {
331+ configuration .screenWidthDp = screenWidthDp ;
332+ configuration .screenHeightDp = screenHeightDp ;
333+ }
334+
294335 /**
295336 * 解决 MIUI 更改框架导致的 MIUI7 + Android5.1.1 上出现的失效问题 (以及极少数基于这部分 MIUI 去掉 ART 然后置入 XPosed 的手机)
296337 * 来源于: https://github.com/Firedamp/Rudeness/blob/master/rudeness-sdk/src/main/java/com/bulong/rudeness/RudenessScreenHelper.java#L61:5
0 commit comments