[英]Android - showing views using Handler Runnable
我正在为自己创建的练习应用程序创建一个简单的启动屏幕。 我有一堆不可见的图像,我想在使用Handler Runnable设置的5秒钟延迟后显示这些图像。 到目前为止,这是我的代码:
long secondsDelayed = 5;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//I want to display this images in 2 second count
ivLogoText.setVisibility(View.VISIBLE);
ivLogoStem.setVisibility(View.VISIBLE);
//I want to display this image in 3 second count
ivSmLeaves.setVisibility(View.VISIBLE);
//I want to display this image in 4 second count
ivMedLeaves.setVisibility(View.VISIBLE);
//I want to display this image in 5 second count
ivLargeLeaves.setVisibility(View.VISIBLE);
startActivity(new Intent(SplashScreen.this, HomeLeftPanel.class));
finish();
}
}, (long) (secondsDelayed * 1000));
}
从我现在的代码中,启动屏幕停留5秒钟,这正是我想要的,但是所有图像也将在5秒内显示出来。 有人可以帮我弄这个吗?
这是因为您要同时更改所有图像的可见性。
与此相反,您应该使用“查看动画师”
http://developer.android.com/reference/android/widget/ViewAnimator.html
当您要在2秒,3秒,4秒,5秒显示不同的图像时,您需要在交错的时间发布多个此类可运行对象。 或者,您可以使用倒数计时器,在其中可以计算滴答声的数量并执行不同的操作。
如果要使用您的方法:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//I want to display this images in 2 second count
ivLogoText.setVisibility(View.VISIBLE);
ivLogoStem.setVisibility(View.VISIBLE);
}
}, (long) (2 * 1000));
new Handler().postDelayed(new Runnable() {
//I want to display this image in 3 second count
ivSmLeaves.setVisibility(View.VISIBLE);
}
}, (long) (3 * 1000));
new Handler().postDelayed(new Runnable() {
//I want to display this image in 4 second count
ivMedLeaves.setVisibility(View.VISIBLE);
}, (long) (4 * 1000));
new Handler().postDelayed(new Runnable() {
//I want to display this image in 5 second count
ivLargeLeaves.setVisibility(View.VISIBLE);
startActivity(new Intent(SplashScreen.this, HomeLeftPanel.class));
finish();
}
}, (long) (5 * 1000));
您可以在线程的Runnable
中使用Thread
和Sleep
。 在Thread
内部,您可以使用Activity的runOnUiThread
函数更改UI(您的图像视图),如下所示:
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000); // wait 2s
runOnUiThread(new Runnable() {
@Override
public void run() {
//show hide image view here.
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
试试这个
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
switch (step) {
case 1:
//I want to display this images in 2 second count
ivLogoText.setVisibility(View.VISIBLE);
ivLogoStem.setVisibility(View.VISIBLE);
break;
case 2:
//I want to display this image in 3 second count
ivSmLeaves.setVisibility(View.VISIBLE);
break;
case 3:
//I want to display this image in 4 second count
ivMedLeaves.setVisibility(View.VISIBLE);
break;
case 4:
//I want to display this image in 5 second count
ivLargeLeaves.setVisibility(View.VISIBLE);
break;
case 5:
startActivity(new Intent(SplashScreen.this, HomeLeftPanel.class));
finish();
return;// there is no more step
case 0:
// TODO: do something afeter 1 second
default:
}
++ step;
// reserve next step
handler.postDelayed(this, 1000);
}
}, 1000);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.