Android 12闪屏API -增加闪屏持续时间

esbemjvw  于 2022-12-31  发布在  Android
关注(0)|答案(3)|浏览(344)

我正在学习Android的新闪屏API引入Android 12。我已经得到了我的模拟器和谷歌像素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>

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

vsmadaxz

vsmadaxz1#

在我写这个问题的时候,我无意中发现了一个方法setKeepOnScreenCondition(见下图),这个方法属于我们必须安装在主Activity的onCreate上的splashScreen。我觉得不发布这个方法似乎是浪费,因为没有关于这个主题的其他帖子,也没有其他相关问题的类似答案(截至2022年1月)。

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

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

public boolean shouldKeepOnScreen()

这个方法似乎会被闪屏调用,并且在返回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 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);;
    }

    
}

如果你有意见或反馈(除了告诉我,我不应该增加启动画面的持续时间),或更好的方式,请做评论或回应额外的答案。

nfeuvbwi

nfeuvbwi2#

在Kotlin:

var keepSplashOnScreen = true
val delay = 2000L

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

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

cbjzeqam

cbjzeqam3#

一种代理方式是在onCreate方法中使用runBlocking { delay(1200) },以便在某个特定时间内保持在主线程上。

相关问题