Use AndroidAutoSize for screen adaptation

The UI of many of the company's applications is adapted using the Android AutoSize framework, and the system source apk is compiled based on the mk file. Since a lot of application transplantation and adaptation work is required without re-providing the UI design diagram, the AndroidAutoSize library can well adapt to the UI display of different models.
  1. introduce

It is very simple to use, you only need to fill in the size of the design drawing to access the project. Use dp, sp as the default unit for layout, which is very low intrusive.
In principle, because you only need to modify the density once, all places in the project will be automatically adapted. This one-size-fits-all approach is sometimes an advantage, but it is also its biggest disadvantage. This problem becomes more serious when the design drawing size of a system control or a third-party library control is very different from the design drawing size of our project itself.
You can decide whether to access the adaptation solution according to the project situation.

  1. project integration

First upload autosize-1.2.1.aar under project libs

1) gradle method

implementation fileTree(dir: 'libs', include: ['*.jar','*.aar'])

2) mk mode

include $(CLEAR_VARS)
LOCAL_AAPT_FLAGS := --auto-add-overlay
LOCAL_AAPT_FLAGS += --extra-packages me.jessyan.autosize
LOCAL_STATIC_JAVA_AAR_LIBRARIES := OtaUpdate_autosize-1.2.1
#解决Error: Compilation can't be completed because `android.support.v4.app.FragmentManager$FragmentLifecycleCallbacks` 
LOCAL_STATIC_ANDROID_LIBRARIES := \
	android-support-v4 \
	androidx.appcompat_appcompat
include $(BUILD_PACKAGE)

include $(CLEAR_VARS) 
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := OtaUpdate_autosize-1.2.1:libs/autosize-1.2.1.aar
include $(BUILD_MULTI_PREBUILT)
  1. basic use

1) Manifest file

<application
	<!--解决Androidx和Android support库不能共存的编译错误-->
	android:appComponentFactory="androidx.core.app.CoreComponentFactory"
	tools:replace="android:appComponentFactory">

	<!--重点:填写以某设计图为基准的design UI dp值-->
	<meta-data
		android:name="design_width_in_dp"
		android:value="1333" />
    <meta-data
		android:name="design_height_in_dp"
		android:value="800" />
<application

2)CustomAdapt和CancelAdapt

  • If the page needs to be adapted, it is recommended to inherit from this base class. Adaptation parameters can be extended by implementing the CustomAdapt interface.
public abstract class BaseAutoSizeActivity extends Activity implements CustomAdapt {

    private float density;

    @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();
        setDensityByAutoSize();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setScreenWidthHeight();
        setDensityByAutoSize();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        setScreenWidthHeight();
        setDensityByAutoSize();
    }

    @Override
    public Resources getResources() {
        setDensityByAutoSize();
        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);
    }

    private void setDensityByAutoSize() {
        float tempDensity = super.getResources().getDisplayMetrics().density;
        if (density != tempDensity) {
            setScreenWidthHeight();
            try {
                AutoSizeCompat.autoConvertDensityOfCustomAdapt(super.getResources(), this);
            } catch (Exception e) {
                e.printStackTrace();
            }
            density = super.getResources().getDisplayMetrics().density;
        }
    }
}
  • When an Activity wants to give up adaptation, it can implement the CancelAdapt interface.
  1. common problem

1) System configuration changes such as rotating the screen lead to invalidation: rewrite the getResource method to reset the density, as shown in the base class above.

2) Multi-process adaptation failure: the default process interface is normally adapted, and other process interfaces (specified to run in another process through android:process) display abnormalities. Call in the Application's onCreate method: AutoSize.initCompatMultiProcess(this).

3) For more questions, please refer to: https://github.com/JessYanCoding/AndroidAutoSize/issues

Guess you like

Origin blog.csdn.net/qq_23069607/article/details/124502565