In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use Bitmap efficiently". In daily operation, I believe many people have doubts about how to use Bitmap efficiently. 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 about "how to use Bitmap efficiently". Next, please follow the editor to study!
Background: image loading can be seen everywhere in a project, and in many cases the Bitmap (bitmap) class is required for image loading. Bitmap can be said to be a "fat man" because Bitmap itself stores all the attributes of each pixel of the picture in memory. This will cause us to inadvertently create a Bitmap object that takes up a lot of memory, which leads to slow loading speed, such as OOM (Out of Memory).
It is necessary to optimize Bitmap so as to reduce the probability of Crash, improve the speed of loading pictures in app, and enhance the stability of APP.
1 Application scenario
Take a chestnut.
The above code often appears in our projects. Do you know the processing logic behind it?
According to the source code, the two methods setBackgroundResource () and setImageResource () in the above code use Bitmap as a transition. At run time, the above two methods use the BitmapFactory.decodeStream () method to generate a Bitmap from the resource image, then generate a Drawable from this Bitmap, and finally set the Drawable to ImageView.
If you use the above code, the size of the loaded image is much larger than that of ImageView, you will find that the control loads the image very slowly. When you bulk load some unknown size images in ListView or RecycleView, you will find that stutters occur.
2BitmapFactory
BitmapFactory is a class that Google officially provides us developers with the way to load pictures. It can load pictures from files, image transfer streams, and byte arrays.
Introduction to the 3Options class
To achieve efficient loading of Bitmap, we first need to understand several parameters of the Options class, because it is through the reasonable configuration of these parameters that we can load Bitmap objects efficiently. The Options class is a static inner class of BitmapFactory. Let's take a look at its source code:
Explain a few nouns:
Screen density
Screen density is divided into relative screen density and absolute screen density.
Density: it can be understood as relative screen density. We know that a DIP is about 1 pixel on a 160dpi screen. We use 160dpi as the baseline, and the value of density is the relative screen density relative to the 160dpi screen. For example, the density value of the 160dpi screen is 1 and the density value of the 320dpi screen is 2
DensityDpi: can be understood as absolute screen density, that is, the actual screen density value (dots per inch). For example, the density DPI value of 160dpi screen is 160d.
Scaling factor
There is an inScaled parameter in the Options class, which indicates whether scaling is supported. We can see from the default construction method of Options that this parameter is initialized to true, that is, scaling is supported by default. So how will it be scaled? The answer is to scale according to the scaling factor. As for the method of calculating the scaling factor, we have already introduced it when we explained how to calculate the size of the Bitmap in memory. The scaling factor is inTargetDensity divided by inDensity. InDensity indicates that the dpi,inTargetDensity corresponding to the resource folder where our images are located represents the screen density of the target device.
Sampling rate (inSampleSize)
When this parameter is 1, the size of the sampled image is the same as the original; when this parameter is 2, the width and height of the sampled image is the original 1 + 2, and the size becomes the original 1 + 4. That is, the size after sampling is equal to the original size divided by the square of the sampling rate. The official document states that the value of inSampleSize should be a non-negative integer power of 2. Otherwise, it will be rounded down by the system and find the nearest value By setting inSampleSize, we can scale the picture to a reasonable size.
4Bitmap optimization
1. Get the original width and height of the picture
By calling the decodeResource method after setting the inJustDecodeBounds property of Options to true, you can get the size information of the picture instead of actually loading the picture.
2. Calculate the inSampleSize based on the original width and height. The code is as follows:
3. Generate Bitmap according to the calculated inSampleSize
4. Call the above decodeSampledBitmapFromResource method to use self-sized Bitmap. If you want to set a large image to a 250 "250 thumbnail, execute the following code:
At this point, the study on "how to make efficient use of Bitmap" 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.