我正在学习安卓的新SplashScreen API,它是用安卓12推出的。到目前为止,我已经让它在我的模拟器和GooglePixel4A上工作了,但我想延长它的使用期限。在我的飞溅屏幕上,我不想要一个花哨的动画,我只是想要一个静态绘图。
我知道,我知道(叹气)你们中的一些人可能在想,我不应该延长时间,我知道有几个很好的理由支持不这样做。然而,对我来说,使用非动画绘图的启动屏幕的持续时间是如此之短(不到一秒),我认为它引起了可访问性的关注,特别是因为它不能被禁用(具有讽刺意味)。简单地说,产品背后的组织或其品牌/产品标识不能被新用户正确地吸收或识别,因此在那个时候,新的splash屏幕是多余的。
我在启动屏幕的主题中看到属性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>
是否有一种直接的方式来延长非动画启动屏幕的持续时间?
发布于 2022-01-26 00:09:00
当我写这个问题时,我几乎准备发布它,我偶然发现了setKeepOnScreenCondition方法(下面),它属于我们必须安装在我们的主要活动的onCreate上的splashScreen。我认为不发布这篇文章似乎很浪费,因为没有关于这个主题的其他帖子,也没有类似于其他相关问题的答案(截至2022年1月)。
SplashScreen splashScreen = SplashScreen.installSplashScreen(this);
plashScreen.setKeepOnScreenCondition(....);
在检查它时,我发现该方法接收到splashScreen.KeepOnScreenCondition()接口的一个实例,该实现必须为其提供以下方法签名实现:
public boolean shouldKeepOnScreen()
看来,这个方法将由启动屏幕调用,并明显保留启动屏幕,直到返回false为止。这就是我如此热爱编程的时刻。
如果我使用初始化为true的布尔值,并在延迟后将其设置为false怎么办?这种预感终于起作用了。这是我的解决办法。它似乎很有效,我认为它对其他人是有用的。可以推测,与使用Handler进行延迟相比,还可以使用它来在某个进程完成后设置布尔值。
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感兴趣,那么一个更好、更紧凑的解决方案如下:
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);;
}
}
如果你有评论或反馈(除了告诉我,我不应该增加飞溅屏幕的持续时间),或者一个更好的方法,请做评论或回应以更多的答案。
发布于 2022-04-19 08:49:04
在科特林:
var keepSplashOnScreen = true
val delay = 2000L
installSplashScreen().setKeepOnScreenCondition { keepSplashOnScreen }
Handler(Looper.getMainLooper()).postDelayed({ keepSplashOnScreen = false }, delay)
您可以在onCreate调用之前将其放入super.onCreate fun中(在Manifest中使用启动程序意图过滤器进行活动)
https://stackoverflow.com/questions/70857274
复制相似问题