版權聲明:本文爲HaiyuKing原創文章,轉載請註明出處!android
StartingWindow 的處理方式:git
-------摘自《知乎 救救你的 StartingWindow》github
android開發者應該都有這樣的體會:開發到必定的階段,包變得很大了,每次啓動APP的時候,老是有白屏一閃而過(白屏的時間和包的大小成正比!)。瀏覽器
-------摘自《Android APP啓動白屏優化》微信
本文講的是第二種處理方式。app
暫無ide
注意事項:佈局
一、 導入類文件後須要change包名以及從新import R文件路徑優化
二、 Values目錄下的文件(strings.xml、dimens.xml、colors.xml等),若是項目中存在,則複製裏面的內容,不要整個覆蓋動畫
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <!-- 應用啓動頁(StartingWindow)的theme --> <style name="AppTheme.StartingWindowTheme" parent="AppTheme"> <!-- 能夠設置成純顏色(設置一個和Activity UI類似的背景) --> <!--<item name="android:windowBackground">@color/startingwindow_bgcolor</item>--> <!--也能夠設置成一張圖片 --> <item name="android:windowBackground">@drawable/startingwindow_bg</item> </style> </resources>
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#FF4081</color> <!-- 應用啓動頁(StartingWindow)的theme的背景色 --> <color name="startingwindow_bgcolor">#00bfff</color> </resources>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.why.project.androidstartingwindowdemo"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <!--將首頁的them設置成自定義的樣式--> <activity android:name=".MainActivity" android:theme="@style/AppTheme.StartingWindowTheme"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>
package com.why.project.androidstartingwindowdemo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTheme(R.style.AppTheme);//恢復原有的樣式 setContentView(R.layout.activity_main); } }
擴展:若是在styles.xml文件中將啓動頁窗口背景圖片(android:windowBackground)設置爲歡迎界面的背景圖片,而後歡迎界面佈局文件中將背景(android:background)設置爲透明,Activity中不恢復原有的樣式,那麼這樣就能夠實現APP啓動後白屏部分和歡迎界面是同一張背景圖片。
須要注意,若是將以前歡迎界面的背景圖片做爲窗口背景圖片,那麼就須要考慮到底部導航欄的高度的問題。不然背景圖片的底部會被遮蓋住。因此可能須要從新調整歡迎界面的背景圖片。
無