我使用以下代码创建了一个启动画面:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.splash_layout);
Thread splashThread = new Thread() {
@Override
public void run() {
try {
int waited = 0;
while (_active && (waited < _splashTime)) {
sleep(100);
if (_active) {
waited += 100;
}
}
} catch (InterruptedException e) {
// do nothing
} finally {
_active = false;
finish();
startActivity(new Intent(SplashActivity.this, MyMainActivity.class));
}
}
};
splashThread.start();
}
在splash_layout中有一个图像视图,在启动画面出现一段时间后,然后消失,然后MyMainActivity启动,问题是,在启动消失后,在MyMainActivity启动之前,我可以看到上一个屏幕(与我的应用程序无关) ,例如带有小部件的桌面,或以前运行的应用程序),如何使转换流畅,以便直接将屏幕转到MyMainActivity?
谢谢!
答案 0 :(得分:1)
你能试试吗我不确定这是100%的工作,但尝试可能会有所帮助..
protected int _splashTime = 3000;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_layout);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
finish();
startActivity(new Intent(SplashActivity.this, MyMainActivity.class));
}
}, _splashTime);
}
答案 1 :(得分:1)
尝试在finish()
后调用startActivity()
。
答案 2 :(得分:1)
private static final long SPLASH_SCREEN_MS = 2500;
private long mTimeBeforeDelay;
private Handler mSplashHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
// Create a new Handler.
mSplashHandler = new Handler();
}
@Override
protected void onResume() {
super.onResume();
// The first time mTimeBeforeDelay will be 0.
long gapTime = System.currentTimeMillis() - mTimeBeforeDelay;
if (gapTime > SPLASH_SCREEN_MS) {
gapTime = SPLASH_SCREEN_MS;
}
mSplashHandler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
startActivity(intent);
SplashScreenActivity.this.finish();
}
}, gapTime);
// Save the time before the delay.
mTimeBeforeDelay = System.currentTimeMillis();
}
@Override
protected void onPause() {
super.onPause();
mSplashHandler.removeCallbacksAndMessages(null);
}
答案 3 :(得分:0)
供您参考,以下是Android - Splash Screen example的最佳示例。
答案 4 :(得分:-1)
您可以尝试以下代码:1
public class MainActivity extends Activity {
private ImageView splashImageView;
boolean splashloading = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
splashImageView = new ImageView(this);
splashImageView.setScaleType(ScaleType.FIT_XY);
splashImageView.setImageResource(R.drawable.ic_launcher);
setContentView(splashImageView);
// interesting music
/**
* Gets your sound file from res/raw
*/
splashloading = true;
Handler h = new Handler();
h.postDelayed(new Runnable() {
public void run() {
splashloading = false;
setContentView(R.layout.activity_main);
}
}, 3000);
}
祝你好运!