geometer/FBReaderJ — pull request #37 by snaury (2012-02-26 14:39:31+00:00)
Title: Fix jerky scrolling in Network Library

For quiet a while I found that scrolling in Network Library is very slow and very jerky, besides LogCat shows that gc is basically working all the time. This is especially noticeable when on LitRes going to Genres -> Fiction -> Science Fiction, which has very long lists, with pretty big covers. I found that this is mainly due to the fact that cover bitmap decoding and resizing is done on the ui thread, and on top of that tree adapter's data set is changed quite often, causing list view to call getView for new Tree objects, re-decoding and rescaling those same covers over and over again. And while this causes bad scrolling on my Galaxy S II, it's absolutely horrible on a tablet, e.g. on Transformer Prime.

This my 3rd or something attempt at moving cover bitmap generation to separate threads, which turned out to be tricky. Basically, this patch adds a recommended practice of using ViewHolder in NetworkLibraryAdapter, adds a LRU map of bitmaps from their tree.getUniqueKey(), offloads cover bitmap generation to a pool of threads (in this patch it's just 1 thread, but maybe it should be increased), and uses Future so that cover generation can be cancelled, when view is reused for another tree. Also, since bitmap generation is now on different threads, getBitmap in ZLAndroidImageData is made synchronized.

It appears to work really well on Galaxy S II, however on Transformer Prime I still had noticeable UI freezes for several seconds sometimes (which then stabilized and doesn't seem to happen as bad restarts now). I'm not sure if this is because I screwed something up with too much locking, or maybe cover generation shouldn't really use minimum priority?

Anyway, with this patch scrolling is very smooth most of the time, with stock bitmaps showing up, which are shortly replaced by proper covers. Of course this needs more testing, but I hope you can make this into something useable.