Copyright notice: This article is the blogger’s original article, shall not be reproduced without the permission of the blogger.

As apps become more and more functional, the size of apps also grows. This will bring users trouble to upgrade and affect the user experience, so it is especially important to slim down the app. There are many ways to slim down your app. A quick search will reveal many options, such as enabling Obfuscate, shrinkResources, and removing large third-party libraries. Are simple and easy to operate, and the effect is obvious. In addition, image resources in APK also occupy a very large part, currently Google official recommended method is to use WebP instead of JPG, PNG and other pictures, here is mainly the use of WebP in Android project practice, to still hesitate to provide you with a clear guide.

What is Webp?

WebP is Google’s new image technology, which allows web pages to be effectively compressed. At the same quality, WebP images are 40% smaller than JPEG images, thus making the overall web page download faster. To improve JPEG image compression, they used a VP8-encoded image compressor using predictive coding, along with a very lightweight riFF-based container. This container adds only 20 bytes to each image, but allows the image author to store as much metadata as they want.

Android support for WebP

Android also as a Google product, minSDK 4.0, namely api14 above can support WebP, but there are some problems for transparent pictures, MinSDK 4.2.1+, namely api17 can perfectly support WebP. Considering that the proportion of mobile phones below 4.2.1 in the current market is very rare, it is very feasible to replace JPG and PNG with webP format.

PNG, JPG and other formats to webP tools

Webp, Google’s official image format, certainly provides conversion tools: Google has released webP file decoders (libvpx) and command line tools (webPconv) for different operating systems.

Download tools: developers.google.com/speed/webp/ need over the wall oh ~

For Windows, we need webpcodecsetup. exe (a webP image viewer plug-in that allows you to view webP images) and libwebp-0.5.1- Windows-x86. zip (conversion core tool). For MAC or Linux, there are conversion tools.

It is also very easy to use, unzip libwebp-0.5.1-windows-x86.zip, go to the bin directory to see cwebp.exe, this is the transcoding tool.

For Windows, open the DOS command here and type:

cwebp D:\from\test.jpg -o D:\to\test.webpCopy the code

Pictures can be converted.

However, this can only convert one image. What if I have a lot of images to convert? Of course, there is a way, here need to use DOS cyclic execution command, first create a TXT, write the following content:

@echo off for %%a in (D:\from\*.jpg,D:\from\*.png) do ( cwebp %%a -o D:\to\%%~na.webp )Copy the code

For a quick explanation, %%a is a variable that indicates that all.jpg and.png files in the D:\from directory are converted to cwebp and output to the D:\to\ directory, ending with.webp.

Save the file, change the suffix to. Bat, and place it in the bin directory.

Double-click and you will find that all JPG and PNG images in the from directory are converted to webP and placed in the to directory!

If you only want to convert a few images, or can’t flip the wall ————

In addition, I also found some online conversion sites (reflecting the popularity of webP format) :zhitu.isux.us/



You can see that there are different qualities of compression, and high quality compression can also greatly reduce the size of the image.

The Demo test

In Android projects, webP images are used no differently than JPG images, so simply replace the image in the project with the corresponding WebP format image, without changing the code to achieve image and APK compression. (Note minSdkVersion 17)

Next, we will compare the compressed WebP pictures of different qualities with the original picture to check the effect:

1. The first is lossless compression

You can see that the size of the image has been cut in half.

Almost indistinguishable from the naked eye, WebP lossless compression can reduce the size of an image by half to achieve the same lossless effect.

2. Webp with 75% compression loss

The compressed image is 44 percent the size of the original image. Again, let’s look at the image on the phone

Contrast picture quality, coarse look words can not see the difference, close look words or can see the line slightly advantage fuzzy.

3. Webp that loses 30 qualities

The compressed image is only 22 percent the size of the original image

Personal feeling is no different from 75% compression…

4. Minimum quality compression

The compressed image is only 20 percent the size of the original image

This is obvious, but for small images, you can use the lowest quality compression to dramatically reduce the image size.

conclusion

Webp format images can effectively reduce the size of image files with little or no impact on image quality. If the quality of the picture requirements, or some full-screen large pictures, can be lossless compression, can reduce the size of 50%; If the quality of the picture is not too high, you can use the lowest quality compression, can reduce 80% of the picture size;

Disadvantages:

1. Only 4.2.1+ is supported. If downward compatibility is required, the so library needs to be added and the code needs to be changed, which makes the implementation complicated. 2. Although Android can use Webp like PNG, that is, without changing the code, it is impossible to preview webP images in Android Studio, which makes the development and debugging become troublesome. I don’t know whether the subsequent version will add support.

If you have any questions, please leave a message at ~~~~

Guess you are looking for