我想为我的程序创建一些简单的动画。 有4个隐形按钮,我想要的是程序启动时, 这些按钮会延迟显示。
显示按钮1 - >按钮2 - >等等。
我试过这个,但是当程序运行时,所有按钮都会同时出现。
try {
((Button) findViewById(R.id.f1)).setVisibility(View.VISIBLE);
Thread.sleep(1200);
((Button) findViewById(R.id.f2)).setVisibility(View.VISIBLE);
Thread.sleep(1200);
((Button) findViewById(R.id.f3)).setVisibility(View.VISIBLE);
Thread.sleep(1200);
((Button) findViewById(R.id.f4)).setVisibility(View.VISIBLE);
Thread.sleep(1200);
} catch (Exception e) {
}
任何人都可以帮助我吗?
答案 0 :(得分:7)
使用处理程序:
private Handler handler;
private void showButtons(){
handler = new Handler();
handler.postDelayed(new Runnable(){
@Override
public void run(){
((Button) findViewById(R.id.f1)).setVisibility(View.VISIBLE);
}
}, 1200);
handler.postDelayed(new Runnable(){
@Override
public void run(){
((Button) findViewById(R.id.f2)).setVisibility(View.VISIBLE);
}
}, 2400);
//etc
}
答案 1 :(得分:0)
这与这个问题无关,但如果你睡觉主线程(就像你做的那样) 它可以使您的应用程序冻结