Saturday, 15 June 2013

How to determine android View rendered with asychronous image loading -



How to determine android View rendered with asychronous image loading -

edit 03/12/14: posted few weeks no activity. question clear enough? or tl;dr? unfortunately since i'm new fellow member can't offer bounty :-(

my question can summarised as:

how can determine when view contains asynchronously loaded elements (such web images) has loaded , rendered without resorting bit of hack - see below. is there deterministic way find out when view has finished loading without using longish, defensive delay value view.postdelayed()?

please read on background , specifics of question if think can help.

background of question

i'm making utilize of 3rd party library within app effort following:

third party library asynchronously loads info via rest api library processes json info , uses picasso api download image urls contained within json response , load them customised view (library view, not mine) display in activity. note: picasso calls encapsulated library using , 3rd party library opted not utilize callbacks provided picasso indicates when images have loaded. simply have access callback when underlying json request has returned successfully. once custom view has loaded , rendered, want save view storage png.

my problem occurs @ step 3: unable deterministically observe when custom view has finished loading can save png after view has been rendered. picasso, design, loads images asynchronously android view objects and, think result, methods have attempted below have not worked:

use globallayoutlistener determine when view has finished loading suggested here.

class="lang-java prettyprint-override">myview.getviewtreeobserver().addongloballayoutlistener(new ongloballayoutlistener() { @override public void ongloballayout() { // seek begin saving png here savepng(); }); this didn't work ongloballayout() fires unpredictable number of times before view rendered depending on how images beingness loaded in background. i have used view.postdelayed(runnable, delay) in ongloballayout() but, again, it's not possible know how long delay should due device powerfulness , network bandwidth plus introduces race status of sorts.

many answers similar questions on have suggested posting savepng() runnable on ui message queue. think works scenarios not when images beingness asynchronously loaded views on internet. time approach leads attempting save image before view loaded , rendered. of course, can postdelayed leads same uncertainty on delay length , race conditions in previous point.

i have attempted wait messagequeue.idlehandler.queueidle() event savepng() sadly occurs before resources have been loaded , rendered.

maintain lastly time ongloballayout called , have thread periodically check "no updates in xxx ms" determine view has settled down. not great solution , create app appear unnecessarily slow @ to the lowest degree work.

i tried sub-classing view , overriding dispatchdraw() (suggested here) , calling application - same problem ongloballayout - fires multiples times before view loaded , rendered.

wow, long post means won't silly if there's obvious "gotcha" has been staring me in face time :-)

create subclass of custom view overrides setimagebitmap() , setimagedrawable() methods. can create own listener interface , registration api in subclass. when picasso sets image can phone call registered listener(s) bitmap or drawable. don't forget phone call through superclass image applied custom view.

android android-layout android-activity android-view picasso

No comments:

Post a Comment