capacitor-splash-screen
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSplash Screen in Capacitor
Capacitor中的启动屏
Configure and customize splash screens for iOS and Android.
为iOS和Android配置并自定义启动屏。
When to Use This Skill
适用场景
- User wants to customize splash screen
- User needs splash screen assets
- User wants animated splash
- User has splash screen issues
- 用户想要自定义启动屏
- 用户需要启动屏资源
- 用户想要添加动画启动屏
- 用户遇到启动屏相关问题
Quick Start
快速开始
Install Plugin
安装插件
bash
bun add @capacitor/splash-screen
bunx cap syncbash
bun add @capacitor/splash-screen
bunx cap syncBasic Configuration
基础配置
typescript
// capacitor.config.ts
import type { CapacitorConfig } from '@capacitor/cli';
const config: CapacitorConfig = {
plugins: {
SplashScreen: {
launchShowDuration: 2000,
launchAutoHide: true,
backgroundColor: '#ffffff',
androidSplashResourceName: 'splash',
androidScaleType: 'CENTER_CROP',
showSpinner: false,
splashFullScreen: true,
splashImmersive: true,
},
},
};typescript
// capacitor.config.ts
import type { CapacitorConfig } from '@capacitor/cli';
const config: CapacitorConfig = {
plugins: {
SplashScreen: {
launchShowDuration: 2000,
launchAutoHide: true,
backgroundColor: '#ffffff',
androidSplashResourceName: 'splash',
androidScaleType: 'CENTER_CROP',
showSpinner: false,
splashFullScreen: true,
splashImmersive: true,
},
},
};Programmatic Control
程序化控制
typescript
import { SplashScreen } from '@capacitor/splash-screen';
// Hide after app is ready
async function initApp() {
// Initialize your app
await loadUserData();
await setupServices();
// Hide splash screen
await SplashScreen.hide();
}
// Show splash (useful for app refresh)
await SplashScreen.show({
autoHide: false,
});
// Hide with animation
await SplashScreen.hide({
fadeOutDuration: 500,
});typescript
import { SplashScreen } from '@capacitor/splash-screen';
// 应用就绪后隐藏
async function initApp() {
// Initialize your app
await loadUserData();
await setupServices();
// Hide splash screen
await SplashScreen.hide();
}
// 显示启动屏(适用于应用刷新)
await SplashScreen.show({
autoHide: false,
});
// 带动画隐藏
await SplashScreen.hide({
fadeOutDuration: 500,
});Generate Assets
生成资源
Using Capacitor Assets
使用Capacitor Assets
bash
bun add -D @capacitor/assetsbash
bun add -D @capacitor/assetsPlace source images in resources/
将源图片放置在resources/目录下
resources/splash.png (2732x2732 recommended)
resources/splash.png(推荐尺寸2732x2732)
resources/splash-dark.png (optional)
resources/splash-dark.png(可选)
bunx capacitor-assets generate
undefinedbunx capacitor-assets generate
undefinediOS Sizes
iOS尺寸
| Size | Usage |
|---|---|
| 2732x2732 | iPad Pro 12.9" |
| 2048x2732 | iPad Pro portrait |
| 2732x2048 | iPad Pro landscape |
| 1668x2388 | iPad Pro 11" |
| 1536x2048 | iPad |
| 1242x2688 | iPhone XS Max |
| 828x1792 | iPhone XR |
| 1125x2436 | iPhone X/XS |
| 1242x2208 | iPhone Plus |
| 750x1334 | iPhone 8 |
| 640x1136 | iPhone SE |
| 尺寸 | 用途 |
|---|---|
| 2732x2732 | iPad Pro 12.9" |
| 2048x2732 | iPad Pro 竖屏 |
| 2732x2048 | iPad Pro 横屏 |
| 1668x2388 | iPad Pro 11" |
| 1536x2048 | iPad |
| 1242x2688 | iPhone XS Max |
| 828x1792 | iPhone XR |
| 1125x2436 | iPhone X/XS |
| 1242x2208 | iPhone Plus |
| 750x1334 | iPhone 8 |
| 640x1136 | iPhone SE |
Android Sizes
Android尺寸
| Density | Size |
|---|---|
| mdpi | 320x480 |
| hdpi | 480x800 |
| xhdpi | 720x1280 |
| xxhdpi | 960x1600 |
| xxxhdpi | 1280x1920 |
| 密度 | 尺寸 |
|---|---|
| mdpi | 320x480 |
| hdpi | 480x800 |
| xhdpi | 720x1280 |
| xxhdpi | 960x1600 |
| xxxhdpi | 1280x1920 |
iOS Storyboard
iOS Storyboard配置
xml
<!-- ios/App/App/Base.lproj/LaunchScreen.storyboard -->
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0">
<scenes>
<scene sceneID="1">
<objects>
<viewController id="2" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="3">
<rect key="frame" x="0" y="0" width="414" height="896"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<subviews>
<imageView
contentMode="scaleAspectFit"
image="splash"
translatesAutoresizingMaskIntoConstraints="NO"
id="4">
</imageView>
</subviews>
<constraints>
<constraint firstItem="4" firstAttribute="centerX" secondItem="3" secondAttribute="centerX" id="5"/>
<constraint firstItem="4" firstAttribute="centerY" secondItem="3" secondAttribute="centerY" id="6"/>
</constraints>
</view>
</viewController>
</objects>
</scene>
</scenes>
</document>xml
<!-- ios/App/App/Base.lproj/LaunchScreen.storyboard -->
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0">
<scenes>
<scene sceneID="1">
<objects>
<viewController id="2" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="3">
<rect key="frame" x="0" y="0" width="414" height="896"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<subviews>
<imageView
contentMode="scaleAspectFit"
image="splash"
translatesAutoresizingMaskIntoConstraints="NO"
id="4">
</imageView>
</subviews>
<constraints>
<constraint firstItem="4" firstAttribute="centerX" secondItem="3" secondAttribute="centerX" id="5"/>
<constraint firstItem="4" firstAttribute="centerY" secondItem="3" secondAttribute="centerY" id="6"/>
</constraints>
</view>
</viewController>
</objects>
</scene>
</scenes>
</document>Android Configuration
Android配置
XML Splash Screen (Android 11+)
XML启动屏(Android 11+)
xml
<!-- android/app/src/main/res/values/styles.xml -->
<resources>
<style name="AppTheme.NoActionBarLaunch" parent="Theme.SplashScreen">
<item name="windowSplashScreenBackground">@color/splash_background</item>
<item name="windowSplashScreenAnimatedIcon">@drawable/splash</item>
<item name="windowSplashScreenAnimationDuration">1000</item>
<item name="postSplashScreenTheme">@style/AppTheme.NoActionBar</item>
</style>
</resources>xml
<!-- android/app/src/main/res/values/styles.xml -->
<resources>
<style name="AppTheme.NoActionBarLaunch" parent="Theme.SplashScreen">
<item name="windowSplashScreenBackground">@color/splash_background</item>
<item name="windowSplashScreenAnimatedIcon">@drawable/splash</item>
<item name="windowSplashScreenAnimationDuration">1000</item>
<item name="postSplashScreenTheme">@style/AppTheme.NoActionBar</item>
</style>
</resources>Colors
颜色配置
xml
<!-- android/app/src/main/res/values/colors.xml -->
<resources>
<color name="splash_background">#FFFFFF</color>
</resources>
<!-- android/app/src/main/res/values-night/colors.xml -->
<resources>
<color name="splash_background">#121212</color>
</resources>xml
<!-- android/app/src/main/res/values/colors.xml -->
<resources>
<color name="splash_background">#FFFFFF</color>
</resources>
<!-- android/app/src/main/res/values-night/colors.xml -->
<resources>
<color name="splash_background">#121212</color>
</resources>Dark Mode Support
深色模式支持
typescript
// capacitor.config.ts
plugins: {
SplashScreen: {
launchAutoHide: false, // Control manually
backgroundColor: '#ffffff',
// iOS will use LaunchScreen.storyboard variations
// Android uses values-night/colors.xml
},
},typescript
// Detect dark mode and configure
import { SplashScreen } from '@capacitor/splash-screen';
const isDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
// Show appropriate themed content
await SplashScreen.hide({
fadeOutDuration: 300,
});typescript
// capacitor.config.ts
plugins: {
SplashScreen: {
launchAutoHide: false, // Control manually
backgroundColor: '#ffffff',
// iOS will use LaunchScreen.storyboard variations
// Android uses values-night/colors.xml
},
},typescript
// Detect dark mode and configure
import { SplashScreen } from '@capacitor/splash-screen';
const isDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
// Show appropriate themed content
await SplashScreen.hide({
fadeOutDuration: 300,
});Animated Splash
动画启动屏
Lottie Animation
Lottie动画
typescript
import { SplashScreen } from '@capacitor/splash-screen';
async function showAnimatedSplash() {
// Keep native splash while loading
await SplashScreen.show({ autoHide: false });
// Load Lottie animation in web
const lottie = await import('lottie-web');
// Show web-based animated splash
document.getElementById('splash-animation').style.display = 'block';
const animation = lottie.loadAnimation({
container: document.getElementById('splash-animation'),
path: '/animations/splash.json',
loop: false,
});
animation.addEventListener('complete', async () => {
// Hide native splash
await SplashScreen.hide({ fadeOutDuration: 0 });
// Hide web splash
document.getElementById('splash-animation').style.display = 'none';
});
}typescript
import { SplashScreen } from '@capacitor/splash-screen';
async function showAnimatedSplash() {
// Keep native splash while loading
await SplashScreen.show({ autoHide: false });
// Load Lottie animation in web
const lottie = await import('lottie-web');
// Show web-based animated splash
document.getElementById('splash-animation').style.display = 'block';
const animation = lottie.loadAnimation({
container: document.getElementById('splash-animation'),
path: '/animations/splash.json',
loop: false,
});
animation.addEventListener('complete', async () => {
// Hide native splash
await SplashScreen.hide({ fadeOutDuration: 0 });
// Hide web splash
document.getElementById('splash-animation').style.display = 'none';
});
}Best Practices
最佳实践
- Keep it fast - Under 2 seconds total
- Match branding - Use consistent colors/logo
- Support dark mode - Provide dark variants
- Don't block - Load essentials only
- Progressive reveal - Fade out smoothly
- 保持快速 - 总时长控制在2秒以内
- 匹配品牌 - 使用一致的颜色/标志
- 支持深色模式 - 提供深色变体
- 不要阻塞 - 仅加载必要资源
- 渐进式显示 - 平滑淡出
Troubleshooting
故障排除
| Issue | Solution |
|---|---|
| White flash | Match splash background to app |
| Stretching | Use correct asset sizes |
| Not hiding | Call |
| Dark mode wrong | Add values-night resources |
| 问题 | 解决方案 |
|---|---|
| 白屏闪烁 | 使启动屏背景与应用背景匹配 |
| 图片拉伸 | 使用正确的资源尺寸 |
| 启动屏不隐藏 | 手动调用 |
| 深色模式异常 | 添加values-night资源 |
Resources
参考资源
- Capacitor Splash Screen: https://capacitorjs.com/docs/apis/splash-screen
- Capacitor Assets: https://github.com/ionic-team/capacitor-assets
- Android Splash Screens: https://developer.android.com/develop/ui/views/launch/splash-screen
- Capacitor Splash Screen: https://capacitorjs.com/docs/apis/splash-screen
- Capacitor Assets: https://github.com/ionic-team/capacitor-assets
- Android Splash Screens: https://developer.android.com/develop/ui/views/launch/splash-screen