Janpan
Janpan

Reputation: 2284

android animation freeze

I have an animation with 12 images that displays as a loading image. It works fine on its own, but when I use my httpClient class to get content from the online db, it freezes completely and when the content is done fetching, you can see the animation stopped on a different image.

How can I make that the image load while the content is being fetched from the db ?

Here is some code:

Class members:

ImageView loading;
AnimationDrawable loadAnimation;

onCreate:

    LinearLayout main = (LinearLayout) findViewById(R.id.loading);
    loading = new ImageView(this);
    loading.setImageResource(R.drawable.loading);
    loadAnimation = (AnimationDrawable)loading.getDrawable();
    main.addView(loading);

I use this to start the animation before the content is fetched:

loadAnimation.start();

And this when it is done fetching:

loadAnimation.stop();

Upvotes: 1

Views: 2710

Answers (1)

Oleg Vaskevich
Oleg Vaskevich

Reputation: 12682

The reason this is happening is that you are performing database operations on the main thread, which freezes the UI while your blocking code is executing. To avoid this, you need to perform those operations in a different thread. Look into using AsyncTask to see an example of how to download files asynchronously.

In your case, start the animation in onPreExecute() and stop it in onPostExecute().

Upvotes: 2

Related Questions