Memory usage calculation formula for images
Does the same image occupy the same amount of memory when displayed on mobile phones with different resolutions?Inconsistent. Because the inDensity value is the same, but the targetDensity value is not, the image scaling is different, and the memory usage is naturally different. When loading the same picture from folders with different resolutions on the same mobile device, will the memory usage be the same?Although the inDensity values are different at different resolutions, as long as the designer cuts the image according to the specification, the aspect ratio between images of different resolutions = the ratio between inDensity. Therefore, when images of different resolutions are displayed on the same device, the final values after the image width and height are scaled are consistent, and the memory is also consistent. Does the size of the ImageView control affect the memory usage?No. Because through the src attribute, when we get the corresponding drawable, we set the density to 0. When the density is 0, the width and height of the image will not be scaled, so the memory size will not change. Memory size = original image width * original image height * color depth For the same resource id, do the bitmap created by BitmapFactory and the src set by ImageView in the xml file occupy the same amount of memory?There will be inconsistent scenes. If inDensity and targetDensity are inconsistent, the bitmap created by BitmapFactory will be scaled, resulting in memory size != original image width * original image height * color depth. However, for images set by xml through the src attribute, because density=0, the image will not be scaled at all, and the memory size = original image width * original image height * color depth Will setting the image size in a third-party image loading framework affect the memory usage of the image?Yes. The essence of the operation of setting the image size in the third-party framework is to modify the width and height of the image. When the width and height of the image change, the memory size occupied will naturally change. Android image adaptation rulesFirst, find the drawable folder corresponding to the dpi of the mobile device. If it cannot be found in the current folder, the strategy is to reduce the image first. So it will then look for the image in the high-resolution folder. If it is not found in the high-resolution folder, it will then search in the low-resolution folder. For example: the dpi of a mobile device is 320 (xhdpi). If you cannot find the corresponding image in the drawable-xhdpi file, then search in the drawable-xxhdpi and drawable-xxxhdpi folders. If you still cannot find it, search in the drawable-hdpi and drawable-mdpi folders. Why is it recommended that images be placed in folders with the correct resolution?Only analyze the resolution of mobile devices with xxhdpi, and think about other resolutions yourself, targetDensity=480 The resolution is xxhdpi, put it in the correct drawable-xxhdpi folder. inDensity=480, targetDensity=480, image scaling scale=1 The image with resolution of xxhdpi is placed in the wrong drawable-xhdpi folder. inDensity=320, targetDensity=480, image scaling ratio scale=1.5 The memory size has changed from width*height*pointMemory to (width*1.5+0.5)*(height*1.5+0.5)*pointMemory, resulting in a significant increase in the occupied memory. On a mobile device with a dpi of 320, if I load a picture in drawable-xxxhdpi, will the memory occupied be the same as that of a picture in drawable-xhdpi?Of course, when loading high-resolution images, the images will be scaled down, and the image size will be scaled to the same as other resolutions, so the memory size occupied is the same Since the same image in different resolution directories is loaded on our mobile phone, the memory size occupied is the same, so why do we need to create multiple drawable directories? Wouldn't it be possible to reduce the package size by directly using drawable-xxxhdpi?There is a problem involved here, that is, in the XML file, we use the ImageView control to directly reference the image resource through the src attribute. When referencing image resources through XML files, the memory size occupied is as follows: Because through the src attribute, when we obtain the corresponding drawable, we set the density to 0. When the density is 0, the width and height of the image will not be scaled, so the memory size will not change. Memory size = original image width * original image height * color depth So if I have a 320 phone, I load an image in xhdpi, assuming the image is 18*12 argb8888, then the memory usage is 18*12*4, but if I only have images in xxxhdpi, assuming the image is 36*24, then the memory usage is 36*24*4, which is 4 times the memory usage. The images in our drawable directory are almost all used for XML reference images, and are rarely created with bitmapFactory, so it is best to put images of corresponding resolutions in each folder. |
<<: Difference between FragmentPagerAdapter and FragmentStatePagerAdapter
>>: The basic principles and implementation details of calling APP from the Web in Android
In life, we often encounter a scenario: for examp...
Early morning of October 16, 2021 Shenzhou 13 man...
What is event operation? My understanding of even...
In the 1990s, the movie "Jurassic Park"...
Question 1: How do you evaluate the effectiveness...
When the spotlight of the stage finally fell on L...
WeChat has begun to crack down on third-party web...
This era does not only belong to young people. In...
In April 2014, Hunan Television announced that it...
↑ User comments There is a common skin disease th...
In May 2024, the pickup truck market sold 45,000 ...
Stocking up on radishes means stocking up on the ...
With the continuous development of China's eco...
One day 4000 years ago The mythical hero Dayu Pas...
When talking about Mimi Meng and Uncle Tongdao , ...