我的目标是通过控制更改布局时的时间来创建一种动画。
这就是我的尝试:
try{
Thread.sleep(3000);
}
catch(Exception e){}
setContentView(R.layout.first_screen);
try{
Thread.sleep(1000);
}
catch(Exception e){}
setContentView(R.layout.second_screen);
try{
Thread.sleep(500);
}
catch(Exception e){}
setContentView(R.layout.third_screen);
try{
Thread.sleep(1200);
}
catch(Exception e){}
setContentView(R.layout.forth_screen);
try{
Thread.sleep(800);
}
catch(Exception e){}
setContentView(R.layout.fifth_screen);
try{
Thread.sleep(500);
}
catch(Exception e){}
setContentView(R.layout.sixth_screen);
但是它没有正常工作,因为选择了最后一个布局,而其他布局被跳过了。
答案 0 :(得分:0)
在预定义延迟后执行操作的一种方法是使用Handler
:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//Do stuff here
setContentView(R.layout.first_screen);
}
}, 3000);
run()
中的代码将在延迟3秒后执行。要为每个屏幕重复此操作,请定义此方法:
private void delayedSetContentView(final int viewId, int delay) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
setContentView(viewId);
}
}, delay);
}
然后在onCreate()
:
delayedSetContentView(R.layout.first_screen, 3000);
delayedSetContentView(R.layout.second_screen, 1000);
delayedSetContentView(R.layout.third_screen, 500);
delayedSetContentView(R.layout.fourth_screen, 1200);
delayedSetContentView(R.layout.fifth_screen, 800);
delayedSetContentView(R.layout.sixth_screen, 500);