Android啟動頁優化之實現應用秒開
Android 應用冷啟動時,需要從Application開始啟動,加載時間就會比較長,這段時間里,用戶所能看到的就是”白屏“(這是因為默認的AppTheme的 android:windowBackground 默認是設置成白色的),因此我認為真正的啟動頁就應該是讓用戶點開應用時看到的不是”白屏“,而是我們創建的一個頁面,可以是一張圖片、一段文字。這樣,不明真相的用戶直觀感覺到的就是,這個應用可以秒開。
1.首先在 drawable 目錄下新建一個 splash_screen.xml 文件<?xml version='1.0' encoding='utf-8'?><layer-list xmlns:android='http://schemas.android.com/apk/res/android' android:opacity='opaque'> <item android:drawable='@color/colorPrimary'/> <item><bitmap android:src='http://www.aoyou183.cn/bcjs/@drawable/ic_logo' android:gravity='center'/> </item></layer-list>
我們使用 layer-list 標簽創建一個圖層列表,實際就是一個 LayerDrawable ,設置一個背景,然后放上應用圖標,這是我想展示的啟動頁,可以根據自己的需要自行定義。
2.然后在 style.xml 文件中定義一個 SplashTheme<resources> ...<style name='SplashTheme' parent='AppTheme'><item name='android:windowBackground'>@drawable/splash_screen</item> </style></resources>
這里只需要將窗口背景設置為我們剛才定義的 LayerDrawable。
3.然后需要在 AndroidMenifest.xml 文件中將我們的主頁面,我這里是 MainActivity 的 android:theme 設置成我們定義的SplashTheme<?xml version='1.0' encoding='utf-8'?><manifest xmlns:android='http://schemas.android.com/apk/res/android' xmlns:tools='http://schemas.android.com/tools' ... > ... <application... ><activity android:name='.activity.MainActivity' android:launchMode='singleTask' android:theme='@style/SplashTheme'> <intent-filter><action android:name='android.intent.action.MAIN' /><category android:name='android.intent.category.LAUNCHER' /> </intent-filter></activity>... </application></manifest>
是不是很簡單這樣就可以了
以上就是Android啟動頁優化之實現應用秒開的詳細內容,更多關于Android 實現應用秒開的資料請關注好吧啦網其它相關文章!
相關文章:
