Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of slimming down Android picture resources?

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "what is the method of slimming Android picture resources". In the daily operation, I believe that many people have doubts about the method of slimming Android picture resources. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "what is the method of slimming Android picture resources?" Next, please follow the editor to study!

one。 Select Compression tool

The first is the question of choosing a compression tool, and before that, let's take a look at how the system does it. Android's aapt actually compresses png images during the compilation phase, using libpng and zlib, which can be proved by the source code of aapt:

Preprocess the picture with libpng

Compress the preprocessed picture with zlib to generate a new picture.

You can see that aapt used the highest compression level of the picture 9, during which the system will also do color table conversion, which can reduce a large part of the image volume, but the system compression scheme is perfect? At present, the commonly used lossless compression is probably Pngrewrite, pngcrush, optipng, advancecom, pngout, referring to a lot of articles, the result is that pngout is still king, after all, it is said that the author of Doom and Quake is a child prodigy of Ken (it is said that Doom and Quake's author John respects him, and games must know John). In addition, because pngout can well support the command line, it is easy to put into the compilation script automation, so choose it for the time being.

two。 Practical case

Once the compression tool is selected, the second step is to experiment. Take Mobile Q as an example. All the png in the direct opponent Q are pressed once. The speed of Pngout is really average. It takes about 13 minutes to process all 4, 000 images, but this process only needs to be done locally, so it can be tolerated, but the result is not ideal. Because there is no effect, the reduction is more than a dozen KB~~. After careful analysis, we know that it is aapt who is in trouble. The compression effect will be overwritten by adjusting pngout first and then aapt. So can I turn off aapt? Check the parameters of aapt. There are only the following two parameters related to compression:

Crunch is used to preprocess resources, but there is no parameter to turn off crunch. Whether it's right to run out of skills. You can only look for inspiration in the source code and look at the source code of aapt:

Google hides it, but does not print it out to the user to enable this parameter. Add the-no-crunch parameter to the resource packaging script in Mobile QQ to compress the system and mask it. The style is as follows:

As for why it can be shielded when this parameter is set, the procedure for calling the source code is as follows:

Step 1 (Main.cpp)

Step 2 (Command.cpp)

Step 3 (Resource.cpp)

End: (Resource.cpp)

However, the experiment is not over yet, because a strange scene will appear when it is shielded. The effect of Mobile QQ's picture is as follows:

Why? After careful analysis, it is found that there is something wrong with the Nine Miyagi grid image. Aapt will determine whether it is a Nine Miyagi grid picture when processing the png image, and if so, do special pre-processing:

In fact, the main purpose of do_9patch is to get the information of the ninth palace grid, write it into the info9Patch field, and finally write it into the chunk of nptc:

Back to the first step here why I said Ken is a child prodigy, because Pngout can choose chunk for compression, so the solution is: for nine grid images, we take them out separately, first use aapt's aapt crunch for pre-processing to get the npTc field, and then use pngout to call the "knptc" parameter to protect the npTc block when compressing, so that you get the correct nine palace grid picture, and the effect of the installation package is normal.

At this point, the study on "what is the method of slimming Android picture resources" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report