进度对话框停止UI线程

时间:2014-07-01 07:48:19

标签: android android-asynctask android-animation

我正在展示一些动画并同时发出网络请求。两者都独立工作。但是当我尝试在asynctask中放置进度对话框时,直到进度对话框才开始动画,直到 onPostExecute()。我的猜测是,因为动画和progressdialog都在 UI线程上运行,所以一次只能运行一个。有没有办法在UI线程上同时显示进度对话框和运行动画?

这是我的代码:

public class DailyTarrotActivity extends FragmentActivity {
ImageView imageViewAnimation;
AnimationDrawable spinAnimation;
AnimatorSet set = new AnimatorSet();
FlipImageView imageViewCard;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_daily_tarrot);
    imageViewAnimation = (ImageView) findViewById(R.id.imageViewAnimation);
    imageViewAnimation.setBackgroundResource(R.drawable.spin_animation);
    imageViewCard = (FlipImageView) findViewById(R.id.imageViewCard);
    spinAnimation = (AnimationDrawable) imageViewAnimation.getBackground();
    new DailyTarrotAsyncTask().execute();
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    // TODO Auto-generated method stub
    super.onWindowFocusChanged(hasFocus);
    Log.d("start", "focuschange");
    if (hasFocus) {
        spinAnimation.start();
    }
}
public class DailyTarrotAsyncTask extends AsyncTask<Void, Void, Void> {
    ProgressDialog pd;

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        pd = new ProgressDialog(DailyTarrotActivity.this);
        pd.setCancelable(false);
        pd.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        Thread thread = new Thread();
        try {//just to mimic downloading behaviour
            thread.sleep(10000);//animation starts only after 10 secs
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        pd.dismiss();
        Toast.makeText(DailyTarrotActivity.this, "async task finished",
                Toast.LENGTH_SHORT).show();
    }
}
}

0 个答案:

没有答案