我想在异步任务的前五秒显示一个图像,然后显示一个进度对话框。
是否可以在前五秒钟的图像视图后面显示进度对话框,然后隐藏该图像视图并显示进度对话框?
答案 0 :(得分:1)
通过应用Rajen Raiyarela的建议,我做到了:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_act1);
pb = (ProgressBar) findViewById(R.id.pb);
iv = (ImageView) findViewById(R.id.iv);
TimerTask task = new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
swapVisibility();
}
});
}
};
timer = new Timer();
timer.schedule(task, 5000);
new Task().execute();
}
class Task extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
try {
Thread.sleep(7000); // imitating heavy operation
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
timer.cancel();
if (pb.getVisibility() != View.VISIBLE)
swapVisibility();
}
}
private void swapVisibility() {
pb.setVisibility(View.VISIBLE);
iv.setVisibility(View.GONE);
Log.e("TAG", "Visibility changed");
}