首页 > 解决方案 > Android Splash Screen App Icon加载后不会消失 - React Native

问题描述

我在这里遵循了本教程,它确实有效,因为它在我的应用程序上添加了启动屏幕应用程序徽标。

基本上在我的AndroidManifest.xml文件中,我有 ff 代码:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.coupalreactnative">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
      android:name=".MainApplication"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:allowBackup="false"
      android:theme="@style/AppTheme">
        <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>
    </application>
</manifest>

然后在我的res/drawable/splash_screen.xml文件上:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@color/white"/>
    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/books_splash"/>
    </item>
</layer-list>

当我运行该应用程序时,它基本上可以工作,但是即使主屏幕已经加载,应用程序徽标也不会在背景上消失。

在此处输入图像描述

我是 React Native 的初学者,有什么解决方法吗?我如何解决它?

标签: androidreact-native

解决方案


我已经这样做了(在 Kotlin 中,但我认为在 java 中是一样的):

  1. 创建一个 SplashScreen 活动
  2. 设置 SplashScreen 的布局(使用 ImageView)
  3. 使用 viewBinding(在 gradle 中设置:BuildFeatures { viewBinding = true }
  4. 复制可绘制文件夹中的徽标/图像并将其源到 ImageView
  5. 创建动画 XML 文件
  6. 使用绑定、layoutInflater 加载 SplashScreen 布局
  7. 创建和实现动画功能(在 AnimationEnd 上)
  8. 在 onAnimationEnd 函数中,我设置了 2.5 秒的延迟,然后关闭 SplashScreen 并转到 MainActivity。
  9. 确保您将 MAIN 和 LAUNCHER 的 INTENT 设置为 SplashScreenActivity(在 AndroidManifest 中)

我的安卓清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.uwbeinternational.weatherapp">

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:networkSecurityConfig="@xml/network_security_config">
        <activity
            android:name=".SplashScreenActivity"
            android:exported="true"
            android:theme="@style/Theme.UwBeWeatherApp.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:exported="true">
        </activity>
    </application>

</manifest>

我的动画 XML 文件(在新文件夹/包“anim”下):

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true">

    <!--<translate
        android:duration="2000"
        android:fromXDelta="0%p"
        android:fromYDelta="0%p"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:toXDelta="0%p"
        android:toYDelta="50%p"
        />-->

    <alpha
        android:duration="2000"
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator">

    </alpha>

</set>

我的 SplashScreen 活动:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        //SplashScreen - UwBe Logo Anzeigen und einblenden lasssen
        val splashBinding: ActivitySplashScreenBinding = ActivitySplashScreenBinding.inflate(layoutInflater)
        setContentView(splashBinding.root)

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            window.insetsController?.hide(WindowInsets.Type.statusBars())
        } else {
            @Suppress("DEPRECATION")
            window.setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN
            )
        }

        val splashAnimation = AnimationUtils.loadAnimation(this, R.anim.anim_splash)
        splashBinding.ivAppLogo.animation = splashAnimation

        splashAnimation.setAnimationListener(object: Animation.AnimationListener{
            override fun onAnimationStart(animation: Animation?) {
                //
            }

            override fun onAnimationEnd(animation: Animation?) {
                Handler(Looper.getMainLooper()).postDelayed({
                    startActivity(Intent(this@SplashScreenActivity, MainActivity::class.java))
                    finish()
                }, 1500)
            }

            override fun onAnimationRepeat(animation: Animation?) {
                //
            }

        })

推荐阅读