简体   繁体   English

如何为 react-native android 设置启动画面

[英]How to set the splash screen for react-native android

How does one set a splash screen for react-native android, I can't find anything on the topic and I thought it was odd.如何为 react-native android 设置启动画面,我找不到有关该主题的任何内容,我认为这很奇怪。

Thanks谢谢

I had tried 3 of the following ways.我尝试了以下 3 种方法。 The first one is what I am currently using for android splash screen for react-native projects.第一个是我目前用于 react-native 项目的 android 启动画面。

  1. Using a npm package written by other.使用其他人编写的 npm 包。

    reference: https://github.com/remobile/react-native-splashscreen参考: https : //github.com/remobile/react-native-splashscreen

  2. Create a SplashScreen component and redirect afterward.创建一个SplashScreen组件,然后重定向。

    reference: How to create some kind of Splash screen/Launching screen, which disappears after App loaded?参考: 如何创建某种启动画面/启动画面,在应用程序加载后消失? (React Native) (反应原生)

  3. Natively in java code.原生在java代码中。

    reference:https://www.bignerdranch.com/blog/splash-screens-the-right-way/参考:https ://www.bignerdranch.com/blog/splash-screens-the-right-way/

I have a fetch request in the componentDidMount() of initialRoute .我在initialRoutecomponentDidMount()中有一个fetch请求。

Using the first way from the list above performs the request while showing the splash screen.使用上面列表中的第一种方式在显示启动屏幕的同时执行请求。

Whereas the second way, needs to wait until the SplashScreen component get unmounted.而第二种方式,需要等到SplashScreen组件被卸载。

Third way is slightly more codes to write and maintain.第三种方法是编写和维护的代码稍微多一些。

This tutorial here works great - I quoted it and modified a bit, I added the react-native-background-color touch.本教程在这里效果很好 - 我引用了它并进行了一些修改,我添加了react-native-background-color触摸。

https://medium.com/react-native-development/change-splash-screen-in-react-native-android-app-74e6622d699 (which is based on thishttps://www.bignerdranch.com/blog/splash-screens-the-right-way/ - so this is native Android technique) https://medium.com/react-native-development/change-splash-screen-in-react-native-android-app-74e6622d699 (基于这个https://www.bignerdranch.com/blog/splash -screens-the-right-way/ - 所以这是原生 Android 技术)

  1. You need to create splash screen in res/drawable.您需要在 res/drawable 中创建启动画面。 Let's call it splash_screen.xml我们称之为 splash_screen.xml

     <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@android:color/white"/> <item> <bitmap android:gravity="center" android:src="@mipmap/ic_launcher"/> </item> </layer-list>
  2. Now you need to update res/values/styles.xml现在你需要更新 res/values/styles.xml

     <resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> </style> <style name="SplashTheme" parent="AppTheme"> <item name="android:windowBackground">@drawable/splash_screen</item> </style> </resources>
  3. Now open AndroidManifest.xml and replace AppTheme with SplashTheme in MainActivity现在打开 AndroidManifest.xml 并将AppTheme SplashTheme为 MainActivity 中的SplashTheme

     <activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/SplashTheme" android:configChanges="keyboard|keyboardHidden|orientation|screenSize"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>

    We use SplashTheme instead of updating AppTheme, to add this background only to start activity and leave other stuff without changes.我们使用 SplashTheme 而不是更新 AppTheme,添加此背景只是为了启动活动,而其他内容保持不变。

  4. After you try the above out, you will notice the splash screen will always stay under your js views.尝试以上操作后,您会注意到启动画面将始终停留在您的 js 视图下方。 You have two options to hide the splash screen:您有两个选项可以隐藏启动画面:

    • Either use react-native-background-color module from https://github.com/ramilushev/react-native-background-color to set a color on the background which will remove the image.使用https://github.com/ramilushev/react-native-background-color 中的react-native-background-color 模块在背景上设置一种颜色,这将删除图像。 (This is the recommended way because when keyboard shows in some cases, it makes the root view visible for a split second.) (这是推荐的方式,因为在某些情况下当键盘显示时,它会使根视图在一瞬间可见。)
    • Or set a solid (non-transparent) background color on your root view.或者在您的根视图上设置纯色(非透明)背景色。

Note on what "root view" means.请注意“根视图”的含义。 This is the very first <View> you render in your app that you control (meaning that you can style it).这是您在应用中呈现的第一个由您控制的<View> (意味着您可以对其进行样式设置)。

Custom Color自定义颜色

If you want to use a custom color, other then @android:color/*** then what you have to do is create colors.xml at android\\app\\src\\main\\res\\values\\colors.xml and define a colors like this:如果你想使用自定义颜色,然后其他@android:color/*** ,那么你所要做的就是创建colors.xmlandroid\\app\\src\\main\\res\\values\\colors.xml并定义颜色像这样:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="foobar">#ca949b</color>
</resources>

You can use whatever name and whatever color code.您可以使用任何名称和任何颜色代码。

Then back in splash_screen.xml we update <item android:drawable="@android:color/white" /> to be <item android:drawable="@color/foobar" />然后回到splash_screen.xml我们将<item android:drawable="@android:color/white" /><item android:drawable="@color/foobar" />

Extra info on removing the background splash image from behind关于从后面删除背景飞溅图像的额外信息

After you create a splash like this.在你创建这样的飞溅之后。 You will notice that the image stays in the background forever.您会注意到图像永远留在背景中。 To remove this image use this module - https://github.com/ramilushev/react-native-background-color - and call BackgroundColor.setColor('#XXXXXX') with whatever color.要删除此图像,请使用此模块 - https://github.com/ramilushev/react-native-background-color - 并使用任何颜色调用BackgroundColor.setColor('#XXXXXX') It will remove the image.它将删除图像。

Instead of using an opaque color on your root view to cover the splash, it is still recommended to use the above module, because when the keyboard shows, the background view shows for a split second.不要在根视图上使用不透明的颜色来覆盖飞溅,仍然建议使用上述模块,因为当键盘显示时,背景视图会显示一秒钟。

Did you try to use this ?你试过用这个吗? I came across this a few days ago.几天前我遇到了这个。 Works fine on iOS but I haven't tested it yet on Android (it should be fine).在 iOS 上运行良好,但我还没有在 Android 上测试过(应该没问题)。 You should have node >= 6 and imageMagic installed.您应该安装了 node >= 6 和 imageMagic。 (for mac: brew install imagemagic ) (对于 Mac: brew install imagemagic

npm install -g yo generator-rn-toolbox
yo rn-toolbox:assets --splash IMGAE --android

All you need to do is call the function in splash screen. 您需要做的就是在启动画面中调用该功能。

componentWillMount() {
    setTimeout(() => {
        this.props.navigation.navigate('Login')
    }, 1000);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM