Android loads the relationship between different DPI resources and memory consumption

Source: Internet
Author: User

by'll of tinylab.org
2015/04/21

Introduction to the Android DPI rating standard

Android devices vary greatly in physical size and screen density, in order to simplify the design of multiple devices by setting a set of grading criteria. The grading criteria on screen density are: ldpi, MDPI, HDPI, XHDPI, which is the DPI of all sizes (Dots per inch). DPI is the measure of screen pixel density .

What are the issues with different devices sharing the same set of DPI resources?

Now get to the point.

Q: Many companies use a set of DPI resources for simplified design and research and development, which is not a good idea.

A: Android has a set of rules to load resources, if the corresponding DPI folder does not exist to use the resources will follow the rules to find other DPI folder, if you can eventually find the use. So the above scheme is "feasible" – it can operate without error. Another aspect of the feasibility is the impact on performance. The above question becomes the following question:

Q: What is the difference in memory consumption when using the same set of DPI resources on different phones? Or how is the memory consumption of resources in the App loaded in different DPI folders different?

Problem: The smaller the DPI the larger the memory consumption of the folder?

The following is an example of PNG image loading.

Original picture (Attribute-width:960,height:540,bit depth:32,size:217082bytes).

Do a simple demo app that only loads this image in the activity.

Put in the hdpi folder, Dumpsys Meminfo found that the Heap Alloc is 5420, much larger than size, so it is certain that the memory consumption is independent of the picture file size.

And then put it in a different DPI folder to find: the more DPI Small folder memory consumption is greater!

Analysis: Loading a low DPI resource will stretch the image in extra magnification

Since heap Alloc can only see the overall size of the heap allocation, and cannot see what the above findings are "regular", the MAT is then used for analysis.

In hdpi, grab the hprof file and open it with the MAT:

See the byte array in the figure, size 2073600, which is the size of the memory occupied by the PNG image that was loaded.

The MDPI, ldpi, and xhdpi are analyzed separately, and the byte array sizes are: 4665600, 8294400, 1166400, respectively. The memory size of the different DPI folders and pictures is as follows:

DPIs ldpi mdpi hdpi xhdpi
byte[] Size 8294400 4665600 2073600 1166400
Ratio 8^2 6^2 4^2 3^2

Starting with the Android screen density grading standard is ldpi, MDPI, HDPI, XHDPI These various sizes of DPI. That is, ldpi devices use the resources under the LDPI folder by default. Then, depending on the size of the DPI value, the screen pixel density values correspond to the following DPI folder relationships:

DPIs ldpi mdpi hdpi xhdpi
Density 120 160 240 320
Ratio 3 4 6 8

Based on the Ratio values of the above two tables, it is possible to find that memory consumption and DPI resources are regular. In fact, we know that the consumption of PNG loaded memory is independent of the file size, but is related to the length and depth of the PNG image , i.e.:

Memory consumption Size (uom:byte) = Width * Height * (Bit DEPTH/8)

The above formula is not fully used in Android, according to the rules found above, the PNG image of Android memory consumption formula can be summarized as:

Memory consumption Size (uom:byte) = Scaledwidth * Scaledheight * (Bit depth/8) scaledwidth = Width * Factorscaledheight = Height * Factorfactor = density_device/resourcedensity  //Density_device is the DPI size of the device, resourcedensity is the DPI file that the device is loading DPI size of the clip

So:

Memory Consumption Size = Width * Height * (density_device/resourcedensity) ^2 * (Bit DEPTH/8)

2073600 of the above hdpi can be calculated from this:

960 * 540 * (240/240) ^2 * (32/8) = 2073600

Add log in BitmapFactory.cpp's Dodecode () to validate the above formula (resources in XDPI, SX, SY are the factor in the above formula):

01-15 21:00:49.479  3079  3079 D bitmapfactory:dodecode----sx:0.750000, sy:0.750000, scaledwidth:405, scaledheight:720, decodingbitmap.width:540, decodingbitmap.heigth:960

Android Load resource default selection and device DPI matching resources, if not go to other DPI folder to find resources. Once found, it will assume that resources with different dpi are used, and in order to remain consistent with the device DPI, the resource will be stretched or scaled to reload . The following PNG images are placed under the MDPI and xxhdpi folders, respectively:

mdpi xxhdpi

It is obvious that we can see a lot of blur under the xxhdpi. The test phone used is hdpi, but the default hdpi cannot find the picture resource, it will follow certain rules to find the resources I put in xxhdpi. The mobile phone thinks that the resource obtained from XXHDPI is higher than the DPI of the cell phone, it will reduce the resources according to the proportion of the table, that is, the image resource loaded into memory is 1/2 of the original size, and the memory will of course reduce the 1/4 of the image that does not do the scale operation. But the downside is also obvious, showing that the image of the mobile phone is reduced in clarity, a lot of blur.

Conversely, HDPI's phone loads low dpi resources, such as ldpi, which are scaled up before loading into memory. There is no problem when stretching and then showing the sharpness to the phone, but the memory footprint is up to 4 times times the original! It's also important to note that if the image resource puts the wrong DPI folder in the app, the experience will be compromised or use 9patch images as much as possible.

Summary: We recommend that you configure DPI resources based on your device

Now it's time to answer the questions raised:

Q: What is the difference in memory consumption when using the same set of DPI resources on different phones? Or how is the memory consumption of resources in the App loaded in different DPI folders different?

a: Do not use a set of resources for different DPI devices, so the clarity of the picture and memory consumption will be problematic. This is why Android requires different resources for different DPI files, and the aspect ratio of different DPI resources should correspond to DPI Ratio.

PS: The conclusion of this paper can be made clear, but the process of derivation is more important. Einstein said: Imagination is more important than knowledge .

Resources

Refer to the Android source code:

/frameworks/base/core/java/android/util/displaymetrics.java/frameworks/base/graphics/java/android/graphics/ Bitmap.java/frameworks/base/graphics/java/android/graphics/drawable/bitmapdrawable.java/frameworks/base/core/ Jni/android/graphics/bitmapfactory.cpp

Android loads the relationship between different DPI resources and memory consumption

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.