In my android app, I download an image from the cloud. The download is performed in a thread and in that same thread I also set an image view with the newly downloaded image. After setting the image, I then call postinvalidate().
However the image does not immediately show up. Is there a way to make the redraw happen immediately? I need a way to trigger a drawing cycle.
Each class which is derived from the View class has the
invalidate()and thepostInvalidate()method. Ifinvalidate()gets called it tells the system that the current view has changed and it should be redrawn as soon as possible. As this method can only be called from your UIThread another method is needed for when you are not in the UIThread and still want to notify the system that your View has been changed. ThepostInvalidate()method notifies the system from a non-UIThread and the View gets redrawn in the next eventloop on the UIThread as soon as possible.In your case you can achieve what you want with the help of
AsyncTask(an intelligent backround thread).AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations (download image in background in your case) and publish results (set your bitmap to your ImageView) on the UI thread without having to manipulate threads and/or handlers.An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called begin, doInBackground, processProgress and end, as explained in the docs: