gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 23:47:02 25 4
gpt4 key购买 nike

我正在学习 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>
有没有一种直接的方法来延长非动画启动画面的持续时间?

最佳答案

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

SplashScreen splashScreen = SplashScreen.installSplashScreen(this);
plashScreen.setKeepOnScreenCondition(....);
经过检查,我发现此方法接收到 的实例。 splashScreen.KeepOnScreenCondition() 实现必须为其提供以下方法签名实现的接口(interface):
 public boolean shouldKeepOnScreen() 
似乎此方法将被启动屏幕调用并保持启动屏幕可见,直到它返回 false。这就是我如此热爱编程的灯泡时刻发生的地方。
如果我使用初始化为 true 的 boolean 值,并在延迟后将其设置为 false 怎么办?事实证明,这种预感奏效了。这是我的解决方案。它似乎有效,我认为它对其他人有用。据推测,不是使用 Handler 进行延迟,还可以在某个过程完成后使用它来设置 boolean 值。
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);;
}



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

关于java - Android 12 启动画面 API - 增加启动画面持续时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70857274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com