我想让我的闪屏更加流畅。我读了这个link后,我制作了闪屏。但是,当我完成SplashActivity并且意图使用MainActivity时,我觉得SplashActivity太早完成,我在MainActivity show之前看到了我的设备的屏幕保护程序(我估计延迟为0.5秒)。我想要的是在SplashActivity完成之前准备好的MainActivity。
我尝试了在我的Intent中设置了标志FLAG_ACTIVITY_NEW_TASK和FLAG_ACTIVITY_CLEAR_TASK的解决方案,但它并不顺利。
以下是两个活动之间转换的代码:
private void transitionActivity() {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
和清单文件:
<activity
android:name=".SplashActivity"
android:launchMode="singleTask"
android:theme="@style/SplashScreenTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:launchMode="singleTask"
android:theme="@style/AppThemeNoActionBar" />
也许关键是他想在启动画面中延迟
如果是这样你可以使用Handler()
int SplashDuration = 2000;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(splashScreen.this, MainActivity.class);
startActivity(i);
finish();
}
}, SplashDuration);
如果你仍然要快或慢,你可以改变“SplashDuration”的时间
你可以使用https://developer.android.com/reference/android/os/AsyncTask.html来做到这一点。在启动画面时制作逻辑直到5秒,在4秒或更低时制作以调用您的主要活动。
首先,我要感谢所有回答我的人以及对我的问题发表评论。 @MOF(answer link)有一个答案,它没有解决问题,但它帮助我思考一种新的方式,这种新方式帮助我的问题不复存在。我的方式是:
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
finish();
}
}, getResources().getInteger(android.R.integer.config_mediumAnimTime));
谢谢大家!