Recently, I was responsible for leading the company's project refactoring. During the refactoring, I found that the project was using two image loading frameworks at the same time, android-universal-image-loader and fresco. These two frameworks are actually good, but the project cannot use two frameworks at the same time. Because they need to allocate a certain amount of memory when initializing and running, which will cause the memory of cached images to increase. If you accidentally allocate too much, it may also cause invisible OOM. When I asked former employees, they all said they didn't know the specific reason and said it was a historical issue.
***An old employee said that in a place similar to the WeChat Moments feature, if the user selects multiple pictures to be sent, and then clicks to delete a picture, the remaining pictures will flicker. He said that Fresco cannot solve this problem, but imageloader will not cause flickering. Oh, we can't introduce a 700kb third-party framework just to solve a problem! Fresco is produced by Facebook and is trustworthy in terms of stability and ease of use. Andriod-universal-image-loader is relatively large and seems to have not been maintained and updated for a long time. *** decided to use the Fresco framework. If you use Fresco, you will have to solve the problem of flickering when loading images by yourself. In fact, the principles of all image frameworks are similar. First, load it from the memory. If it is not found, search and load it from the local cache sdcard. If it is not found, then you have no choice but to use the network to load it from the image server. Back to the topic, what is the reason for avoiding deleting pictures? After deleting a picture, you need to refresh the picture list. At this time, you need to read the picture from sdcard again. This is a problem, because the pixels of mobile phone cameras are very high now, many of them are 4000*2500. You can test that BitmapFactory.decodeFile() takes more than 300ms to load a picture of this size from sdcard. If you add rotation transformation, it will take at least more than 1500ms. You will definitely see the problem of freezing and flickering. Know the cause, how to solve it?
***Step: Create a hashmap to save the bitmap object. Remember to use weak references for the bitmap to prevent OOM caused by excessive loading.
Step 2: Take the bitmap directly from the map. If it is not empty, display it directly. If it is empty, load it from the sdcard.
Step 3: Remember to enable multi-threaded loading. It may seem fast locally, but if there are too many pictures, it will cause ANR and lag, which is a bad user experience.
Step 4: There are two key technical points here. 1. When loading the bitmap, compress the image.
2. Regarding the problem of image rotation after taking photos with Samsung mobile phones, how to rotate the image.
The above is all the code to solve the problem. There are less than 100 lines in total, and the problem is solved. The most important thing is that these less than 100 lines of code replace a picture loading framework of about 700kb, which is the biggest benefit of solving the problem. A common problem among programmers nowadays is that they like to use third-party products for everything. As a result, many companies' projects now introduce a large number of third-party libraries, many of which only use less than one thousandth of the functions. Why bother? Analyze it yourself, and you will find that the problem can often be solved with just a few lines of code. ***A word for all my IT friends, I hope you can encourage each other.
|
<<: How to operate mini program data?
>>: 【Android】I can’t describe this effect
In the past 2016, Samsung was in a quagmire. The ...
The five elements landing system of community ope...
People watch what they want to see, and sometimes...
In the view of Zheng Gang of Zihui Venture Capita...
In the new year, everyone should have a new look....
...
Private domain traffic is very popular, do you al...
Due to the impact of the epidemic, this year'...
During this period, Tik Tok has become popular an...
Bullet Messenger has been very popular recently. ...
This article was reviewed by Tao Ning, PhD, Assoc...
The concept of growth hacking has been very popul...
A journey of a thousand miles begins with a singl...
|||| Compiled by New Media Editor Fang Yongzhen T...
As technology becomes more and more powerful, mob...