This article is transcoded by Jian Yue SimpRead, the original address is www.jianshu.com
Background
I previously wrote an article based on Toutiao’s adaptation solution. Android screen adaptation has never been so simple, but later I found that there are still a lot of pitfalls. These pitfalls are all recorded in GitHub. Summary and solutions of screen adaptation problems. Based on so many pitfalls , and finally I found a more perfect adaptation solution. I originally planned to write this article more than a month ago, but due to the busy schedule of the company, I have not been able to find time, so I delayed the update until now. Let me share it below Let’s share this method, first let’s talk about the advantages.
Advantages
1. Non-invasive
First of all, let’s learn about a unit of length in Android: pt
, which represents a point and is the physical size of the screen. Its size is 1 inch of 1 / 72
, which is 72pt
is equal to 1 inch (in fact, there are relatively rare in
and mm
length units in Android). The unit I used for this adaptation happens to be pt
, so it will not have any impact on the layout you used before. You can boldly add this adaptation when developing new functions in old projects. For new projects, you can use this adaptation without hesitation, and after turning it off, the effect of pt
is equivalent to dp
.
2. High flexibility
If you want to make a View
have the same proportion of its size in the adapted dimension under devices with different resolutions, then use pt
for it. Units are enough. If you don’t want this effect, but want a larger device to display more content, then you can write dp
, sp
or whatever. Combining these two points, you can achieve the effect you want in terms of interface layout.
3. Will not affect the size of system View and third-party View
This has actually been shown in the non-intrusiveness. Since Toutiao’s solution is to directly modify the dp
adaptation of DisplayMetrics#density
, this will cause the system to View
size is inconsistent with the original, such as Dialog
, Toast
, size. Similarly, the size of the three-party View
will also be inconsistent with the original effect. This is one of the reasons why I chose pt
adaptation.
4. Will not expire
This is the most bragging point, because regardless of Toutiao’s adaptation or AndroidAutoSize, there will be situations where DisplayMetrics#density
is restored, and you need to reset it yourself. The most obvious thing is that there is in the interface For WebView
, the value of DisplayMetrics#density
will be restored during its initialization, causing the adaptation to fail. Of course, there is a solution for this, but there will be many other situations where DisplayMetrics#density
will be restored. The value of code>DisplayMetrics#density causes the adaptation to fail. My solution is to solve this pain point and prevent the value in DisplayMetrics
from being restored and causing the adaptation to fail.
Effect
Having said so much, let’s put it down first with the upper and lower renderings. The images of each resolution are the effect of width 1080pt
adaptation, height 1920pt
adaptation and the effect of turning off adaptation. .
480 x 800 – mdpi(160dpi)
mdpi
720 x 1280 – xhdpi(320dpi)
xhdpi
1080 x 1920 – xxhdpi(480dpi)
xxhdpi
1440×2560 – 560dpi
560dpi
It can be seen in the rendering that the WebView
does not cause adaptation failure to the subsequent View
. This is a problem that previous adaptation cannot solve.
How to create a preview?
In AS Tools -> AVD Manager -> Create Virtual Device...
, we take adapting 1080 x 1920
px as an example. The specific operations are as follows:
1080pt
After creating the device, we select the device in the preview interface to see the pt
unit effect.
Depending on the size of the design drawing given to you by the designer, you can build the equipment of that size. For example, if it is 720 x 1280
px, then you can change the size of the above picture to 720
and 1280
, then calculate the screen size. If it is 360 x 640
dp, then change the size of the above picture to 360
and 640
, just calculate the screen size. You don’t need to care about what the unit is. You can just write as many as you want in the design diagram, without conversion. When adapting, just pass in the size value of this dimension. For example, if you want to adapt the width of 720 x 1280
, just pass in 720
.
Principles and usage
The principle is actually based on the principle of Toutiao, but I am operating pt
, so instead of changing DisplayMetrics#density
, I change it to DisplayMetrics#xdpi
. The configuration will not be invalid, so you need to rewrite the getResources()
function in the adapted Activity
, because every time the View
changes size getResources()
will be called, so if we adapt directly here, it will not cause failure. The corresponding codes in the renderings are as follows:
override fun getResources(): Resources { return AdaptScreenUtils.adaptWidth(super.getResources(), 1080) } override fun getResources(): Resources { return AdaptScreenUtils.adaptHeight(super.getResources(), 1920) } override fun getResources(): Resources { return AdaptScreenUtils.closeAdapt(super.getResources()) }
Its source code, Demo and API are as follows:
AdaptScreen related -> [AdaptScreenUtils.java][adaptScreen.java] -> [Demo][adaptScreen.demo]
adaptWidth: Adaptation width adaptHeight: adapt height closeAdapt: close adaptation (pt is equivalent to dp) pt2Px: pt to px px2Pt: px to pt
pt2Px and px2Pt are provided for View
that require dynamic operations.
As above, you only need to rely on the latest version of AndroidUtilCode:
implementation 'com.blankj:utilcode:1.22.3'
Final words
After reading the principle, you think it is very simple, but how many people can think of this solution? I also stood on the shoulders of giants to think of this level. I hope this adaptation solution can be like the title of the article. Terminate our adaptation. This is the simplest and most effective adaptation solution I have found so far. If you think it is good, please remember to recommend it to the Androiders around you. If you have any problems during use, please synchronize to the “Android Screen Adaptation Terminator” problem summary. issue.
Finally, I will add some content. The AndroidUtilCode project has been transformed into a component. If you are interested, you can check out the source code. I believe you will definitely learn a lot of good things. I will introduce this aspect to you in the next article. I look forward to it. I can give birth as soon as possible.