There are a lot of Android devices, which makes it difficult to adapt to different models, and screen adaptation is also a tricky thing. One way is to create a one-to-one values folder for different screen resolutions under the RES folder, as shown below:

In fact, this kind of adaptation can also meet the requirements of adaptation, the use of the feeling of the rules. Today I’d like to introduce another adaptation, which is github.com/JessYanCodi…

Use the pose

  • Add the dependent

Implementation 'me. Jessyan: autosize: 1.1.2'

  • Add the following configuration to the manifest file
<manifest>
    <application>            
        <meta-data
            android:name="design_width_in_dp"
            android:value="360"/>
        <meta-data
            android:name="design_height_in_dp"
            android:value="640"/>           
     </application>           
</manifest>
Copy the code
  • Interface adapter
public class CustomAdaptActivity extends AppCompatActivity implements CustomAdapt {

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

    @Override
    public float getSizeInDp(a) {
        return 667; }}Copy the code

If a particular activity does not need to be adapted, the CancelAdapt interface is implemented

public class CancelAdaptActivity extends AppCompatActivity implements CancelAdapt {}Copy the code

Fragment adaptation is the same as activity adaptation. The CancelAdapt interface tells the framework that adaptation is not required.

  • Problems encountered during use
  1. When accessing third-party libraries, third-party activities and fragments must also be adapted. Otherwise, the following error will be reported

"Java. Lang. RuntimeException: Unable to start activity ComponentInfo{com.wdg.tradecenter/com.yanzhenjie.permission.PermissionActivity}:java.lang.IllegalArgumentException: You must set design_width_in_dp in your AndroidManifest file"

You can add the following code to the Application#onCreate method to unmatch the specified activities and fragments

AutoSizeConfig.getInstance()
                .getExternalAdaptManager()
                .addCancelAdaptOfActivity(PermissionActivity.class);
Copy the code
  • application

The above is a UI design diagram on Lanhu. Click the control, and the corresponding XML code appears in the lower right corner. Using autosize for adaptation, you can directly use the layout code generated on Lanhu, which is really convenient.

Principle of the framework

  • unit
unit explain
px pixel
dp Independent device pixel calculation formula: Px = DP (DPI /160)
dpi Pixel density, which refers to the number of pixels per unit size specified in the system software, is often a fixed value written in the system’s factory configuration file

Through the introduction of the above units and the conversion between them, we can draw the following conclusions:

Px = dp (dpi/160), dpi/160 => px= dp*density => dp = px/density; Understanding the above conclusion, let’s discuss why we can’t fit each mobile phone screen when we set the width/height of the control to a certain DP.

Device A, screen width 720px, DPI 160, total dp 720/(160/160) = 720, Device B, screen width 720px, DPI 320, Then the total screen DP is 720/(320/160) = 360 dp. It can be seen that the total screen DP width varies on different devices, but the DP value we filled in the layout is fixed, which results in the fixed width displayed in different proportions on different devices. For example, we have A View in the layout with A fixed width of 180DP. On device A, it will take up 1/4 of the screen width, but on device B, it will only take up 1/2 of the screen width. The difference is huge.

In order to fit perfectly, we have to make sure that the View is the same proportion to the screen on any screen resolution.

The solution

By the formula: Dp = px/density; dp = px/density; dp = px/density; The proportion of DP to the total width is the same, so that the proportion of the screen is the same.

So how do we determine density?

Dp = px/density => total screen px width /density = total screen DP width

Total px width of the screen/density = Total DP width of the screen => Total screen width of the device (in pixels)/total design width (in dp) = density

If we take the total width (DP) of a set of design drawings as the median width (DP) of the final phone screen, the density can be modified while ensuring that the total screen width of the final phone with different resolutions is the same, then adaptation is completed.

AndroidAutoSize/ Toutiao is based on this principle to achieve screen adaptation.

How does AndroidAutoSize apply the above principles with minimal intrusion cost

Initialize the library with a ContentProvider — the onCreate of the ContentProvider is called between Application attachBaseContext and onCreate.

At the time of initialization register an Application. The ActivityLifecycleCallbacks callback interface class

application.registerActivityLifecycleCallbacks(mActivityLifecycleCallbacks); At the interface Application. ActivityLifecycleCallbacks implementation class density changes and adaptation in order to achieve the purpose.

Conclusion This adaptation scheme has obvious advantages

Low cost and simple to use. Code of invasive low There is no performance loss Adaptive activity/fragments dialog/toast shortcomings

Since the AndroidAutoSize library can modify density, and other libraries can, this can cause conflicts.