Theme background image of Android performance optimization game

Recently, the memory of the game was optimized. Through the memory snapshot, it was found that the theme background image of a certain Activity took up more than 3M. Consider starting alignment for optimization.

question

Check the memory snapshot in the game and find that there is a picture bitmap occupying more than 3M, which is set in the background of the Activity:

insert image description here
View the source code in PhoneWindow: Found that its mBackgroundDrawable generates a corresponding picture from the windwoBackground property in Theme.
insert image description here
Add a picture as a background to the theme in the Activity of the main entrance of the program, which is often used to solve the problem of starting a black and white screen .

It is found that the background image has been set in the theme of the splash screen page in the game project, so here, there is no need to consider the black and white screen problem.

Then it was discovered that the background image was set in the game interface for the transitional image before the game engine renders it. But there are two repeated setting problems, one is the bg in the theme, and the other is the backgroud in the root layout of the game. The picture is also set, as shown below:

insert image description here

solution

In the game interface, set the windowbackground in the theme to another color, and remove the background. Save 3M of memory, especially important for devices below android 8.0.

Compile the new package, check the memory snapshot, and find that the bitmap corresponding to the picture is missing, and the memory usage of 3M is reduced.
insert image description here
After testing on multiple devices, there will be no white screen problem, which proves that the solution is feasible.

Summary :

  • Carefully use the Theme's windowBackgroud to set the background image. It is recommended to use a dynamic view to set the background image. When not in use, remove it to save memory.
  • Prevent the Theme's windowBackgroud and the Activity's root layout windowBackgroud from being drawn repeatedly, causing memory waste.

Guess you like

Origin blog.csdn.net/hexingen/article/details/131922311