Fafhrd
Fafhrd

Reputation: 223

How to keep animation running while background thread is active?

I have an application that needs to convert a lot of SVGs to Drawables right at the beginning, using a variant of svg-android.

The problem I've run into is that the conversion is fairly processor intensive, so if I run it on the UI thread, everything pauses for 10 seconds or so while it happens. I've tried moving the conversion to a background thread, but that causes some weirdness because some other methods that are dependent on the array of Drawables fire before all the Drawables are complete. I've tried a couple of different things to do a loading animation, but they won't animate while the background process is active.

Here's my relevant code:

//get and resize SVGs
new Thread(new Runnable(){
    public void run(){
       for (int i=0;i<drawablesArray.length;i++){
            SVG vector = SVGParser.getSVGFromResources(getResources(),resourceArray[i]);
            Drawable drawable = new PictureDrawable(vector.resizePicture(height,width); //resize picture is a custom function in SVG-Android
            drawablesArray[i]=drawable;
        }
    }
}).start();

while(drawablesArray[20]==null){
    loadingView.animate().setDuration(100).rotationBy(25);
}

With that code it will successfully hold off the later methods from firing until the background process completes, but the animation in the while loop will never actually play. I've also tried using an indeterminate ProgressBar for loadingView, both with and without the while loop (altered to not actually do anything), and it will animate up until the background thread starts, at which point it will freeze.

If I take the animation code that's there out of the while loop, the animation will fire after the background thread finishes, not concurrently with the background thread.

How do I keep the animation going while the background thread is running?

Upvotes: 1

Views: 1336

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007658

With that code it will successfully hold off the later methods from firing until the background process completes, but the animation in the while loop will never actually play.

You are tying up the main application thread in your while loop. Your UI will be frozen during this time, just as if you had done the SVG work on the main application thread.

I've also tried using an indeterminate ProgressBar for loadingView, both with and without the while loop (altered to not actually do anything), and it will animate up until the background thread starts, at which point it will freeze.

Presumably, that is because you are tying up the main application thread with your while loop, though I am less certain of that as we do not have this code.

How do I keep the animation going while the background thread is running?

You start by getting rid of the while loop.

Just have a ProgressBar be visible while the background work is going on for now -- later, once you have that working, you can experiment with other solutions. Switch the Thread to an AsyncTask, and in onPostExecute() of the AsyncTask, you hide the ProgressBar and do whatever it is that you needed to do with your converted SVGs.

Upvotes: 2

Related Questions