2017-08-02 96 views
1

我有一個啓動畫面,僅在應用程序的新開始時顯示。如果用戶點擊返回按鈕並再次啓動應用程序,則不顯示啓動畫面。一切都很好,直到這裏,如果飛濺不顯示,打開應用程序時有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(); 
} 

我該如何解決這個問題?

+0

試試這個:'機器人:主題=「@機器人:樣式/ Theme.Translucent「'在'manifest.xml' –

+0

同樣的問題試試這個https://stackoverflow.com/questions/8817568/how-to-avoid-black-screen-in-android-while-my-app -is-loading –

回答

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()在飛濺的活動,因爲這會增加不必要的加載時間你的閃屏。

+0

更好的方法,但是每次用戶打開應用程序時都會顯示splash?讓我們說我的應用程序加載1秒,這將只顯示飛濺1秒,是不是沒有必要?如何使用這種技術,並顯示飛濺最小3秒​​,只有在新的開始? – user198989

+0

@ user198989因爲這不是最佳做法,正如我鏈接到的[文章](https://www.bignerdranch.com/blog/splash-screens-the-right-way/)中所述。爲什麼讓用戶等待?移動應用的目的不是快速? – Bryan

+0

@ user19889是的,這種方法會在每次打開應用程序時顯示啓動畫面(但不會在被暫停時顯示在前面)。 – Bryan

1

在應用樣式補充一點:

<item name="android:windowDisablePreview">true</item> 
1
/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" 
1

您必須給出啓動畫面背景色作爲您的應用程序主要顏色。

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

設置樣式在你的價值觀/ 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> 

設置樣式在你的價值觀-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> 
1

添加@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); 
相關問題