Skip to content

Commit

Permalink
Add screenWidthDp and screenHeightDp support.
Browse files Browse the repository at this point in the history
  • Loading branch information
xxjy committed Sep 4, 2019
1 parent 4bc1c23 commit 2d071d3
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 4 deletions.
47 changes: 44 additions & 3 deletions autosize/src/main/java/me/jessyan/autosize/AutoSize.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.app.Application;
import android.app.Dialog;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.net.Uri;
import android.util.DisplayMetrics;
Expand Down Expand Up @@ -163,6 +164,8 @@ public static void autoConvertDensity(Activity activity, float sizeInDp, boolean
int targetDensityDpi = 0;
float targetScaledDensity = 0;
float targetXdpi = 0;
int targetScreenWidthDp;
int targetScreenHeightDp;

if (displayMetricsInfo == null) {
if (isBaseOnWidth) {
Expand All @@ -175,26 +178,32 @@ public static void autoConvertDensity(Activity activity, float sizeInDp, boolean
targetScaledDensity = targetDensity * scale;
targetDensityDpi = (int) (targetDensity * 160);

targetScreenWidthDp = (int) (AutoSizeConfig.getInstance().getScreenWidth() / targetDensity);
targetScreenHeightDp = (int) (AutoSizeConfig.getInstance().getScreenHeight() / targetDensity);

if (isBaseOnWidth) {
targetXdpi = AutoSizeConfig.getInstance().getScreenWidth() * 1.0f / subunitsDesignSize;
} else {
targetXdpi = AutoSizeConfig.getInstance().getScreenHeight() * 1.0f / subunitsDesignSize;
}

mCache.put(key, new DisplayMetricsInfo(targetDensity, targetDensityDpi, targetScaledDensity, targetXdpi));
mCache.put(key, new DisplayMetricsInfo(targetDensity, targetDensityDpi, targetScaledDensity, targetXdpi, targetScreenWidthDp, targetScreenHeightDp));
} else {
targetDensity = displayMetricsInfo.getDensity();
targetDensityDpi = displayMetricsInfo.getDensityDpi();
targetScaledDensity = displayMetricsInfo.getScaledDensity();
targetXdpi = displayMetricsInfo.getXdpi();
targetScreenWidthDp = displayMetricsInfo.getScreenWidthDp();
targetScreenHeightDp = displayMetricsInfo.getScreenHeightDp();
}

setDensity(activity, targetDensity, targetDensityDpi, targetScaledDensity, targetXdpi);
setScreenSizeDp(activity, targetScreenWidthDp, targetScreenHeightDp);

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"
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"
, activity.getClass().getName(), activity.getClass().getSimpleName(), isBaseOnWidth, isBaseOnWidth ? "designWidthInDp"
: "designHeightInDp", sizeInDp, isBaseOnWidth ? "designWidthInSubunits" : "designHeightInSubunits", subunitsDesignSize
, targetDensity, targetScaledDensity, targetDensityDpi, targetXdpi));
, targetDensity, targetScaledDensity, targetDensityDpi, targetXdpi, targetScreenWidthDp, targetScreenHeightDp));
}

/**
Expand All @@ -217,6 +226,9 @@ public static void cancelAdapt(Activity activity) {
, AutoSizeConfig.getInstance().getInitDensityDpi()
, AutoSizeConfig.getInstance().getInitScaledDensity()
, initXdpi);
setScreenSizeDp(activity
, AutoSizeConfig.getInstance().getInitScreenWidthDp()
, AutoSizeConfig.getInstance().getInitScreenHeightDp());
}

/**
Expand Down Expand Up @@ -291,6 +303,35 @@ private static void setDensity(DisplayMetrics displayMetrics, float density, int
}
}

/**
* 给 {@link Configuration} 赋值
*
* @param activity {@link Activity}
* @param screenWidthDp {@link Configuration#screenWidthDp}
* @param screenHeightDp {@link Configuration#screenHeightDp}
*/
private static void setScreenSizeDp(Activity activity, int screenWidthDp, int screenHeightDp) {
if (AutoSizeConfig.getInstance().getUnitsManager().isSupportDP() && AutoSizeConfig.getInstance().getUnitsManager().isSupportScreenSizeDP()) {
Configuration activityConfiguration = activity.getResources().getConfiguration();
setScreenSizeDp(activityConfiguration, screenWidthDp, screenHeightDp);

Configuration appConfiguration = AutoSizeConfig.getInstance().getApplication().getResources().getConfiguration();
setScreenSizeDp(appConfiguration, screenWidthDp, screenHeightDp);
}
}

/**
* Configuration赋值
*
* @param configuration {@link Configuration}
* @param screenWidthDp {@link Configuration#screenWidthDp}
* @param screenHeightDp {@link Configuration#screenHeightDp}
*/
private static void setScreenSizeDp(Configuration configuration, int screenWidthDp, int screenHeightDp) {
configuration.screenWidthDp = screenWidthDp;
configuration.screenHeightDp = screenHeightDp;
}

/**
* 解决 MIUI 更改框架导致的 MIUI7 + Android5.1.1 上出现的失效问题 (以及极少数基于这部分 MIUI 去掉 ART 然后置入 XPosed 的手机)
* 来源于: https://github.com/Firedamp/Rudeness/blob/master/rudeness-sdk/src/main/java/com/bulong/rudeness/RudenessScreenHelper.java#L61:5
Expand Down
43 changes: 42 additions & 1 deletion autosize/src/main/java/me/jessyan/autosize/AutoSizeCompat.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package me.jessyan.autosize;

import android.app.Activity;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;

Expand Down Expand Up @@ -151,6 +152,8 @@ public static void autoConvertDensity(Resources resources, float sizeInDp, boole
int targetDensityDpi = 0;
float targetScaledDensity = 0;
float targetXdpi = 0;
int targetScreenWidthDp;
int targetScreenHeightDp;

if (displayMetricsInfo == null) {
if (isBaseOnWidth) {
Expand All @@ -163,21 +166,27 @@ public static void autoConvertDensity(Resources resources, float sizeInDp, boole
targetScaledDensity = targetDensity * scale;
targetDensityDpi = (int) (targetDensity * 160);

targetScreenWidthDp = (int) (AutoSizeConfig.getInstance().getScreenWidth() / targetDensity);
targetScreenHeightDp = (int) (AutoSizeConfig.getInstance().getScreenHeight() / targetDensity);

if (isBaseOnWidth) {
targetXdpi = AutoSizeConfig.getInstance().getScreenWidth() * 1.0f / subunitsDesignSize;
} else {
targetXdpi = AutoSizeConfig.getInstance().getScreenHeight() * 1.0f / subunitsDesignSize;
}

mCache.put(key, new DisplayMetricsInfo(targetDensity, targetDensityDpi, targetScaledDensity, targetXdpi));
mCache.put(key, new DisplayMetricsInfo(targetDensity, targetDensityDpi, targetScaledDensity, targetXdpi, targetScreenWidthDp, targetScreenHeightDp));
} else {
targetDensity = displayMetricsInfo.getDensity();
targetDensityDpi = displayMetricsInfo.getDensityDpi();
targetScaledDensity = displayMetricsInfo.getScaledDensity();
targetXdpi = displayMetricsInfo.getXdpi();
targetScreenWidthDp = displayMetricsInfo.getScreenWidthDp();
targetScreenHeightDp = displayMetricsInfo.getScreenHeightDp();
}

setDensity(resources, targetDensity, targetDensityDpi, targetScaledDensity, targetXdpi);
setScreenSizeDp(resources, targetScreenWidthDp, targetScreenHeightDp);
}

/**
Expand All @@ -200,6 +209,9 @@ public static void cancelAdapt(Resources resources) {
, AutoSizeConfig.getInstance().getInitDensityDpi()
, AutoSizeConfig.getInstance().getInitScaledDensity()
, initXdpi);
setScreenSizeDp(resources
, AutoSizeConfig.getInstance().getInitScreenWidthDp()
, AutoSizeConfig.getInstance().getInitScreenHeightDp());
}

/**
Expand Down Expand Up @@ -264,6 +276,35 @@ private static void setDensity(DisplayMetrics displayMetrics, float density, int
}
}

/**
* 给 {@link Configuration} 赋值
*
* @param resources {@link Resources}
* @param screenWidthDp {@link Configuration#screenWidthDp}
* @param screenHeightDp {@link Configuration#screenHeightDp}
*/
private static void setScreenSizeDp(Resources resources, int screenWidthDp, int screenHeightDp) {
if (AutoSizeConfig.getInstance().getUnitsManager().isSupportDP() && AutoSizeConfig.getInstance().getUnitsManager().isSupportScreenSizeDP()) {
Configuration activityConfiguration = resources.getConfiguration();
setScreenSizeDp(activityConfiguration, screenWidthDp, screenHeightDp);

Configuration appConfiguration = AutoSizeConfig.getInstance().getApplication().getResources().getConfiguration();
setScreenSizeDp(appConfiguration, screenWidthDp, screenHeightDp);
}
}

/**
* Configuration赋值
*
* @param configuration {@link Configuration}
* @param screenWidthDp {@link Configuration#screenWidthDp}
* @param screenHeightDp {@link Configuration#screenHeightDp}
*/
private static void setScreenSizeDp(Configuration configuration, int screenWidthDp, int screenHeightDp) {
configuration.screenWidthDp = screenWidthDp;
configuration.screenHeightDp = screenHeightDp;
}

/**
* 解决 MIUI 更改框架导致的 MIUI7 + Android5.1.1 上出现的失效问题 (以及极少数基于这部分 MIUI 去掉 ART 然后置入 XPosed 的手机)
* 来源于: https://github.com/Firedamp/Rudeness/blob/master/rudeness-sdk/src/main/java/com/bulong/rudeness/RudenessScreenHelper.java#L61:5
Expand Down
29 changes: 29 additions & 0 deletions autosize/src/main/java/me/jessyan/autosize/AutoSizeConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ public final class AutoSizeConfig {
* 最初的 {@link DisplayMetrics#xdpi}
*/
private float mInitXdpi;
/**
* 最初的 {@link Configuration#screenWidthDp}
*/
private int mInitScreenWidthDp;
/**
* 最初的 {@link Configuration#screenHeightDp}
*/
private int mInitScreenHeightDp;
/**
* 设计图上的总宽度, 单位 dp
*/
Expand Down Expand Up @@ -199,6 +207,7 @@ AutoSizeConfig init(final Application application, boolean isBaseOnWidth, AutoAd
this.mApplication = application;
this.isBaseOnWidth = isBaseOnWidth;
final DisplayMetrics displayMetrics = Resources.getSystem().getDisplayMetrics();
final Configuration configuration = Resources.getSystem().getConfiguration();

getMetaData(application);
isVertical = application.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
Expand All @@ -212,6 +221,8 @@ AutoSizeConfig init(final Application application, boolean isBaseOnWidth, AutoAd
mInitDensityDpi = displayMetrics.densityDpi;
mInitScaledDensity = displayMetrics.scaledDensity;
mInitXdpi = displayMetrics.xdpi;
mInitScreenWidthDp = configuration.screenWidthDp;
mInitScreenHeightDp = configuration.screenHeightDp;
application.registerComponentCallbacks(new ComponentCallbacks() {
@Override
public void onConfigurationChanged(Configuration newConfig) {
Expand Down Expand Up @@ -479,6 +490,24 @@ public float getInitXdpi() {
return mInitXdpi;
}

/**
* 获取 {@link #mInitScreenWidthDp}
*
* @return {@link #mInitScreenWidthDp}
*/
public int getInitScreenWidthDp() {
return mInitScreenWidthDp;
}

/**
* 获取 {@link #mInitScreenHeightDp}
*
* @return {@link #mInitScreenHeightDp}
*/
public int getInitScreenHeightDp() {
return mInitScreenHeightDp;
}

/**
* 获取屏幕方向
*
Expand Down
33 changes: 33 additions & 0 deletions autosize/src/main/java/me/jessyan/autosize/DisplayMetricsInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class DisplayMetricsInfo implements Parcelable {
private int densityDpi;
private float scaledDensity;
private float xdpi;
private int screenWidthDp;
private int screenHeightDp;

public DisplayMetricsInfo(float density, int densityDpi, float scaledDensity, float xdpi) {
this.density = density;
Expand All @@ -41,6 +43,15 @@ public DisplayMetricsInfo(float density, int densityDpi, float scaledDensity, fl
this.xdpi = xdpi;
}

public DisplayMetricsInfo(float density, int densityDpi, float scaledDensity, float xdpi, int screenWidthDp, int screenHeightDp) {
this.density = density;
this.densityDpi = densityDpi;
this.scaledDensity = scaledDensity;
this.xdpi = xdpi;
this.screenWidthDp = screenWidthDp;
this.screenHeightDp = screenHeightDp;
}

public float getDensity() {
return density;
}
Expand Down Expand Up @@ -73,6 +84,22 @@ public void setXdpi(float xdpi) {
this.xdpi = xdpi;
}

public int getScreenWidthDp() {
return screenWidthDp;
}

public void setScreenWidthDp(int screenWidthDp) {
this.screenWidthDp = screenWidthDp;
}

public int getScreenHeightDp() {
return screenHeightDp;
}

public void setScreenHeightDp(int screenHeightDp) {
this.screenHeightDp = screenHeightDp;
}

@Override
public int describeContents() {
return 0;
Expand All @@ -84,13 +111,17 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.densityDpi);
dest.writeFloat(this.scaledDensity);
dest.writeFloat(this.xdpi);
dest.writeInt(this.screenWidthDp);
dest.writeInt(this.screenHeightDp);
}

protected DisplayMetricsInfo(Parcel in) {
this.density = in.readFloat();
this.densityDpi = in.readInt();
this.scaledDensity = in.readFloat();
this.xdpi = in.readFloat();
this.screenWidthDp = in.readInt();
this.screenHeightDp = in.readInt();
}

public static final Creator<DisplayMetricsInfo> CREATOR = new Creator<DisplayMetricsInfo>() {
Expand All @@ -112,6 +143,8 @@ public String toString() {
", densityDpi=" + densityDpi +
", scaledDensity=" + scaledDensity +
", xdpi=" + xdpi +
", screenWidthDp=" + screenWidthDp +
", screenHeightDp=" + screenHeightDp +
'}';
}
}
23 changes: 23 additions & 0 deletions autosize/src/main/java/me/jessyan/autosize/unit/UnitsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public class UnitsManager {
* 是否支持副单位, 以什么为副单位? 默认不支持
*/
private Subunits mSupportSubunits = Subunits.NONE;
/**
* 是否支持 ScreenSizeDp 修改, 默认不支持
*/
private boolean isSupportScreenSizeDP = false;

/**
* 设置设计图尺寸
Expand Down Expand Up @@ -175,6 +179,25 @@ public Subunits getSupportSubunits() {
return mSupportSubunits;
}

/**
* 是否支持 ScreenSizeDp 修改, 默认不支持, 详情请看类文件的注释 {@link UnitsManager}
*
* @return {@code true} 为支持, {@code false} 为不支持
*/
public boolean isSupportScreenSizeDP() {
return isSupportScreenSizeDP;
}

/**
* 是否让 AndroidAutoSize 支持 ScreenSizeDp 修改, 默认不支持, 详情请看类文件的注释 {@link UnitsManager}
*
* @param supportScreenSizeDP {@code true} 为支持, {@code false} 为不支持
*/
public UnitsManager setSupportScreenSizeDP(boolean supportScreenSizeDP) {
isSupportScreenSizeDP = supportScreenSizeDP;
return this;
}

/**
* 让 AndroidAutoSize 以什么单位为副单位, 在 pt、in、mm 这三个冷门单位中选择一个即可, 三个效果都是一样的
* 按自己的喜好选择, 比如我就喜欢 mm, 翻译为中文是妹妹的意思
Expand Down

0 comments on commit 2d071d3

Please sign in to comment.