android - Synchronous image loading on a background thread with Picasso - without .get() -


i have custom viewgroup (that contains picasso-loaded images) can reused in 2 places:

  1. displayed user in application (on ui thread)
  2. drawn canvas , saved .jpeg (on background thread)

my code drawing canvas looks this:

    int measurespec = view.measurespec.makemeasurespec(width, view.measurespec.exactly);     view.measure(measurespec, measurespec);     bitmap bitmap =         bitmap.createbitmap(view.getmeasuredwidth(), view.getmeasuredheight(), bitmap.config.argb_8888);     canvas canvas = new canvas(bitmap);     view.layout(0, 0, view.getmeasuredwidth(), view.getmeasuredheight());     view.draw(canvas); 

the problem there no time image load before draw view canvas. i'm trying avoid coupling as possible here i'd prefer not add picasso callback because class that's doing drawing doesn't know view it's drawing.

i'm working around issue changing image loading code .get() rather .load() , using imageview.setimagebitmap(). unfortunately, adds ton of complexity view , don't it.

what i'd pass option picasso's requestcreator request should executed synchronously on current thread (and throw exception if it's main thread). wonder if of edge case support built directly picasso? or in api , i'm oblivious it?

here solution:

/**  * loads request imageview.  * if called background thread, request performed synchronously.  * @param requestcreator request creator  * @param imageview target imageview  * @param callback picasso callback  */ public static void into(requestcreator requestcreator, imageview imageview, callback callback) {   boolean mainthread = looper.mylooper() == looper.getmainlooper();   if (mainthread) {     requestcreator.into(imageview, callback);   } else {     try {       bitmap bitmap = requestcreator.get();       imageview.setimagebitmap(bitmap);       if (callback != null) {         callback.onsuccess();       }     } catch (ioexception e) {       if (callback != null) {         callback.onerror();       }     }   } } 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -