Due to my previous information, the author did not say the version number. This is an adaptation made when I made SDK3 and SDK5.
Preface:
The author has little experience in making Android, but only has the knowledge of using game engine. When he made Hongmeng, he worked for half a year in 2020-12. In fact, for software, sometimes it’s not necessary to adapt, because the system will separate the modules that you need, but not for games, we have to follow our own coordinates. At that time, we were in a hurry to finish the production. Although adaptation was considered in the test, there were no abundant machines and corresponding tests were not carried out. As a result, when The Hongmeng Smart screen was updated, Huawei had more models, which was immediately exposed. Because we had been using the coordinates of the way the px (pixels), huawei’s use dp (px, generate different according to different resolution, and subsequent to, they said the vp, they seem to want to let more people to abandon some of the android, such as the earliest logcat exists, then become hilog), first I make adaptation scheme, It is considered to change px into DP, but most of the content is completed by our staff, so px is almost full of the code, and according to my calculation, DP and PX are not equal proportion, DP is suitable for software adaptation, but not suitable for game adaptation.
Body:
– DependentLayout is used when creating a game and does not affect each other.
First we fit, to get the equipment the length and width (DisplayManager. GetInstance () to obtain the device length and width, and this will include Liu Haibing that part,,, there are other can remove Liu Haibing after long and wide, Look for it) DisplayManager dm = DisplayManager. GetInstance (); Point p = new Point(); dm.getDefaultDisplay(context).get().getRealSize(p); If (p.osition [1] > p.osition [0]){deviceHeight = p.osition [0]; deviceWidth = p.position[1]; }else{ deviceHeight = p.position[1]; deviceWidth = p.position[0]; }Copy the code
Now, if you look at this picture, this is the adaptation principle.
What we know is deviceWidth and deviceWidth ok, first we need a base resolution for doing it (normally we do games 540*960), but the smart screen we were given was 1920-1080, so we used that as the base resolution. AppWidth and appHeight (this is the content of the game screen, appMLR and appMTB are redundant parts of the different resolutions)
The deviceWidth/deviceWidth ratio is 16/9(because 1920/1080 is 16/9).
- If yes, it’s a normal screen, and if you calculate the corresponding scaling,
appWidth = (int) deviceWidth;
appHeight = (int) deviceHeight;
- The width and height of the device is larger than that of the normal screen, proving that it is a long screen, the height is the height of the device, and the length has offset
appHeight = (int) deviceHeight;
appWidth = (int) (deviceHeight * appRatio);
appMLR = (int) ((deviceWidth – appWidth) / 2);
- The width and height of the device is lower than that of the normal screen, proving a widescreen screen. The length is the length of the device, and the height has offset
appWidth = (int) deviceWidth;
appHeight = (int) (deviceWidth / appRatio);
appMTB = (int) ((deviceHeight – appHeight) / 2);
Finally calculate a game screen and the basic screen of the proportional relationship
appUnit = appWidth * 1f / 1920;
Now that we’ve computed these values, it’s easy
Size of the adaptive component (the transmission value is W: length under the base screen, h: height under the base screen)
DependentLayout.LayoutConfig config = (DependentLayout.LayoutConfig) c.getLayoutConfig();
float unit = DeviceManager.get().appUnit;
config.width = (int) (w * unit);
config.height = (int) (h * unit);
c.setLayoutConfig(config);
Copy the code
Coordinates of adaptive components (x, y)
MarginLeft: appMLR +x * DeviceManager.get().appUnit;
MarginBottom: appMLR +x * DeviceManager.get().appUnit;