【问题标题】:blank screen comes before splash黑屏出现在飞溅之前
【发布时间】:2015-08-01 07:16:06
【问题描述】:

主要问题是 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>

【问题讨论】:

标签: android splash-screen


【解决方案1】:

一般来说,不建议在应用中使用启动画面,但如果您确实必须这样做的话。

Android 会在加载基于您为其设置的主题的活动布局之前加载空白布局。解决方法是将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>

【讨论】:

  • 你已经更新你的styles.xml到这个
  • 此主题不适用于 AppCompatActivity,也不会移除状态栏。
【解决方案2】:

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

在 res/values/styles.xml 中将 android:windowDisablePreview 添加到您的 AppTheme

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

然后在清单中设置您的活动主题:

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

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

【讨论】:

  • 禁用预览救了我。谢谢
【解决方案3】:

您在这里面临的问题称为“冷启动”,这是您的 Application.onCreate 方法所花费的大部分时间,该方法通常会进行一些初始化,并且可能需要比您希望的更长的时间。你可以在这里阅读官方文档:https://developer.android.com/topic/performance/launch-time.html

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

空白,黑色或白色屏幕,这实际上取决于您的主要活动主题中指定的android:windowBackground 属性。 除了移动您可能在 Application.onCreate 方法中所做的一些初始化之外,您能做的最好的事情就是按照这篇文章中的建议为您的预览窗口添加一个徽标:

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

如果是这种情况,您可以通过在初始屏幕中为您的徽标图像设置动画,或者通过设计预览窗口以平滑过渡到您的主要活动内容的方式来进一步改善用户体验,如下所述:

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

同样的问题在这里得到了正确的回答:https://stackoverflow.com/a/40482549/7094200,并在这篇博文中进行了描述:https://www.bignerdranch.com/blog/splash-screens-the-right-way/

【讨论】:

    【解决方案4】:

    支持 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

    Source

    【讨论】:

    • 拯救了我的一天。谢谢
    【解决方案5】:

    只需在 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>
    

    【讨论】:

      【解决方案6】:

      真的!下面的链接对我有用!!!!

      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>
      

      【讨论】:

      • 非常感谢,这是我一直在寻找的教程!我完全忘记了是谁写的。
      【解决方案7】:

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

      <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"
           >
      

      【讨论】:

        【解决方案8】:

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

        但简而言之,它建议的是,在应用程序启动时,您会启动初始启动活动。对于此活动,您将创建一个 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>
        

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

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

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

        <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>
        

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

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

        【讨论】:

          【解决方案9】:

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

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

          【讨论】:

            【解决方案10】:

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

            【讨论】:

              【解决方案11】:

              添加此样式

               <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>
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2011-07-20
                • 1970-01-01
                • 2012-12-27
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多