Android memory optimization image resources that testers should know

Comparing the memory test results of the old and new versions, we found that the memory usage of the new version has increased a lot. Using the MAT tool to analyze the memory occupied by the APP, it is found that the image resource, Bitmap object, occupies the largest memory resource of the APP. Our developers have mainly optimized in the following areas:

 

1. Place some default image resources in a higher screen resolution folder (for all models).

 

2. Use a solid color background skin to replace the original gradient color background image of the APP (for low-end machines).

 

3. Delay initialization of pictures (for low-end machines).

 

4. Disable rounded corner processing (for low-end machines).

 

5. Disable Gif. Since Gif images generally have many frame image structures, the memory usage is very large, and at the same time, continuous rendering is required, which will cause lag and power consumption (for low-end machines).

 

6. Turn off the animation effect, the animation needs to be drawn continuously, which will cause the interface to freeze and power consumption (for low-end machines).

 

This article mainly analyzes the technical principles behind the first optimization measures. (The rest of the optimization measures are actually removing some functions, sacrificing some functions in exchange for better performance)

 

Several basic concepts are involved here: pixel, resolution, density, target density, indensity, ARGB8888, calculation method of memory space occupied by image resources

 

Pixels: Every image is made up of color points, and each color point is called a pixel.

 

Resolution: The number of pixels in the horizontal and vertical directions. For example, the resolution of 720*1280 means that the number of horizontal pixels is 720 and the number of vertical pixels is 1280. In the case of the same screen size, the higher the resolution, the finer and more delicate the display effect will be.

 

The density value indicates how many display points per inch. Its unit is dpi: dot per inch. Usually, when the screen is large, the density is large, and when the screen is small, the density is small.

 

inDensity represents the set image density. This value is determined by the file directory where the image is placed, such as drawable-hdpi, drawable-xhdpi, etc. If the image is placed under drawable-hdpi, inDensity=240, if placed in drawable- Under xhdpi, inDensity=320.

density ldpi mdpi hdpi xhdpi xxhdpi
Density value 120 160 240 320 480
Representative resolution 240*320 320*480 480*800 720*1280 1080*1920

 

 

TargetDensity表示最终需要适配到的图片密度,这个值由手机设备来决定,手机屏幕越高清这个值越大。

 

ARGB8888:Android设备上图片都被处理成Bitmap对象。生成Bitmap有一个非常重要的参数Config,属性值有ALPHA_8、RGB_565、ARGB_4444、ARGB_8888四种。不同的属性值对应的图片每个像素点占用内存大小不同,ALPHA_8每个像素占用1byte,RGB_565和ARGB_4444占用2byte,ARGB_8888占用4byte,其中ARGB_4444在高版本中已经废弃。ARGB指的是一种色彩模式,里面A代表Alpha(A值代表透明度,当 A == 0xff 时不透明,当 A == 0x00 时完全透明),R表示red,G表示green,B表示blue,ARGB分别占8bit即1byte,因此一个像素占4byte。

 

在Android中,在不同的屏幕密度下,加载同一套资源所占用的内存空间是不一样的,Android在加载默认图资源时会根据资源所在的文件对应的像素密度以及自身像素密度比例缩放,具体公式如下(默认ARGB8888) :

BitmapSize = 4* WIDTH * (TargetDensity  /  inDensity)*HEIGHT *  (TargetDensity /  inDensity) 

 

假设一个Android APP的默认图片资源放在xhdpi下,在一台720*1280分辨率的手机上,加载到内存中的大小是4*720*1280=3.5MB,但是在一台1080*1920分辨率的手机上,加载到内存中的大小是4*720*(480/320)*1280*(480/320)= 7.1MB。可以看出将图片放到不同分辨率文件夹下,在不同density手机下加载到内存所占大小相差很多,因此图片放置的目录一定要慎重。我们可以将APP中一些较大的默认图片资源放到xxhdip文件夹下,这样在1080p手机下加载就不会缩放,在720p下加载会减少内存占用,虽然可能会导致渲染效率降低,但是基本影响不大。

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326801378&siteId=291194637