Screen adaptation is our Android development, the platitude of the problem!
Interviewer: Your resume says you’re familiar with Android screen adaptation, so tell me about it
I:
1. Why should we adapt?
Android system is open, is open source, any user, developers, mobile phone manufacturers, operators can customize Android, such as Xiaomi, Huawei and so on, there are many industrial boards, using The Android system, so that leads to a variety of devices running Android system, Since there are so many devices, they have different screen sizes and pixel densities. Although the interface can be adapted to different screens through basic zooming and resizing, Android developers should make further improvements to make our apps look beautiful on all screens
2. Some concepts about screens
Screen size: Screen size refers to the diagonal length of the screen, in inches (1 inch =2.54cm)
Like the size of my mi 11Pro
Screen resolution: screen resolution refers to the pixels in horizontal and vertical points, generally in vertical pixels * horizontal pixels, 1920*1080 means that the width direction has 1080 pixels, the height direction has 1920 pixels, the whole screen has 1920*1080 pixels
Unit px 1px=1 pixel point
Screen Pixel density (DPI) : Screen pixel density is the number of pixels per inch in dPI, short for dot per inch. Screen pixel density is related to screen size and screen resolution. Under a single changing condition, the smaller the screen size, the higher the resolution and the higher the pixel density
The density of the type | Represents the resolution (PX) | Screen Pixel Density (DPI) |
---|---|---|
Low density (LDPI) | 240×320 | 120 |
Medium density (MDPI) | 320×480 | 160 |
High density (HDPI) | 480×800 | 240 |
Ultra High Density (XHDPI) | 720×1280 | 320 |
Ultra High Density (XXHDPI) | 1080×1920 | 480 |
The resolution is width by height
Density -independent pixel, DP or DIP are the width units we used for Android development to ensure the same effect on devices with different screen pixel densities
Dp and PX conversion
Dpi is the pixel density of the screen, and 160 is an official value set by Google,
Use 160dpi (320×480 screen resolution) as the baseline: 1dp=1px
Looking at the above data, we found that when we used DP during the redevelopment process, Android adapted according to the screen pixel density, but why did we write the page is still not ideal?
3. Toutiao adaptation scheme
That’s because we screen width when the UI in figure to us is a fixed value, but because of the diversity of the Android mobile phone screen, screen pixels with dp, length and diversity, so we according to the size of the UI gives the effect, effect is not very ideal, so in order to solve this problem, we have to change my several parameter values, to achieve, We wrote the code according to the size of the design diagram given by the UI to achieve a percentage fit on other Android devices!
For example, the density of a design with a width of 360DP is dpi/160
public static void setCustomDensity
(Activity activity,final Application application){
final DisplayMetrics appDisplayMetrics
=application.getResource().getDisplayMetrics();
final float targetDensity=appDisplayMetrics.widthPixels/360;
final int targetDensityDpi=(int) (160*targetDensity);
appDisplayMetrics.density=targetDensity;
appDisplayMetrics.densityDpi=targetDensity;
//Activty's DisplayMetrics should also be set
final DisplayMetrics activityDisplayMetrics
=activity.getResource().getDisplayMetrics();
activityDisplayMetrics.density=targetDensity;
activityDisplayMetrics.densityDpi=targetDensity;
}
Copy the code
This can be adapted according to the percentage, screen width pixel/design draft size can be 1DP = how many pixels of the value calculated, that is, the density of the value calculated, can be calculated 360DP DPI, after setting, and then according to the size of the design draft, can be achieved and the percentage of the design draft adaptation
This is the adaptation of the core program, feel the adaptation of most of the equipment effect is very good!
Hongyang recommended library autoSize is also used in this scheme!