Android implements a single thread round robin mechanism to download pictures in batches

Android implements a single thread round robin mechanism to download pictures in batches

When listview adds pictures downloaded from the Internet to an item, if it needs a new thread to download pictures every time it integrates an item, a large number of threads will be generated when listview slides for a long time.

Single thread round robin mechanism can solve this problem

The steps are as follows:

1. A task collection is required

2. Create a thread in the construction method, download pictures from the Internet through the path in the task set, obtain the bitmap, place it in the task, and send it to the handler in the form of message obj for processing.

This worker thread needs to be created only when setting up the adapter for listview, and there is only one

(while (true) the loop will not terminate until the activity calls ondestroy)

This is the httputils tool

3. In the setview method of the custom adapter, place the path and position of the picture download in the task, and set the flag for the ImageView in the item, for the convenience of putting the picture in the item of the listview.

4. Send a message to the main thread and let hanler update the UI

The above is the explanation of downloading pictures in batches using Android. If you have any questions, please leave a message or go to the community of this site for communication and discussion. Everyone makes common progress. Thank you for reading. I hope it can help you. Thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>