了解如何向 Expo 项目添加启动画面和应用图标。
启动画面和应用图标是移动应用的基本元素。它们在用户体验和应用品牌推广中发挥着重要作用。本指南提供了如何创建它们并将其添加到你的应用的步骤。
¥A splash screen and an app icon are fundamental elements of a mobile app. They play an important role in the user experience and branding of the app. This guide provides steps on how to create and add them to your app.
¥Splash screen
启动屏幕,也称为启动屏幕,是用户打开你的应用时看到的第一个屏幕。当应用加载时它保持可见。你还可以使用原生 启动画面 API 来控制启动屏幕消失时的行为。
¥A splash screen, also known as a launch screen, is the first screen a user sees when they open your app. It stays visible while the app is loading. You can also control the behavior of when a splash screen disappears by using the native SplashScreen API.
Expo 项目中的默认启动画面是空白屏幕。可以使用项目的 应用配置 中的 splash
属性按照以下步骤进行自定义。
¥The default splash screen in an Expo project is a blank white screen. It can be customized using the splash
property in the project's app config using the steps below.
1
¥Create a splash screen
要创建启动图片,你可以使用此 Figma 模板。它为 Android 和 iOS 的图标和启动图片提供了最低限度的设计。
¥To create a splash image, you can use this Figma template. It provides a bare minimum design for an icon and splash images for Android and iOS.
有关详细演练,请观看下面的视频:
¥For an in-detail walkthrough, see the video below:
¥Android
Android 屏幕尺寸因设备种类繁多而差异很大。有关如何为任何屏幕找到正确设备指标的更多信息,请参阅 Material Design 博客文章。
¥Android screen sizes vary greatly with the massive variety of devices. See Material Design blog post for more information on how to find the right device metrics for any screen.
¥iOS
Expo 的 Splash Screen API 将根据设备屏幕的大小调整应用图片的大小。你可以使用 splash.resizeMode
指定用于调整图片大小的策略。有关 iOS 人机界面指南中的 设备屏幕和尺寸规范,了解最新的屏幕尺寸列表。
¥Expo's Splash Screen API will resize the image for your app depending on the size of the device's screen. You can specify the strategy used to resize the image with splash.resizeMode
. See the Device screens and sizes specifications from the iOS Human Interface Guidelines for an up-to-date list of screen sizes.
2
¥Export the splash image as a .png
创建启动画面后,将其导出为 .png 并保存在 assets/images 目录中。默认情况下,Expo 使用 splash.png 作为文件名。如果你决定更改启动画面文件的名称,请确保在下一步中使用该名称。
¥After creating a splash screen, export it as a .png and save it in the assets/images directory. By default, Expo uses splash.png as the file name. If you decide to change the name of your splash screen file, make sure to use that in the next step.
注意:目前,仅支持将 .png 图片用作 Expo 项目中的启动画面。如果你使用其他图片格式,则应用的生产版本将会失败。
¥Note: Currently, only .png images are supported to use as a splash screen in an Expo project. If you use another image format, making a production build of your app will fail.
3
¥Add the splash screen in app config
打开应用配置并添加本地路径作为 splash.image
属性的值,以指向你的新启动图片。
¥Open the app config and add the local path as the value of splash.image
property to point to your new splash image.
{
"splash": {
"image": "./assets/images/splash.png"
}
}
你可以通过使用 npx expo start
启动开发服务器并使用开发版本或 Expo Go 进行预览来测试新的启动画面。
¥You can test your new splash image by starting the development server with npx expo start
and using a development build or Expo Go to preview it.
注意:在开发模式下,当你测试新的启动画面时,你会注意到屏幕底部有一个信息栏。它显示有关在准备应用时在设备上下载了多少 JavaScript 代码的信息。它不会出现在生产应用中。
¥Note: In development mode, when you test your new splash screen, you will notice an information bar at the bottom of the screen. It displays the information about how much of the JavaScript code is downloaded on the device when preparing your app. It doesn't appear in production apps.
4
¥Change the background color
如果你为初始图片设置白色以外的背景颜色,你可能会在其周围看到白色边框。这是由于 splash.backgroundColor
属性的默认值为 #ffffff
。
¥If you set a background color other than white for your splash image, you may see a white border around it. This is due to the splash.backgroundColor
property that has a default value of #ffffff
.
你可以更新此属性的值以匹配启动画面的背景颜色。例如:
¥You can update this property's value to match your splash image's background color. For example:
{
"splash": {
"image": "./assets/images/splash.png",
"backgroundColor": "#FEF9B0"
}
}
5
¥Resize a splash screen (optional)
你提供的任何启动图片都会调整大小以保持其纵横比并适合用户设备的解析。
¥Any splash image you provide gets resized to maintain its aspect ratio and fit the resolution of the user's device.
你可以使用两个属性来调整大小:contain
(默认)和 cover
。这些属性的工作方式类似于 React Native <Image>
中的 resizeMode
,如下所示:
¥You can use two properties for resizing: contain
(default) and cover
. These properties work similar to the resizeMode
in React Native <Image>
, as demonstrated below:
将其应用于示例:
¥Applying this to an example:
{
"splash": {
"image": "./assets/images/splash.png",
"resizeMode": "cover"
"backgroundColor": "#FFFFFF"
}
}
在上面的示例中,图片被拉伸以填充整个宽度,同时保持纵横比。这就是为什么当 resizeMode
设置为 contain
时,启动图片上的徽标会变大的原因。
¥In the above example, the image is stretched to fill the entire width while maintaining the aspect ratio. This is why the logo on the splash image is larger when resizeMode
is set to contain
.
splash
properties separately for Android and iOS你可以使用 splash.android
和 splash.ios
为原生平台配置任何 splash
属性以使用特定于平台的选项:
¥You can configure any splash
properties for a native platform using splash.android
and splash.ios
to use a platform-specific option:
在 Android 上,你可以设置 不同设备 DPI 从 mdpi
到 xxxhdpi
的启动图片。
¥On Android, you can set splash images for different device DPIs from mdpi
to xxxhdpi
.
在 iOS 上,你可以将 ios.splash.tabletImage
设置为在 iPad 上具有不同的启动图片。
¥On iOS, you can set ios.splash.tabletImage
to have a different splash image on iPads.
如果你的应用不使用 Expo 预建(以前称为托管工作流)来生成原生 android 和 iOS 目录,则应用配置中的更改将不起作用。欲了解更多信息,请参阅 如何手动自定义配置。
¥If your app does not use Expo Prebuild (formerly the managed workflow) to generate the native android and iOS directories, then changes in the app config will have no effect. For more information, see how you can customize the configuration manually.
在 Android 上,启动画面在大多数情况下与 iOS 上的行为相同。但是,有一个细微的差别。在这种情况下,应特别注意 app.json 中的 android.splash
部分配置。
¥On Android, the splash screen behaves in most cases the same as on iOS. However, there is a slight difference. In this scenario, extra attention should be paid to android.splash
section configuration inside app.json.
根据 resizeMode
,你将在 Android 上看到以下行为:
¥Depending on the resizeMode
you will get the following behavior on Android:
包含:初始屏幕 API 无法拉伸或缩放初始图片。因此,contain
模式最初将仅显示背景颜色,当安装初始视图层次结构时,将显示 splash.image
。
¥contain: The splash screen API is unable to stretch or scale the splash image. As a result, the contain
mode will initially display only the background color, and when the initial view hierarchy is mounted then splash.image
will be displayed.
封面:此模式具有与 contain 相同的限制。
¥cover: This mode has the same limitations as contain.
原生:在这种模式下,你的应用将利用 Android 在应用启动时渲染静态位图的能力。Android(与 iOS 不同)不支持拉伸提供的图片,因此应用将在屏幕上居中显示给定的图片。默认情况下,splash.image
将用作 xxxdpi
资源。你可以自行决定提供满足你的期望并适合屏幕尺寸的图形。为此,请对 不同设备 DPI 使用不同的解析,例如从 mdpi
到 xxxhdpi
。
¥native: In this mode, your app will be leveraging Android's ability to present a static bitmap while the app is starting up. Android (unlike iOS) does not support stretching the provided image, so the app will present the given image centered on the screen. By default splash.image
will be used as the xxxdpi
resource. It's up to you to provide graphics that meet your expectations and fit the screen dimension. To achieve this, use different resolutions for different device DPIs such as from mdpi
to xxxhdpi
.
在 iOS 开发构建中,启动画面有时会在构建之间保持缓存,这使得测试新图片变得更加困难。Apple 建议在重建之前清除派生数据文件夹,这可以使用 Expo CLI 通过运行以下命令来完成:
¥In iOS development builds, launch screens can sometimes remain cached between builds, making it harder to test new images. Apple recommends clearing the derived data folder before rebuilding, this can be done with Expo CLI by running:
-
npx expo run:ios --no-build-cache
请参阅 Apple 测试启动屏幕指南 了解更多信息。
¥See Apple's guide on testing launch screens for more information.
¥App icon
应用的图标是应用用户在其设备的主屏幕和应用商店中看到的图标。Android 和 iOS 有不同且严格的要求。
¥An app's icon is what your app users see on their device's home screen and app stores. Android and iOS have different and strict requirements.
1
¥Create an app icon
要创建应用图标,你可以使用此 Figma 模板。它为 Android 和 iOS 的图标和启动图片提供了最低限度的设计。
¥To create an app icon, you can use this Figma template. It provides a bare minimum design for an icon and splash images for Android and iOS.
有关详细演练,请观看下面的视频:
¥For an in-detail walkthrough, see the video below:
2
¥Export the icon image as a .png
创建应用图标后,将其导出为 .png 并保存在 assets/images 目录中。默认情况下,Expo 使用 icon.png 作为文件名。如果你决定使用它的文件名,请确保在下一步中使用该名称。
¥After creating an app icon, export it as .png and save it in the assets/images directory. By default, Expo uses icon.png as the file name. If you decide to use its file name, make sure to use that in the next step.
3
¥Add the icon in app config
打开应用配置并添加本地路径作为 icon
属性的值,以将其指向你的新应用图标:
¥Open the app config and add the local path as the value of icon
property to point it to your new app icon:
{
"icon": "./assets/images/icon.png"
}
¥Android
可以使用 android.adaptiveIcon
属性进一步自定义 Android 图标,该属性将覆盖前面提到的两个设置。
¥Further customization of the Android icon is possible using the android.adaptiveIcon
property, which will override both of the previously mentioned settings.
Android 自适应图标由两个独立的层组成 - 前景图片和背景颜色或图片。这允许操作系统将图标屏蔽成不同的形状,并且还支持视觉效果。对于 Android 13 及更高版本,操作系统支持主题应用图标,该图标使用壁纸和主题来确定设备主题设置的颜色。
¥The Android Adaptive Icon is formed from two separate layers — a foreground image and a background color or image. This allows the OS to mask the icon into different shapes and also supports visual effects. For Android 13 and later, the OS supports a themed app icon that uses a wallpaper and theme to determine the color set by the device's theme.
你提供的设计应遵循启动器图标的 Android 自适应图标指南。你还应该:
¥The design you provide should follow the Android Adaptive Icon Guidelines for launcher icons. You should also:
使用 .png 文件。
¥Use .png files.
使用 android.adaptiveIcon.foregroundImage
属性指定前景图片的路径。
¥Use the android.adaptiveIcon.foregroundImage
property to specify the path to your foreground image.
使用 android.adaptiveIcon.monochromeImage
属性指定单色图片的路径。
¥Use the android.adaptiveIcon.monochromeImage
property to specify the path to your monochrome image.
默认背景颜色为白色;要指定不同的背景颜色,请使用 android.adaptiveIcon.backgroundColor
属性。你可以使用 android.adaptiveIcon.backgroundImage
属性指定背景图片。确保它与前景图片具有相同的尺寸。
¥The default background color is white; to specify a different background color, use the android.adaptiveIcon.backgroundColor
property. You can instead specify a background image using the android.adaptiveIcon.backgroundImage
property. Make sure that it has the same dimensions as your foreground image.
你可能还想为不支持自适应图标的旧版 Android 设备提供单独的图标。你可以使用 android.icon
属性来执行此操作。这个单个图标将是前景层和背景层的组合。
¥You may also want to provide a separate icon for older Android devices that do not support Adaptive Icons. You can do so with the android.icon
property. This single icon would be a combination of your foreground and background layers.
请参阅 苹果最佳实践 以确保你的图标看起来很专业,例如在不同的壁纸上测试你的图标并避免在产品的字标旁边出现文本。提供至少 512x512 像素的图标。
¥See Apple best practices to ensure your icon looks professional, such as testing your icon on different wallpapers and avoiding text beside your product's wordmark. Provide an icon that's at least 512x512 pixels.
¥iOS
对于 iOS,你的应用图标应遵循 Apple 人机界面指南。你还应该:
¥For iOS, your app's icon should follow the Apple Human Interface Guidelines. You should also:
使用 .png 文件。
¥Use a .png file.
1024x1024 是一个不错的尺寸。如果你有一个使用 npx create-expo-app
创建的 Expo 项目,EAS 构建 将为你生成其他尺寸。如果是裸 React Native 项目,请自行生成图标。EAS Build 生成的最大尺寸为 1024x1024。
¥1024x1024 is a good size. If you have an Expo project created using npx create-expo-app
, EAS Build will generate the other sizes for you. In case of a bare React Native project, generate the icons on your own. The largest size EAS Build generates is 1024x1024.
图标必须是正方体。例如,1023x1024 图标无效。
¥The icon must be exactly square. For example, a 1023x1024 icon is not valid.
确保图标填充整个正方形,没有圆角或其他透明像素。操作系统会在适当的时候屏蔽你的图标。
¥Make sure the icon fills the whole square, with no rounded corners or other transparent pixels. The operating system will mask your icon when appropriate.