Android solves ListView loading picture flickering

Android solves ListView loading picture flickering

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.


A flashing page appears

***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?

  1. The Bitmap must be thumbnailed.
  2. Memory cache is performed for the loaded files. Without further ado, let's get straight to the code!

***Step: Create a hashmap to save the bitmap object. Remember to use weak references for the bitmap to prevent OOM caused by excessive loading.


Weak references save objects

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.


Determine whether to use sdcard or memory

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.


Multithreaded sdcard loading pictures

Step 4: There are two key technical points here.

1. When loading the bitmap, compress the image.


Compress images

2. Regarding the problem of image rotation after taking photos with Samsung mobile phones, how to rotate the image.


Get the image rotation angle


Rotate an image using a matrix

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.

  • All complex problems should be simplified, and all simple problems should still be simplified.

<<:  How to operate mini program data?

>>:  【Android】I can’t describe this effect

Recommend

Why do people drink ice beer when eating crayfish, but no one drinks ice liquor?

After a week of hard work, how do you plan to tre...

Growth activity promotion matrix for online education!

Online education has developed rapidly in 2020 an...

A chart to understand the gender ratio of Silicon Valley technology companies

Until recently, gender and racial diversity data ...

The process of setting up a Toutiao information flow advertising account!

This article will tell you about the process of s...

8000 words to deconstruct the 618 marketing promotion gameplay

At present, most brands’ 618 is still in the proc...

Massive advertising and account establishment

What do you need to know to interpret and deploy ...

10 Tips for Efficient Android App Development

[[147991]] If you want to create a worst-case sce...

How much do you know about data operations?

Generally speaking, data is the most authentic wa...