0

I've run into an issue with my application. I'm porting a series of internal iPad apps from iOS to android and image sequences are integral to their design. They require the standard fullscreen stop, start, pause functionality all of which is working fine from a subclass of ImageView. But it's only currently displaying 2 out of 300 images it gets through before the locked process dialog keeps coming up.

So is there a "good" way to set an image/background without locking the main thread?

Just FYI AnimateDrawable wasn't ideal for what I needed as it doesn't offer pause/resume functionality and the number of sequences I have are easily in the hundreds and are on average a few hundred frames long.

Goo
  • 1,318
  • 1
  • 13
  • 31
Mytheral
  • 3,929
  • 7
  • 34
  • 57
  • Maybe I don't fully understand the question, but could you run your process on a separate thread and then jump back to the main to update the UI? – Evan Dyson Jul 02 '12 at 19:37
  • The issue seems to be how long it takes to load the images. The timer keeps ticking off as it needs to. The Uri is being resolved in plenty of time but setting the image seems to take ages. In the 300ish image URIs it gets through before crashing it only displays a single image or two. Also ImageViews exist in the UI toolkit and according to the docs we can't touch it outside of the main thread. – Mytheral Jul 02 '12 at 20:19

2 Answers2

1

You can load images asyncronously using AsyncTask

Just put the code related to image loading in doInBackground method and try to set an indicator like a progress bar or a progress dialog so you will start showing the dialog or bar at the begining of loading and after finish dismiss the dialog using handlers in onPostExecute method

Sample code :

 class SomeClass extends Activity {
protected ProgressDialog pd ;
protected Handler handler = new Handler(){
@Override 
public void handleMessage(int what){
   if(pd.isShowing())
      pd.dismiss();
}
     .....

     class LoadImageTask extends AsyncTask<URL, Integer, Long> {
          protected void onPreExecute(Long result) {
            pd = ProgressDialog.show(getContext(),"Title","Message");
         }

         protected Long doInBackground(URL... urls) {

          //Load IMAGEs code
             return totalSize;
         }



         protected void onPostExecute(Long result) {
             //finish loadiing images
              handler .sendEmptyMessage(0);
         }
     }
    }

I hope this would help you

Muhannad A.Alhariri
  • 3,702
  • 4
  • 30
  • 46
0

Couple of things to add to Muhannad's answer:

  • You don't necessarily have to do it in AsyncTask, a thread will do;
  • You might want to render the image into a dedicated bitmap, and then use Canvas.drawBitmap() to render it on onDraw() to save time spent on rendering in UI thread.
vt.
  • 436
  • 6
  • 12