Android解決APP啟動白屏

目錄

前言

當APP第一次啟動會有一段時間的白屏,而當代碼變多白屏的時間會更長,這對用戶體驗來說非常差,因此我們需要進行處理,讓用戶感覺不到有白屏的那個間隙。

效果對比

下面是我通過對Application的onCreate進行延時操作來模擬白屏

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        try {
            Thread.sleep(3000);
        }catch (Exception e){}
    }
}

下面是我通過處理解決白屏后的效果的對比


實現步驟

1.增加歡迎頁

增加一個歡迎頁(SplashActivity)

public class SplashActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
    }
}
2.給歡迎頁設置樣式

我們給SplashActivity設置單獨的樣式,我們重點是加了背景和設置狀態欄背景為白色狀態欄文字為黑色(因為演示的SplashActivity背景為白色所以設置狀態欄為白色比較好),并且用了沒有ActionBar的樣式

<style name="Theme.WhitePageDemoSplash" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>

        <!--  這三行是重點  -->
        <!--    設置背景,解決白屏的關鍵    -->
        <item name="android:windowBackground">@drawable/bg_splash</item>
        <!-- 設置狀態欄為白色的,狀態欄文字為黑的 -->
        <item name="android:statusBarColor" tools:targetApi="l">@color/white</item>
        <item name="android:windowLightStatusBar" tools:targetApi="m">true</item>
    </style>

AndroidManifest.xml中設置上

<application
        android:allowBackup="true"
        android:name=".MyApplication"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.WhitePageDemo">
        <activity android:name=".SplashActivity" android:theme="@style/Theme.WhitePageDemoSplash">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity" android:launchMode="singleTask" android:screenOrientation="portrait"/>
    </application>
3.給樣式設置背景

接下來是編寫背景的xml文件,我設置背景為白色,這也是與SplashActivity對應起來,然后加了一張全屏縮放的圖片,圖片的背景透明

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/white" />
        </shape>
    </item>

    <item>
        <bitmap
            android:gravity="fill"
            android:src="@drawable/splashlog" />
    </item>
</layer-list>
4.調整歡迎頁布局

歡迎頁的布局我們也用這張圖片,也是占滿整個布局并縮放,并且加了一個顯示”跳過“的TextView

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/white"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:src="@drawable/splashlog"
        android:scaleType="fitXY"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <TextView
        android:text="跳過"
        android:textSize="18sp"
        android:textColor="@color/black"
        android:layout_gravity="right"
        android:layout_marginTop="40dp"
        android:layout_marginRight="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</FrameLayout>

接下來我們看效果



我們發現頁面會向下移動一下,那是因為當SplashActivity的布局加載出來的時候,會有一塊狀態欄高度的下移,并且如果有虛擬鍵盤的話也會影響整個視覺效果的過渡,接下來我們只需要把SpalshActivity的布局調整為全屏即可

5.讓歡迎頁充滿全屏

這里給大家介紹個工具庫(https://github.com/Blankj/AndroidUtilCode),非常好用

 //工具類
implementation 'com.blankj:utilcodex:1.30.6'

具體操作如下:

public class SplashActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        //設置下布局向上移動狀態欄的高度
        BarUtils.setStatusBarColor(this, ColorUtils.getColor(R.color.white));
        //設置隱藏虛擬按鍵
        BarUtils.setNavBarVisibility(this,false);
    }
}

最后效果如下,可以看到是非常平滑的過渡


案例源碼

https://gitee.com/itfitness/white-page-demo

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容