prabu
prabu

Reputation: 727

Unable to cancel animated loading in onPostExecute() in android

I am trying to show some animation as loading using asynctask in android.But im unable to show the animation which im expecting,more over i couldn't able to cancel the animation as well. Can anyone help me to solve this issue.

thanks for your precious time!..

Show_AnimatedLoading.java

ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imageView=(ImageView) findViewById(R.id.imageView1);

    CustomLoad task = new CustomLoad();
    task.execute();
}

 public class CustomLoad extends AsyncTask<Void, Void, Void>
 {

   @Override
   protected void onPreExecute() {
    // TODO Auto-generated method stub

       Show_animation();


   }

   private void Show_animation() {
    // TODO Auto-generated method stub
       Animation a = AnimationUtils.loadAnimation(Show_AnimatedLoading.this, R.anim.progress_anim);
        a.setDuration(2000);
        imageView.startAnimation(a);


        a.setInterpolator(new Interpolator()
        {
            private final int frameCount = 50;

            @Override
            public float getInterpolation(float input)
            {
                return (float)Math.floor(input*frameCount)/frameCount;
            }
        });
}

@Override
   protected Void doInBackground(Void... params) 
   {
    // TODO Auto-generated method stub
    return null;

   }

   @Override
   protected void onPostExecute(Void result) {
    // TODO Auto-generated method stub

       imageView.clearAnimation();

   } 
 }

Progress.xml

<?xml version="1.0" encoding="utf-8"?>
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite" />

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000">

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:contentDescription="@string/app_name"
    android:src="@drawable/transp_load" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:contentDescription="@string/app_name"
    android:src="@drawable/pc_logo_load" /></RelativeLayout>      

Upvotes: 0

Views: 194

Answers (1)

osayilgan
osayilgan

Reputation: 5893

As far as I see, your AsyncTask doesn't have any background operation. So "Post execute" where you clear animation starts right after "pre execute" where you start the animation. It's normal that you don't see anything. Try to do add some background operation.

Try this one out,

final Handler handler = new Handler();

    // TODO
    // Pre execute.

    new Thread(new Runnable() {

        @Override
        public void run() {
            // TODO
            // Background operation.


            handler.postDelayed(new Runnable() {

                @Override
                public void run() {
                    // TODO 
                    // Post Execute.

                }
            }, 3000);
        }
    }).start();

Upvotes: 1

Related Questions