“This is the 10th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”
👉 About the author
As we all know, life is a long process of constantly overcoming difficulties and reflecting on progress. In this process, there will be a lot of questions and thoughts about life, so I decided to share my thoughts, experiences and stories to find resonance!!
Focus on Android/Unity and various game development tips, as well as various resource sharing (websites, tools, materials, source code, games, etc.)
Welcome to pay attention to the public account [Mr. Empty name] for more resources and communication!
👉 premise
Front small empty insist to write Android novice series, welcome to taste.
In the previous article we learned the layout optimization content, today we say picture optimization of the fast zai river’s lake!!
Pick up where we left off.
👉 Practice
First of all, I recommend a compression website tinypng.com/
If you’ve ever done image optimization, you probably know this site, because it’s the number one search engine, but it’s probably not for new Android developers.
This site compresses images very, very well. It compresses the original file, and even if you zoom in, it’s almost indistinguishable visually. Awesome.
I’ve gotten into the habit of compressing images before I use them. It does have a client with the following interface.
Coming back to Android technology, there is officially a BitmapFactory class that provides a number of parsing methods
DecodeStream method generally comes out from the network images,
DecodeFile method can make SD card picture
The decodeResource method processes the image inside the resource file.
These methods will allocate memory for the Bitmap created and will result in OOM if the image is too large.
Bitmapfactory. Options provides a parameter, inSampleSize, to help with compression.
Here’s an example:
If we have a 6666 by 6666 image and set inSampleSize to 6, then the image will load into memory as 1111 by 1111 pixels, 1/6 of its width and 1/36 of its height, which should theoretically take up 1/36 of its space.
Note: The value of inSampleSize must be a multiple of 2. If it is less than 1, it defaults to 1. If it is odd, it defaults to minus 1
Achieve size compression:
Quality compression can not avoid OOM, but can change the image in the disk or File File size, size compression can avoid OOM, but does not change the size of the image itself, only change the size of the load in memory, namely bitmap.
Speaking of the overall compression, we will introduce another scene: a mobile phone screen shows a large picture!!
For example: 3840*2160 hd wallpapers.
The above compression scheme will definitely result in a blurred image and a poor experience.
So we have to use partial display to show the image.
Android for us to provide a class: BitmapRegionDecoder to partial display of the picture, it can achieve the display of the image of the specified area.
-
BitmapRegionDecoder provides a series of newInstance initialization, supporting the incoming file path, file descriptor and file stream InputStream
-
The decodeRegion method is then used to specify the display area.
-
Finally, add some gestures.
Hongyang Dashen article has already had a tutorial, in this do not repeat.
Specific reference: blog.csdn.net/lmj62356579…
😜 other
There is also a practical scheme. The unit has such a scenario: the teaching content is usually fixed, just like the instruction manual, with colorful and green styles. It is not cost-effective to write the layout code, so it is directly displayed with pictures.
Although the width is usually 1080 pixels, the height can be as high as 50000 pixels. So directly cut the large picture into a number of 1080*600 pictures, and then use RecyclerView to achieve. Item is just an ImageView. Very easy;
😜 summary:
-
When the image is loaded into memory, it will go through resolution conversion and then calculate the size, which is influenced by the device DPI and the DPI resource folder of different density. So the image must be under the corresponding drawable
-
Do a lossless compression of the image from the root before using it.
-
The above two scenarios solve most scenarios, then consider size compression/mass compression
-
For the giant diaosi map, see the business scene to choose a local display scheme or directly cut multiple picture scheme.
👉 other
📢 author: Kom and Kom in Kom
📢 reprint instructions – be sure to specify the source: Zhim Granular’s personal home page – column – Nuggets (juejin. Cn)
📢 welcome to like 👍 collect 🌟 message 📝