Android的启动画面,同时加载MainActivity [英] Android splash screen while loading MainActivity
问题描述
所以,我刚读了这个问题:我如何使机器人启动画面 而不是添加一个固定的延时(如在顶部的答案),但是,我想保持在开机画面上,而MainActivity(带MapFragment)负荷。
So, I just read this question: How do I make a splash screen in android But instead of adding a fixed delay (like in the top answer), I wanted to keep the splash screen on while the MainActivity (with a MapFragment) loads.
public class SplashScreen extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
synchronized (this) {
try {
wait(3000);
System.out.println("Thread waited for 3 seconds");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
try {
t.start();
t.join();
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
我添加的等待(3000)行,因为我在这之前该线程没有活早就注意到。但是,如果我让等待更长的时间,但只是一个黑色的屏幕,持续时间更长。出于某种原因,的闪屏的活动将不会显示ImageView的。 我该怎么办? 谢谢你。
I added the wait(3000) line, because I noticed before that the thread didn't live for long. However, if I make it wait for longer, there's just a black screen that lasts longer. For some reason, the SplashScreen activity won't show the ImageView. What should I do? Thanks.
推荐答案
做这样的启动画面:
while(counter < 1300) {
try {
Thread.sleep(1000);
}catch(InterruptedException e) {
e.printStackTrace();
}
counter+=100;
}
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
希望它能帮助!
Hope it helps!
编辑: Anotehr方法,使飞溅会是这样的:
Anotehr way to make a splash would be like this:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
Intent intent = new Intent(getApplicationContext(), YourNextActivity.class);
startActivity(intent);
}
},3000); -> the splash will show for 3000 ms, you can reduce this.
这篇关于Android的启动画面,同时加载MainActivity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!