Screen adaptation is a problem we often encounter in development
Different models have different screen sizes and densities
There are already many screen adaptation solutions on the market
Today I will introduce one that I think is good
AutoSize
This is an Android tool that can automatically adapt the width and height according to the different screen sizes of the device
The characteristic is that it is very simple to use.
The author’s introduction to its website includes:
https://www.jianshu.com/p/4aa23d69d481
Code location:
https://github.com/JessYanCoding/AndroidAutoSize
Now let’s introduce the basic usage
The first is dependence
Private dependency
implementation 'me.jessyan:autosize:1.1.2'
Shared dependency
api 'me.jessyan:autosize:1.1.2'
The second step is to configure your screen width and height in your configuration list
Note the use of dp to configure
For example, the following is the width and height value of 1280*900
<manifest>
<application>
<meta-data
android:name="design_width_in_dp"
android:value="1280"/>
<meta-data
android:name="design_height_in_dp"
android:value="900"/>
</application>
</manifest>
To get the width and height of the screen, you can get px first, and then convert according to the density
I have written this before:
(Original) Share several tools written by myself (11) Operation tools for configuration files
At this point, the automatic adaptation has been completed
Actually there are some advanced usage
This scheme is adapted according to the width by default
Can also be converted to height
Configure in the onCreate method of Application
AutoSizeConfig.getInstance().setBaseOnWidth(false);
Of course, it can also be customized for a single Activity or Fragment
First of all, we must implement the CustomAdapt class
If you don’t want to adopt this scheme
To implement the CancelAdap class
Then there are several implementation methods
boolean isBaseOnWidth(): Whether to adapt according to the width, false means according to the height
float getSizeInDp(): According to the previous method, decide whether to fit according to width or height, and then return the width or height you want to reset.
Return 0 to continue to use the width and height configured in the configuration list
Note: If it is Fragment, it must be customized
Need to be configured like this in Application's onCreate method
AutoSizeConfig.getInstance().setCustomFragment(true);
There is a more free way to customize the adapter
Configure in the onCreate method of Application:
AutoSizeConfig.getInstance().setAutoAdaptStrategy(new AutoAdaptStrategy());
Implementation principle:
Before setContentView, modify the content of the DisplayMetrics class.
The core code is as follows:
AutoSize.autoConvertDensity(activity, sizeInDp, isBaseOnWidth)
AutoSize.setDensity(activity, density, densityDpi, scaledDensity, xdpi)
AutoSize.senDensity(displayMetrics, density, densityDpi, scaledDensity, xdpi)
If you encounter problems such as adding autosize to some interfaces, canceling autosize in some interfaces, switching between horizontal and vertical screens across applications, etc., you can refer to this solution:
First print out the screen density. If the screen gets bigger and smaller, the density will change
Log.d("print", "result: " + getResources().getDisplayMetrics().density);
Then it is processing:
1. Call setScreenWidthHeight and autoConvertDensity in the appropriate life cycle or method
2. Don't use getResources().getConfiguration().orientation to judge the horizontal and vertical screens, instead use the aspect ratio of DisplayMetrics
See the following example specifically
public class BaseAutoSizeActivity extends Activity implements CustomAdapt {
@Override
public boolean isBaseOnWidth() {
return true;
}
@Override
public float getSizeInDp() {
int sizeInDp;
if (AutoSizeConfig.getInstance().getScreenWidth() > AutoSizeConfig.getInstance().getScreenHeight()) {
sizeInDp = AutoSizeConfig.getInstance().getDesignWidthInDp();
} else {
sizeInDp = AutoSizeConfig.getInstance().getDesignHeightInDp();
}
return sizeInDp;
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setScreenWidthHeight();
AutoSize.autoConvertDensityOfCustomAdapt(this, this);
}
@Override
protected void onResume() {
super.onResume();
setScreenWidthHeight();
AutoSize.autoConvertDensityOfCustomAdapt(this, this);
}
@Override
protected void onRestart() {
super.onRestart();
setScreenWidthHeight();
AutoSize.autoConvertDensityOfCustomAdapt(this, this);
}
@Override
public Resources getResources() {
setScreenWidthHeight();
try {
AutoSizeCompat.autoConvertDensityOfCustomAdapt(super.getResources(), this);
} catch (Exception e) {
e.printStackTrace();
}
return super.getResources();
}
public void setScreenWidthHeight() {
int[] screenSize = ScreenUtils.getScreenSize(getApplicationContext());
int width = screenSize[0];
int height = screenSize[1];
AutoSizeConfig.getInstance().setScreenWidth(width);
AutoSizeConfig.getInstance().setScreenHeight(height);
}
}