Summarize the image optimization of Android App memory optimization

preface

When the memory of Android devices goes to G all the time, it's really unnecessary to pay too much attention to the memory consumption of Android system by app, but in practical work, I do an educational primary school app. The buttons, background and animation transformations in the app are basically pictures. A background picture on 2K screen (resolution 2048 * 1536) will occupy 12m of memory, Switching back and forth several times will increase the memory consumption to hundreds of megabytes. In order to reduce the memory consumption of app by various means without affecting the visual effect of app.

Through the analysis of the app memory usage viewing tool of DDMS, it is found that the pictures occupy the most memory in app, and the pictures occupy most of the memory in each activity. This paper focuses on the memory optimization of pictures.

Do not set the background of the button to selector

    in the layout file and code, you can set the background of the button as selector, which is convenient to realize the positive and negative selection effect of the button. However, the actual tracking shows that if the background of the button is set as selector, the positive and negative selection images will be loaded into memory when initializing the button (for details, you can check the Android source code, parse the image in the createfromxmlinner method of class drawable.java, and finally call drawable's inflate method) , which is equivalent to that one button occupies the memory used by two pictures of the same size. If there are many buttons on an interface or the buttons are large, the memory occupied by the buttons alone will be large. You can only set the background picture in the normal state for the buttons in the layout file, then monitor the click state of the buttons in the code, and set the anti selection effect for the buttons when the buttons are pressed The picture of the fruit is reset to the background in the normal state when lifted. The specific implementation method is as follows:

   the above method can solve the problem that the same button occupies twice the memory. If you think that providing positive and negative selection of two pictures for a button will lead to the increase of the volume of APK, you can realize the negative selection effect of button clicking in the following way. This method will not occupy twice the memory of the button, but also reduce the volume of APK (tintcolor in Android 5.0 can also achieve similar effects):

Draw the background picture on the non UI thread to improve the efficiency of the app

   on high-resolution tablet devices, drawing large background pictures will affect the operation efficiency of the program. In serious cases, it is the same as using the handwriting function without hardware acceleration. Finally, our solution is to draw the background pictures through surfaceview, which is equivalent to drawing in non UI threads and will not affect the UI threads to do other things:

  in RES / values / attr Define the custom attributes of the custom view in the XML file:

It is not necessary to use the hardware acceleration interface. It is recommended to turn off the hardware acceleration

   through the heap tracking of DDMS, it is found that compared with turning off hardware acceleration, turning on hardware acceleration will consume more memory, but turning on or off hardware acceleration in some interfaces does not have much impact on the running efficiency of the program. In this case, it can be considered in androidmanifest Turn off the hardware acceleration of the corresponding activity in the XML file, like this:

Note: if WebView, video playback, handwriting, animation and other functions are used, turning off the hardware acceleration will seriously affect the operation efficiency of the sound program. In this case, only the hardware acceleration of some views in the activity can be turned off, and the hardware acceleration of the whole activity will not be turned off.

   if a view in the activity needs to turn off hardware acceleration, but the whole activity cannot be turned off, you can call the view level method to turn off hardware acceleration:

Minimize the use of animationdrawable. If necessary, you can customize the picture switcher instead of animationdrawable

   animationdrawable also consumes a lot of memory. The more picture frames, the more memory they consume. You can check the source code of animationdrawable. When animationdrawable is instantiated, the createfromxmlinner method of drawable will call the inflate method of animationdrawable. In this method, there is a while loop to read all frames at one time, That is, all frames will be read in memory during initialization. How many pictures there are, it will consume the memory of the corresponding size.

   although the memory occupied by animationdrawable can be released in the following ways, when you exit the interface using animationdrawable and use it to play animation again, you will report an exception of using the recycled image. This should be caused by Android's image processing mechanism. Although the activity is finished, However, the pictures used in this activity are still in memory. If they are recycled, an exception message will be reported when entering next time:

    usually, I will customize an ImageView to realize the function of animationdrawable, and set the background picture of ImageView regularly according to the switching time interval between pictures. In this way, it is always just an ImageView instance, only its background is replaced, and the memory consumption will be much smaller than that of animationdrawable:

Other optimization methods

1. Try to combine the small picture in the activity with the background. A small picture not only wastes the layout time, but also increases the memory occupation;

2. Do not set the default background picture for the activity in the theme of the activity, which will double the memory occupied by the activity:

3. For images or layouts that are displayed only when needed, you can use the viewstub tag through the hierarchyviewer. Org in the SDK / Tools Directory Bat viewing the layout file, you will find that components using the viewtube tag hardly consume layout time. De instantiation in the code when it needs to be displayed helps to improve the layout efficiency of the activity and save the memory consumed by the activity.

summary

The above is the whole content of this article. I hope it can be helpful for everyone to develop Android. If you have any questions, you can leave a message for discussion.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>