AndroidAutoSize Ultimate Edition of Today’s Toutiao Screen Adaptation Solution

AndroidAutoSize

The screen adaptation framework AndroidAutoSize is optimized according to the Toutiao screen adaptation scheme.

Configure third-party remote dependencies

 

<!--私有依赖-->
implementation 'me.jessyan:autosize:1.1.2'
<!--共有依赖-->
api 'me.jessyan:autosize:1.1.2'

Use and function introduction

AndroidAutoSize is very simple to use, you only need to fill in the design drawing size to access the project;

 

<!--适配海博TV-->
<manifest>
    <application>            
        <meta-data
            android:name="design_width_in_dp"
            android:value="540"/>
        <meta-data
            android:name="design_height_in_dp"
            android:value="960"/>           
     </application>           
</manifest>

There are two types of layout units to choose from, one is the main unit (dp, sp) and the other is the sub unit (pt, in, mm)

  • Main unit : Use dp and sp as the unit for layout, which is the least intrusive and will affect the layout effects of other third-party library pages, third-party library controls, and system controls. However, AndroidAutoSize also uses this feature to use ExternalAdaptManager to achieve without modifying the third-party library source code Adapt to the functions of the tripartite library in the case of

  • Sub-unit : Use pt, in, mm as the unit for layout, which is highly intrusive and supports old projects well. It will not affect the layout effects of other tripartite library pages, tripartite library controls, and system controls. Density can be completely shielded and modified All the unknown and known issues caused by this, but in this way AndroidAutoSize cannot adapt to the third party library

When using the main unit, design_width_in_dpand design_height_in_dpthe unit must be dp, the formula dp = px / (DPI / 160)will be converted to dp px dimension size, if it can not find the device directly to the DPI would px size divided by 2 or 3.

Frame access completed

This is the basic function of AndroidAutoSize. The use of AndroidAutoSize is over here. Only the above step is needed to help you access AndroidAutoSize in the simplest way.

Advanced use

The design size filled in AndroidManifest.xml is the global design drawing size of the entire project, but if for some reason, the design drawing size of this page is different from the design drawing size filled in AndroidManifest.xml. How to do it? You can make the Activity of this page implement CustomAdapt, and the first method of the CustomAdapt interface can modify the design size of the current page.

  • Custom Activity

 

public class CustomAdaptActivity extends AppCompatActivity implements CustomAdapt {

     /**
     * 是否按照宽度进行等比例适配 (为了保证在高宽比不同的屏幕上也能正常适配, 所以只能在宽度和高度之中选择一个作为基准进行适配)
     *
     * @return {@code true} 为按照宽度进行适配, {@code false} 为按照高度进行适配
     */
    @Override
    public boolean isBaseOnWidth() {
        return false;
    }

     /**
     * 设计图尺寸为 1080px * 1920px, 高换算成 dp 为 960 (1920px / 2 = 960dp)
     * <p>
     * 返回的设计尺寸, 单位 dp
     * {@link #getSizeInDp} 须配合 {@link #isBaseOnWidth()} 使用, 规则如下:
     * 如果 {@link #isBaseOnWidth()} 返回 {@code true}, {@link #getSizeInDp} 则应该返回设计图的总宽度
     * 如果 {@link #isBaseOnWidth()} 返回 {@code false}, {@link #getSizeInDp} 则应该返回设计图的总高度
     * 如果您不需要自定义设计图上的设计尺寸, 想继续使用在 AndroidManifest 中填写的设计图尺寸, {@link #getSizeInDp} 则返回 {@code 0}
     *
     * @return 单位 dp
     */
    @Override
    public float getSizeInDp() {
        return 667;
    }
}

If an activity wants to give up adaptation, let this activity implement the CancelAdapt interface. For example, if modifying density affects the layout of some Activity pages in the old project, then you can let this activity implement the CancelAdapt interface

 

public class CancelAdaptActivity extends AppCompatActivity implements CancelAdapt {

}
  • Custom Fragment

The customization method of Fragment is the same as that of Activity, except that you need to enable Fragment support when the App is initialized before use

 

AutoSizeConfig.getInstance().setCustomFragment(true);
  • Implement CustomAdapt

 

public class CustomAdaptFragment extends Fragment implements CustomAdapt {

    @Override
    public boolean isBaseOnWidth() {
        return false;
    }

    @Override
    public float getSizeInDp() {
        return 667;
    }
}
  • Implement CancelAdapt

 

public class CancelAdaptFragment extends Fragment implements CancelAdapt {

}

Universal solution

In any case, if there is a sudden adaptation failure or an abnormal adaptation of the originally adapted layout, you only need to rewrite the getResources() method of the Activity. If the Dialog, PopupWindow and other controls have an adaptation failure or an adaptation exception, Also call AutoSize#autoConvertDensity() before each show().

The solution comes from: Blankj

 

@Override
public Resources getResources() {
    //需要升级到 v1.1.2 及以上版本才能使用 AutoSizeCompat
    AutoSizeCompat.autoConvertDensityOfGlobal((super.getResources());//如果没有自定义需求用这个方法
    AutoSizeCompat.autoConvertDensity((super.getResources(), 667, false);//如果有自定义需求就用这个方法
    return super.getResources();
}

 

 

Guess you like

Origin blog.csdn.net/guodashen007/article/details/105205691