Android 12 启动画面 API - 增加启动画面持续时间

Android 12 Splash Screen API - Increasing SplashScreen Duration

我正在学习 Android 的新 SplashScreen API 与 Android 12 一起引入。到目前为止,我已经让它在我的模拟器和 Google Pixel 4A 上工作, 但我想增加它的持续时间。在我的启动画面中,我不想要花哨的动画,我只想要一个静态可绘制对象。

我知道,我知道(叹息)你们中的一些人可能在想,我不应该增加持续时间,而且我知道有几个很好的论据支持不这样做。然而,对我来说,带有非动画可绘制对象的启动画面的持续时间是如此短暂(不到一秒),我认为这引起了可访问性问题,尤其是因为它不能被禁用(具有讽刺意味)。简单地说,产品背后的组织或其 brand/product 身份无法被那个规模和那个时间的新用户正确吸收或识别,从而使新的初始屏幕变得多余。

我在启动画面的主题中看到 属性 windowSplashScreenAnimationDuration(如下所示),但这对持续时间没有影响,大概是因为我没有设置动画。

 <style name="Theme.App.starting" parent="Theme.SplashScreen">
        <!--Set the splash screen background, animated icon, and animation duration.-->
        <item name="windowSplashScreenBackground">@color/gold</item>
    
        <!-- Use windowSplashScreenAnimatedIcon to add either a drawable or an
             animated drawable. One of these is required-->
        <item name="windowSplashScreenAnimatedIcon">@drawable/accessibility_today</item>
        <item name="windowSplashScreenAnimationDuration">300</item> <!--# Required for-->
                                                                    <!--# animated icons-->
        <!--Set the theme of the activity that directly follows your splash screen-->
        <item name="postSplashScreenTheme">@style/Theme.MyActivity</item>
    
        <item name="android:windowSplashScreenBrandingImage">@drawable/wculogo</item>
    
    </style>

有没有直接的方法来延长非动画启动画面的持续时间?

当我写这个问题并且几乎准备好 post 它时,我偶然发现了属于我们必须在我们的主要 activity.我认为不 post 这似乎很浪费,因为没有其他关于这个主题的 post 并且没有对其他相关问题的类似答案(截至 2022 年 1 月)。

SplashScreen splashScreen = SplashScreen.installSplashScreen(this);
plashScreen.setKeepOnScreenCondition(....);

检查后我发现此方法接收 splashScreen.KeepOnScreenCondition() 接口的一个实例,该接口的实现必须提供以下方法签名实现:

 public boolean shouldKeepOnScreen() 

这个方法似乎会被启动画面调用并保持启动画面可见,直到它 returns false。这就是我非常喜欢编程的灯泡时刻。

如果我使用一个初始化为 true 的布尔值,并在延迟后将其设置为 false 怎么办?事实证明,这种预感奏效了。这是我的解决方案。它似乎有效,我认为它对其他人有用。大概不是使用处理程序来延迟,也可以在某些过程完成后使用它来设置布尔值。

package com.example.mystuff.myactivity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.splashscreen.SplashScreen;
import android.os.Bundle;
import android.os.Handler;

public class MainActivity extends AppCompatActivity {
    
    private boolean keep = true;
    private final int DELAY = 1250;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Handle the splash screen transition.
        SplashScreen splashScreen = SplashScreen.installSplashScreen(this);
        super.onCreate(savedInstanceState);

        //Keep returning false to Should Keep On Screen until ready to begin.
        splashScreen.setKeepOnScreenCondition(new SplashScreen.KeepOnScreenCondition() {
            @Override
            public boolean shouldKeepOnScreen() {
                return keep;
            }
        });
        Handler handler = new Handler();
        handler.postDelayed(runner, DELAY);
    }

    /**Will cause a second process to run on the main thread**/
    private final Runnable runner = new Runnable() {
        @Override
        public void run() {
            keep = false;
        }
    };
    
}

如果您喜欢 Java Lambdas,一个更好更紧凑的解决方案如下:

package com.example.mystuff.myactivity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.splashscreen.SplashScreen;
import android.os.Bundle;
import android.os.Handler;

public class MainActivity extends AppCompatActivity {
    
    private boolean keep = true;
    private final int DELAY = 1250;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Handle the splash screen transition.
        SplashScreen splashScreen = SplashScreen.installSplashScreen(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Keep returning false to Should Keep On Screen until ready to begin.
    splashScreen.setKeepOnScreenCondition(() -> keep);
    Handler handler = new Handler();
    handler.postDelayed(() -> keep = false, DELAY);;
    }


    
}

如果您有意见或反馈(除了告诉我不应该增加启动画面的持续时间)或更好的方法,请发表评论或回复其他答案。

在 Kotlin 中:

var keepSplashOnScreen = true
val delay = 2000L

installSplashScreen().setKeepOnScreenCondition { keepSplashOnScreen }
Handler(Looper.getMainLooper()).postDelayed({ keepSplashOnScreen = false }, delay)

您可以在 super.onCreate 调用之前将它放入 onCreate fun 中(在 activity 中使用 Manifest 中的 LAUNCHER intent 过滤器)