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

And*_*w S 13 java android splash-screen android-12

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

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

我在启动画面的主题中看到属性 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>
Run Code Online (Sandbox Code Playgroud)

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

And*_*w S 17

当我写这个问题并准备发布它时,我偶然发现了方法 setKeepOnScreenCondition (如下),该方法属于我们必须在主要活动的 onCreate 上安装的启动屏幕。我认为不发布此内容似乎很浪费,因为没有关于此主题的其他帖子,也没有对其他相关问题的类似答案(截至 2022 年 1 月)。

SplashScreen splashScreen = SplashScreen.installSplashScreen(this);
splashScreen.setKeepOnScreenCondition(....);
Run Code Online (Sandbox Code Playgroud)

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

 public boolean shouldKeepOnScreen() 
Run Code Online (Sandbox Code Playgroud)

看来这个方法将被启动屏幕调用并保留启动屏幕可见直到它返回 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;
        }
    };
    
}
Run Code Online (Sandbox Code Playgroud)

如果您热衷于 Java Lambda,一个更好、更紧凑的解决方案如下:

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);;
    }


    
}
Run Code Online (Sandbox Code Playgroud)

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


idd*_*lip 9

在科特林中:

var keepSplashOnScreen = true
val delay = 2000L

installSplashScreen().setKeepOnScreenCondition { keepSplashOnScreen }
Handler(Looper.getMainLooper()).postDelayed({ keepSplashOnScreen = false }, delay)
Run Code Online (Sandbox Code Playgroud)

您可以在 super.onCreate 调用之前将其放入 onCreate fun 中(在清单中使用 LAUNCHER 意图过滤器的活动中)