1. 程式人生 > 程式設計 >Android 啟動頁白屏解決方案

Android 啟動頁白屏解決方案

當我們開啟app的時候是不是會有一瞬間的白屏然後再進入主活動,雖然這並不會造成什麼不好的後果,但是感覺使用者體驗就不是很好。像網易雲音樂等等,開啟一瞬間就顯示了他們的loge,無縫銜接,沒有白屏,怎麼做到的呢?

一開始我的思路是這樣的。可能是因為我們的主活動邏輯太多,所以載入會變慢,導致顯示白屏。如果使用一個只顯示一張本地圖片的活動,那會不會就不會顯示白屏了呢。話不多說我們嘗試一下:

Activity中的程式碼:

/**
 * 啟動頁,顯示傾旅的logo,停頓2秒後跳轉
 */
public class LunchActivity extends AppCompatActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lunch);

    //開啟子執行緒進行停頓。如果在主執行緒停頓的話,會造成主頁面卡死,所以在子執行緒sleep兩秒後跳轉
    new Thread(new Runnable() {
      @Override
      public void run() {
        try {
          Thread.sleep(1000);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        start();
        LunchActivity.this.finish();
      }
    }).start();
  }
  //跳轉到主頁面
  private void start(){
    Intent intent = new Intent(LunchActivity.this,MainActivity.class);
    startActivity(intent);
  }
}

layout中的程式碼:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#e74b37"
  tools:context=".LunchActivity">

  <ImageView
    android:id="@+id/imageView5"
    android:layout_width="80dp"
    android:layout_height="80dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.31"
    app:srcCompat="@drawable/icon" />
</android.support.constraint.ConstraintLayout>

這裡簡單指定一個imageView來顯示一張圖片。並把背景設定為橘色

最後再把啟動頁活動設定為主活動:

<activity android:name="com.example.qinglv.LunchActivity">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>

一切想的很好,完成後開啟一看,還是會白屏,怎麼回事?

活動的載入都是需要時間的,比較簡單的活動時間會少點,但是以然會有一瞬間的白屏。那這個白屏到底是什麼?就是每個活動的背景。當開啟一個活動的時候,因為還沒加載出內容,所以顯示的就只是背景,所以我們只需要,改變這個背景,設定為我們需要的一個logo照片即可。怎麼設定呢?

  • 背景是在主題中指定的,首先設定一個主題,把背景改成我們要的。一般和我們的啟動頁保持一致,這樣的話就不會看起來像兩個啟動頁一樣。也可以像網易雲音樂那樣,背景設定成logo,但是啟動頁是放廣告,但是這會影響使用者體驗(為了收入打點廣告也是可以理解的)。看程式碼:

在res-value-styles:

<style name="NewAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
   <!-- Customize your theme here. -->
   <item name="colorPrimary">@color/colorPrimary</item>
   <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
   <item name="android:windowBackground">@color/colorPrimary</item>
   <item name="colorAccent">@color/colorAccent</item>
 </style>

重點是這句<item name="android:windowBackground">@color/colorPrimary</item>
這裡我指定的是一種顏色你們也可以指定一張圖片

  • 再給啟動頁活動指定主題:

在:AndroidManifest:

<activity android:name="com.example.qinglv.LunchActivity"
     android:theme="@style/NewAppTheme">
     <intent-filter>
       <action android:name="android.intent.action.MAIN" />
       <category android:name="android.intent.category.LAUNCHER" />
     </intent-filter>
   </activity>

重點是這句android:theme="@style/NewAppTheme"

然後再開啟的時候,就會發現不會了。原本顯示的白屏變成了我們設定好的圖片。

以上就是Android 啟動頁白屏解決方案的詳細內容,更多關於Android 啟動頁白屏的資料請關注我們其它相關文章!