Question

In an android app, I've got a set of thumbnails to download. As the user uses the app, this set of thumbnails could get up to 100 or more items, potentially.

The thumbnails need to be displayed in a gallery and right now, when that gallery is shown, I start one thread to download all thumbnails from 0 to the end of the set. This is bad in a few ways - every image is downloaded even if it isn't required, and if the gallery has the 30th element centered, then the user won't see any image for it until all of the 1st 20 images are downloaded.

Ideally, I would like the images to download and display as required - so if the 30th element were centered when the gallery displayed, then the 29th,30th and 31st elements would be downloaded and displayed, then if the user scrolled left, the 28th, 27th, etc would be downloaded. I'm not sure if this is realistic/efficient.

I've tried starting a thread to download the image in the gallery adapter class (which extends ArrayAdapter), but this obviously starts a new thread for every image as it's scrolled, so the app becomes unusable.

Does anyone have thoughts/advice on how to achieve this?

Cheers, r3mo

Was it helpful?

Solution

You may also want to keep track of which images are currently in view so you can kill threads that have been scrolled past.

For instance:

  1. User opens app, seeing images 1, 2, and 3
  2. You start downloading 1, 2, and 3 with high priority.
  3. You start downloading 4, 5, and 6 with lower priority.
  4. User scrolls to images 23, 24, and 25.
  5. You give it a moment to ensure they've stopped.
  6. You cancel the threads for 1-6 and start downloading 23, 24, and 25 with high priority.
  7. You start downloading 20, 21, and 22 plus 26, 27, and 28 with lower priority.

Depending on what testing shows is a good size for the thread pool, you may even expand the pre-loading out farther. Bonus points for using the velocity instead of a pause to determine when to start downloading something so you'll do it even if they're scrolling slowly. Double bonus points for caching everything you can.

OTHER TIPS

"I've tried starting a thread to download the image in the gallery adapter class (which extends ArrayAdapter), but this obviously starts a new thread for every image as it's scrolled, so the app becomes unusable."

You can use some ThreadPool with fixed number of threads to prevent starting too many threads.

1.) You have ThreadPool with fixed number of threads,. 2.) You add a new Runnable to be executed a.) there is 'resting' thread, it will execute the given Runnable b.) all threads of the pool are 'working', your new Runnable is enqueued in the BlockingQueue of the pool, waiting for execution

You can also just have 1 thread to download the images, and a list of images to download.

The main thread would just add some images to download in the list ( and perhaps remove some if they are no more visible ). The download thread would just donwload the images from the list.

You now have a classic producer / consumer pattern !

Hope it helps,

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top