如果您使用纯色作为初始屏幕的背景,其他帖子中建议的解决方案可能对您有用,但如果您使用复杂图像(如渐变),则需要了解以下内容:
如果您想要在您的启动画面和您的应用程序之间无缝过渡,您需要安装@capacitor/splash-screen,因为 Android 的<item name="android:background">@drawable/splash</item> 不允许您淡出启动画面,而且当 Android 启动画面被您的Ionic App,在呈现 WebView 时,您将体验到短暂的空白屏幕。
@capacitor/splash-screen 允许您通过选择自己何时应该隐藏初始屏幕以及淡出应该花费多长时间来缓解这种情况。
@capacitor/splash-screen 不会替换原生 Android 启动屏幕 <item name="android:background">@drawable/splash</item>,而是会在 Ionic 应用打开后(在原生启动屏幕之后)创建一个 Android ImageView,然后淡出到 WebView。
您可以通过将<item name="android:background">@null</item> 设置为原生启动屏幕来隐藏原生Android 启动屏幕以仅使用@capacitor/splash-screen 之一,但这被认为是一种不好的做法,因为它会产生闲逛几个人的错觉每次启动应用的时刻(创建 Ionic 应用并在屏幕上显示所需的时间)。
最后,如果您希望原生 Android 启动画面覆盖整个屏幕并保持其纵横比,这根本是不可能的(至少对于 Android 11 及更早版本),您只能在后面加上 ImageView该应用程序已启动。
所以...您可以采取以下措施来缓解这种情况:
首先,确保原生 Android 启动画面的配置和@capacitor/splash-screen 创建的启动画面相同,这样当从第一个到第二个时,您不会得到“调整大小”。
然后,您必须将初始屏幕分成 2 层,一层用于背景(可以拉伸以填充视口而不保持其纵横比),另一层用于您的徽标(或其他应该是居中并保持其纵横比)。
然后,为您的初始屏幕创建一个自定义可绘制对象(即 drawable/launch_splash.xml),这将允许您创建一个包含任意多个图层的初始屏幕(在我们的示例 2 中,一个用于背景,一个用于用于徽标)。
最后,使用此自定义可绘制对象代替原始启动屏幕。
这是我所做的完整示例:
capacitor.config.ts
const config: CapacitorConfig = {
// ...
plugins: {
// ...
SplashScreen: {
launchAutoHide: false,
androidSplashResourceName: 'launch_splash',
},
},
};
(确保在对capacitor.config.ts 进行任何更改后重新构建您的应用程序,或自行将更改报告到capacitor.config.json 文件)。
android/app/src/main/assets/capacitor.config.json
{
// ...
"plugins": {
// ...
"SplashScreen": {
"launchAutoHide": false,
"androidSplashResourceName": "launch_splash"
}
}
}
android/app/src/main/res/values/styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:background">@null</item>
</style>
<style name="AppTheme.NoActionBarLaunch" parent="AppTheme.NoActionBar">
<item name="android:background">@drawable/launch_splash</item> <!-- launch_splash -->
</style>
</resources>
android/app/src/main/res/drawable/launch_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- You can add as many layer as you want as long as they are drawable bitmaps or vectors -->
<item android:drawable="@drawable/ic_launcher_background"/> <!-- will stretch to exactly match the viewport size -->
<item android:drawable="@drawable/ic_launcher_foreground" android:gravity="center"/> <!-- will be centered in the viewport without stretching -->
</layer-list>
src/app/tabs/tabs.page.ts
export class TabsPage implements AfterViewInit {
// ...
public ngAfterViewInit(): void {
// Do this in your app landing page
// You may adjust the timing and wait for any promises or data required by your app,
// so its fully ready before hiding the splash screen
// I just added a 100ms delay to help the transition be smooth as it can be quite laggy when your app just finished being rendered
setTimeout(() => {
SplashScreen.hide({fadeOutDuration: 300});
}, 100);
}
}