【问题标题】:How to remove black screen when opening app打开应用程序时如何消除黑屏
【发布时间】:2017-08-02 13:16:44
【问题描述】:

我有一个启动画面,它只在应用程序重新启动时显示。如果用户点击返回按钮并再次启动应用程序,则不会显示启动画面。到这里为止一切都很好,如果没有显示启动画面,则打开应用程序时会出现 1-2 秒的黑屏。这是我的splashactivity java 文件;

public class SplashScreen extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.getBoolean("first_time", false)) // if first time, show splash
{

    SharedPreferences.Editor editor = prefs.edit();
    editor.putBoolean("first_time", true);
    editor.commit();


    setContentView(R.layout.activity_splash);


    Thread t = new Thread() {
        public void run() {
            try {
                int time = 0;
                while (time < 4000) {
                    sleep(100);
                    time += 100;
                }
            }
            catch (InterruptedException e) {
                // do nothing
            }
            finally {

                Intent i = new Intent(SplashScreen.this, MainActivity.class);
                startActivity(i);
                finish();
            }
        }
    };
    t.start();
}
else // if not first time, dont show splash
{
    setContentView(R.layout.activity_splash);
    Intent i = new Intent(SplashScreen.this, MainActivity.class);
    startActivity(i);
    finish();
}

我该如何解决这个问题?

【问题讨论】:

标签: java android


【解决方案1】:

因为您在显示初始屏幕之前检查它是否是“第一次”打开应用程序:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.getBoolean("first_time", false)) {
    // Show splash
} else {
    // Don't show splash
}

这不是 right way 首先创建启动屏幕。您不应该通过创建计时器让您的用户等待启动屏幕。 Instead,您应该只在应用加载所需的时间内显示初始屏幕。

为此,您应该创建一个简单的可绘制图层列表以用作启动活动的背景:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- A solid background fill -->
    <item
        android:drawable="@color/gray"/>
    <item>

    <!-- A centered logo -->
    <bitmap
        android:gravity="center"
        android:src="@mipmap/ic_launcher"/>
    </item>

</layer-list>

然后将此层列表用作您将在启动活动中使用的主题的background

<resources>

    <!-- Base application theme -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>

    <!-- Splash Screen theme -->
    <style name="SplashTheme">
        <item name="android:background">@drawable/background_splash</item>
        <item name="android:windowAnimationStyle">@null</item>
    </style>

</resources>

将该主题应用于清单中的启动活动:

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

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

然后,最后,您在启动活动中所要做的就是启动您的MainActivity

public class SplashScreen extends AppCompatActivity {

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

        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}

确保您不要在您的启动画面中调用setContentView(),因为这会给您的启动画面增加不必要的加载时间。

【讨论】:

  • 更好的方法,但这会在每次用户打开应用程序时显示启动?假设我的应用程序加载了 1 秒,这只会显示 1 秒的启动画面,这不是没有必要的吗?使用这种技术并至少在 3 秒内显示飞溅效果如何,并且仅在重新开始时显示?
  • @user198989 因为这不是最佳实践,正如我链接到的article 中提到的那样。为什么要让用户等待?移动应用的目的不就是快吗?
  • @user19889 是的,这个方法会在每次打开应用程序时显示启动画面(但在暂停后被带到最前面时不会显示)。
【解决方案2】:

在应用样式中添加这个:

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

【讨论】:

    【解决方案3】:
     /Just use the below code Snipeet/
    
    Add following code in Style.xml file
    
    <style name="Theme.Transparent" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowActionBar">false</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowIsTranslucent">true</item>
    
    </style>
    
     And then use this style in AndroidManifest inside application Tag
     Like this: 
     android:theme="@style/Theme.Transparent"
    

    【讨论】:

      【解决方案4】:

      添加@Bryan 我向您推荐cold start。例如我确实喜欢这个

      在您的 Style.xml 中:

          <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
              <item name="colorPrimary">@color/colorPrimary</item>
              <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
              <item name="colorAccent">@color/colorAccent</item>
          </style>
      
      <style name="AppTheme.splashScreenLauncher">
          <item name="android:windowBackground">@drawable/splash_screen</item>
          <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
      
      </style>
      

      splash_screen.xml:

      <layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">
          <!-- The background color, preferably the same as your normal theme -->
          <item android:drawable="@android:color/white"/>
      
          <item>
              <bitmap
                  android:src="@drawable/SPLASHIMAGE"
                  android:gravity="center"/>
          </item>
      </layer-list>
      

      在清单中,您可以在其中设置启动器活动:

      android:theme="@style/AppTheme.splashScreenLauncher"> 
      

      在您的启动器活动中:

      setTheme(R.style.AppTheme); // IMPORTANT BEFORE super.onCreate
      super.onCreate(savedInstanceState);
      

      【讨论】:

        【解决方案5】:

        您必须将初始屏幕背景颜色作为应用程序的主主题颜色。

        <activity
                android:name=".SplashScreen"
                android:label="@string/app_name"
                android:screenOrientation="portrait"
                android:theme="@style/SplashScreenTheme">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        

        在你的 values/style.xml 中设置样式`

        <style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <style name="SplashScreenTheme" parent="AppBaseTheme">
            <item name="windowActionBar">false</item>
            <item name="windowNoTitle">true</item>
            <item name="android:windowBackground">@color/white</item>
            <item name="colorPrimaryDark">@color/white</item>
        </style>
        

        在你的 values-v21/style.xml 中设置样式

         <style name="SplashScreenTheme" parent="AppBaseTheme">
            <item name="windowActionBar">false</item>
            <item name="windowNoTitle">true</item>
            <item name="android:windowBackground">@color/white</item>
            <item name="android:colorPrimaryDark">@color/white</item>
        </style>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-11-19
          • 2021-06-13
          • 1970-01-01
          • 1970-01-01
          • 2020-12-14
          • 1970-01-01
          • 2020-03-17
          相关资源
          最近更新 更多