启动前出现空白屏幕

blank screen comes before splash

主要问题是闪屏会在 2-3 秒后出现。在启动画面之前出现一个我不想要的空白布局。否则它运行良好。只想删除启动页面之前出现的空白布局。

主要活动:

public class MainActivity extends Activity {

    private static String TAG = MainActivity.class.getName();
    private static long SLEEP_TIME = 5; // Sleep for some time

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE); // Removes title bar
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);  // Removes notification bar

        setContentView(R.layout.activity_main);

        // Start timer and launch main activity
        IntentLauncher launcher = new IntentLauncher();
        launcher.start();
    }

    private class IntentLauncher extends Thread {

        @Override
        /**
         * Sleep for some time and than start new activity.
         */
        public void run() {
            try {
                // Sleeping
                Thread.sleep(SLEEP_TIME*1000);
            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
            }

            // Start main activity
            Intent intent = new Intent(MainActivity.this, Login.class);
            MainActivity.this.startActivity(intent);
            MainActivity.this.finish();
        }
    }

}

主要布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/splash"
    tools:context=".MainActivity" >

</RelativeLayout>

只需在 AndroidManifest.xml 文件中将透明主题提到起始 activity 即可。

      <activity
        android:name="first Activity Name"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" >
         <intent-filter>
          <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
      </activity>

一般来说,不建议为应用程序使用启动画面,但如果您确实需要的话。

Android 将在根据您为其设置的主题加载 activity 布局之前加载一个空白布局。解决方法是将splash的主题activity设为透明

res/values/styles.xml

中创建透明主题
<?xml version="1.0" encoding="utf-8"?>
<resources>
   <style name="Theme.Transparent" parent="android:Theme">
      <item name="android:windowIsTranslucent">true</item>
      <item name="android:windowBackground">@android:color/transparent</item>
      <item name="android:windowContentOverlay">@null</item>
      <item name="android:windowNoTitle">true</item>
      <item name="android:windowIsFloating">true</item>
     <item name="android:backgroundDimEnabled">false</item>
  </style>
</resources>

然后在清单中设置主题

<activity android:name=".SplashActivity" android:theme="@style/Theme.Transparent">
...
</activity>

它是 android 功能。您可以更改空白屏幕的背景颜色。使用风格:

<resources>
<style name="Theme" parent="android:style/Theme" />
<style name="Theme.MainTheme" parent="Theme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowBackground">@drawable/background_drawable</item>
</style>
</resources>

然后在清单中使用它:

<application
    android:name="@string/app_name"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.MainTheme"
     >

您在此处面临的问题称为 'cold start',它主要花费在 Application.onCreate 方法上,该方法通常会进行一些初始化,并且可能需要比您希望的时间更长的时间。您可以在这里阅读官方文档:https://developer.android.com/topic/performance/launch-time.html

如果是这种情况,那么按照其他人的建议将主题设置为半透明或禁用预览只会明显地解决问题,因为实际上您将尝试启动该应用程序,但您不会收到有关该事实的任何反馈你点击了应用程序图标。您会看到您的应用启动有延迟,这不是理想的用户体验。

空白、黑色或白色屏幕,这实际上取决于您在 activity 主主题中指定的 android:windowBackground 属性。 除了移动您可能在 Application.onCreate 方法中进行的一些初始化之外,您可以做的最好的事情是使用此 post 中建议的徽标标记您的预览 window:

https://plus.google.com/+AndroidDevelopers/posts/Z1Wwainpjhd

您可以进一步改善用户体验,方法是在启动画面中为您的徽标图像设置动画(如果是这种情况),或者将预览 window 设计为可以平滑过渡到主屏幕的方式activity 内容,如此处所述:

http://saulmm.github.io/avoding-android-cold-starts

同样的问题在这里得到正确回答: and described in this blog post: https://www.bignerdranch.com/blog/splash-screens-the-right-way/

真的!以下 link 适合我!!!

https://cyrilmottier.com/2013/01/23/android-app-launching-made-gorgeous/

在 style.xml 中为您的初始屏幕编写单独的样式,以便您可以将两个屏幕关联起来。

此处样式:

<style name="AppTheme.Splash" parent="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowBackground">@drawable/splash_final</item>
    </style>

开始时最好使用 主题背景 activity 但是如果您不希望在启动主要 activity 之前出现空白屏幕,您可以这样定义你的 activity:

android:windowDisablePreview 添加到 res/values/styles 中的 AppTheme。xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <style name="AppTheme" parent="android:Theme">
        <item name="android:windowDisablePreview">true</item>
  </style>
</resources>

然后在清单中设置 activity 主题:

<activity android:name=".MainActivity" android:theme="@style/AppTheme">
...
</activity>

P.S: 设置android:windowDisablePreview对你的activity背景没有影响,所以你不用担心。

我通过更新 Android Studio 中的构建工具修复了我的问题。

我认为这与上面发布的一些答案类似。我仍然想推荐以下 Big Nerd Ranch 指南,了解如何以正确的方式创建启动画面,因为它有很好的插图并且易于理解。你真的应该去那里阅读它而不是继续下面:)。 https://www.bignerdranch.com/blog/splash-screens-the-right-way/

但简而言之,它的意思是,在应用程序启动时,您会启动初始启动 activity。为此 activity 您创建了一个 XML 可绘制对象 (@drawable/background_splash)。

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/gray"/>
    <item>
        <bitmap
            android:gravity="center"
            android:src="@mipmap/ic_launcher"/>
    </item>
</layer-list>

然后为启动 activity 创建一个主题,并将上面创建的可绘制对象设置为其 window 背景。

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@drawable/background_splash</item>
</style>

然后在清单中告诉启动画面 activity 使用上述主题。

<activity
    android:name=".SplashActivity"
    android:theme="@style/SplashTheme">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

最后在您的 splash activity 中实现如下所示的 onCreate 方法(这就是您在 activity 中需要的所有代码)。这将启动您的主要 activity 并完成启动 activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
    finish();
}

将其添加到您的样式文件中

    <item name="android:windowDisablePreview">true</item>

支持 AppCompatActivity

res/values/styles.xml

<style name="SplashTheme" parent="AppTheme">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:backgroundDimEnabled">false</item>
    <item name="android:windowDisablePreview">true</item>
</style>

GL

添加此样式

 <style name="Theme.Transparent" parent="AppTheme">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDisablePreview">true</item>
        <item name="android:windowContentOverlay">@null</item>
 </style>

如果您在初始屏幕中加载图像而不是从布局中加载图像, 像下面的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/color_white">
    <ImageView
            android:id="@+id/ivSplash"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/SPLASH_IMAGE"
            />
</LinearLayout>