【问题标题】:Splash Screen - white启动画面 - 白色
【发布时间】:2018-10-11 08:03:38
【问题描述】:

对于我正在使用的启动画面:

setContentView(R.layout.activity_home);
new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        startActivity(new Intent(getApplicationContext(),MainActivity.class));
    }
}, 3500);

它正在工作,它显示我的启动屏幕 3 秒半。但是,当我启动应用程序时,首先显示器是白色的一秒钟,然后它显示我的启动画面。由于代码有效,问题可能出在我使用的手机而不是模拟器上吗?或者我需要在我的代码中添加一些东西?

【问题讨论】:

  • 你没有做错什么。您最初看到的空白屏幕是因为您的应用程序冷启动。 saulmm.github.io/avoding-android-cold-starts
  • 看看this
  • @AyushKhare 提供的链接将对您有所帮助。
  • 你用什么手机测试?
  • 您也可以更改该白屏。实际上,这应该是实现“启动画面”的正确方法。检查这个:android.jlelse.eu/…

标签: android splash-screen


【解决方案1】:

白屏是由 AppTheme 引起的。当应用程序初始化时,它会在设置任何视图之前显示默认白屏。

您只需将以下属性添加到您的 AppTheme 即可使该白屏消失。

只需添加

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowBackground"><place here any drawable or color></item>
</style>

如果对您有帮助,请告诉我。谢谢!!!

【讨论】:

  • 我做到了。我将它添加到 styles.xml 但现在,我有背景,而不是白屏,然后是启动活动。所以,我不是白屏,而是我的@drawable/dog 背景
  • 是的,这就是摆脱白屏的方法
【解决方案2】:

不,这不是您的代码的问题。我的应用程序也会发生这种情况。似乎当android加载应用程序本身时出现白屏。如果您在开始时加载很多内容,则加载时间会更长,因此白屏会更大。

【讨论】:

  • 感谢您的回答
【解决方案3】:

对于我的 Splashscreen,我使用了两个活动,您还必须在清单文件中注册:

<activity
        android:name=".ActivityMain"
        android:configChanges="orientation|screenSize"
        android:label="@string/app_name"
        android:parentActivityName=".SplashActivity"
        android:windowSoftInputMode="stateAlwaysHidden">
        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
        </intent-filter>
    </activity>
    <activity android:name=".SplashActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

在我的 Splash Activity 中,我通过以下意图开始我的主要活动:

public class SplashActivity extends AppCompatActivity {

    private final Runnable task = () -> {
        Intent intent = new Intent(this, ActivityMain.class);
        startActivity(intent);
        finish();
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        final AnimatedVectorDrawableCompat animatedVectorDrawableCompat =
                AnimatedVectorDrawableCompat.create(getApplicationContext(),
                        R.drawable.animatedvector);

        ImageView imageView = findViewById(R.id.imageView);
        imageView.setImageDrawable(animatedVectorDrawableCompat);

        final Animatable animatable = (Animatable) imageView.getDrawable();
        animatable.start();

        Handler handler = new Handler();
        handler.postDelayed(task, 1500);
    }
}

如您所见,主要活动的开始被处理程序延迟。

希望这会有所帮助;这段代码我没有得到白屏;-)

【讨论】:

  • 如果应用程序冷启动并且正在进行一些初始化(例如在Applications onCreate等中创建数据库),您将得到白屏
  • 我不这么认为,因为我正在完成主要活动开始后的第一个活动
  • 无关紧要,因为在显示任何活动内容(布局)之前初始化应用程序。这就是冷启动的意义所在。看这里:saulmm.github.io/avoding-android-cold-starts
猜你喜欢
  • 2013-06-15
  • 2021-04-12
  • 1970-01-01
  • 2020-08-18
  • 1970-01-01
  • 2021-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多