Flutter啟動頁(閃屏頁)的具體實(shí)現(xiàn)及原理詳析
為什么要有啟動頁?
在以下文章中,啟動頁就是閃屏頁。
現(xiàn)在大部分App都有啟動頁,那么為什么要有啟動頁?這是個值得思考的問題,如果沒有啟動頁會怎樣,大部分的App會白屏(也有可能是黑屏,主題設(shè)置有關(guān)系)非常短的時間,然后才能展示App的內(nèi)容。
那么問題來了,一定要有啟動頁嗎?答案:不是,而且是盡可能不要有啟動頁,因?yàn)閱禹摃層脩趔w驗(yàn)不夠連貫,甚至IOS在開發(fā)手冊上就不推薦使用啟動頁。
我們深入思考一下,既然不推薦為什么這樣流行,答案非常簡單,啟動頁的成本非常低,如果你想把的App啟動優(yōu)化到一個非常短的時間,還是有一定成本的。
Android啟動流程
為什么要談Android的啟動流程呢?因?yàn)镕lutter啟動的時候,依賴的是Android的運(yùn)行環(huán)境,其本質(zhì)是Activity上添加了一個FlutterView,F(xiàn)lutterView繼承SurfaceView,那么就容易理解了,F(xiàn)lutter的全部頁面都是渲染到了FlutterView上,如果不熟悉Flutter的啟動流程可以參考Flutter啟動流程 這篇文章,下面是對Flutter啟動的一個簡單描述。
在Flutter中,啟動頁的作用是在FlutterView顯示第一幀之前,不要出現(xiàn)白屏,在FlutterView顯示第一幀之前,我們分成兩個階段,Android啟動階段和Flutter啟動階段,Android啟過程添加啟動頁非常容易,在主題xml中添加android:windowBackground屬性,F(xiàn)lutter怎么添加啟動頁呢?其實(shí)框架已經(jīng)幫助咱們實(shí)現(xiàn)好了,我下面就給大家說一下原理。
Flutter啟動頁具體實(shí)現(xiàn)和原理
創(chuàng)建一個SplashActivity,這Activity繼承FlutterActivity,重寫onCreate()方法,在onCreate()方法中調(diào)用GeneratedPluginRegistrant.registerWith()
,下面是啟動頁的代碼。
public class SplashActivity extends FlutterActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); GeneratedPluginRegistrant.registerWith(this); } }
在Manifest中添加SplashActivity作為App的啟動Activity,設(shè)置SplashActivity的主題是LaunchTheme。下面是Manifest的配置文件。
<activity android:name=".SplashActivity" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:launchMode="singleTop" android:theme="@style/LaunchTheme" android:windowSoftInputMode="adjustResize"> <meta-data android:name="io.flutter.app.android.SplashScreenUntilFirstFrame" android:value="true" /> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
meta-data的name = "io.flutter.app.android.SplashScreenUntilFirstFrame"
的value一定要設(shè)置成true,一定要設(shè)置成true,一定要設(shè)置成true重要的事情說三遍,如果這個屬性設(shè)置成false,效果是這樣的。
從現(xiàn)象觀察,啟動頁中間有一段時間黑屏,這個為什么呢?前面我們說過,F(xiàn)lutter的啟動流程分成兩部分,一部分是Android啟動階段,一個是Flutter的啟動階段,這個黑屏就是Flutter的啟動階段沒有啟動頁所造成的。我們從源碼入手,詳細(xì)分析一下,下面是FlutterActivityDelegate的部分源碼。
public final class FlutterActivityDelegate implements FlutterActivityEvents, FlutterView.Provider, PluginRegistry { private static final String SPLASH_SCREEN_META_DATA_KEY = "io.flutter.app.android.SplashScreenUntilFirstFrame"; private View launchView; @Override public void onCreate(Bundle savedInstanceState) { String[] args = getArgsFromIntent(activity.getIntent()); FlutterMain.ensureInitializationComplete(activity.getApplicationContext(), args); flutterView = viewFactory.createFlutterView(activity); if (flutterView == null) { FlutterNativeView nativeView = viewFactory.createFlutterNativeView(); flutterView = new FlutterView(activity, null, nativeView); flutterView.setLayoutParams(matchParent); activity.setContentView(flutterView); launchView = createLaunchView();//1 if (launchView != null) { addLaunchView();//2 } } } private View createLaunchView() { if (!showSplashScreenUntilFirstFrame()) {//3 return null; } final Drawable launchScreenDrawable = getLaunchScreenDrawableFromActivityTheme(); final View view = new View(activity); view.setBackground(launchScreenDrawable); return view; } private Drawable getLaunchScreenDrawableFromActivityTheme() { //省略了部分代碼 try { return activity.getResources().getDrawable(typedValue.resourceId); } catch (NotFoundException e) { return null; } } private Boolean showSplashScreenUntilFirstFrame() { try { ActivityInfo activityInfo = activity.getPackageManager().getActivityInfo( activity.getComponentName(), PackageManager.GET_META_DATA|PackageManager.GET_ACTIVITIES); Bundle metadata = activityInfo.metaData; return metadata != null && metadata.getBoolean(SPLASH_SCREEN_META_DATA_KEY); } catch (NameNotFoundException e) { return false; } } private void addLaunchView() { activity.addContentView(launchView, matchParent);//4 flutterView.addFirstFrameListener(new FlutterView.FirstFrameListener() {//5 @Override public void onFirstFrame() { FlutterActivityDelegate.this.launchView.animate() .alpha(0f) .setListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { ((ViewGroup) FlutterActivityDelegate.this.launchView.getParent()) .removeView(FlutterActivityDelegate.this.launchView);//5 } }); } }); activity.setTheme(android.R.style.Theme_Black_NoTitleBar); } }
注釋1
這個段代碼很容易理解,創(chuàng)建一個LaunchView,主要邏輯在createLaunchView()中,原理也很簡單,根據(jù)主題中的R.attr.windowBackground
屬性,生成一個Drawable,然后創(chuàng)建了一個View,并且把這個View的背景設(shè)置成Drawable。
注釋3
showSplashScreenUntilFirstFrame()是得到Manifet中io.flutter.app.android.SplashScreenUntilFirstFrame
的屬性的值,如果是false,那么久返回一個空的的LaunchView,也就不會執(zhí)行注釋2的代碼。這就是我們上面說的如果設(shè)置成false就顯示黑屏的原因。
注釋2
調(diào)用addLaunchView(),這方法也很簡單,首先看注釋4,把LaunchView添加到當(dāng)前的Activity中,然后添加了一個監(jiān)聽,在注釋5處,這個監(jiān)聽是當(dāng)FlutterView第一幀加載完成后回調(diào),回調(diào)做了什么事情呢?很簡單,把LaunchView刪除了,顯示FlutterView的第一幀。
總結(jié)一下,就是把Android的啟動頁生成一個Drawable,創(chuàng)建了一個LaunchView,把Drawable設(shè)置成LaunchView的背景,當(dāng)前的Activity添加這LaunchView,如果FlutterView的第一幀顯示了,把LaunchView刪除。
設(shè)置主題,下面是LaunchTheme的代碼。
<resources> <style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar"> <!-- Show a splash screen on the activity. Automatically removed when Flutter draws its first frame --> <item name="android:windowBackground">@drawable/launch_background</item> </style> </resources>
下面是launch_background的代碼。
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque"> <item> <bitmap android:src="@mipmap/ic_launch_bg" /> </item> <item android:width="90dp" android:height="90dp" android:gravity="center"> <bitmap android:src="@mipmap/ic_launch_logo" /> </item> </layer-list>
最終效果如下,沒有黑屏,非常順滑。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。
- 使用Flutter開發(fā)的抖音國際版實(shí)例代碼詳解
- Flutter進(jìn)階之實(shí)現(xiàn)動畫效果(一)
- Flutter 超實(shí)用簡單菜單彈出框 PopupMenuButton功能
- flutter InkWell實(shí)現(xiàn)水波紋點(diǎn)擊效果
- 詳解Flutter WebView與JS互相調(diào)用簡易指南
- Flutter持久化存儲之?dāng)?shù)據(jù)庫存儲(sqflite)詳解
- Flutter中如何加載并預(yù)覽本地的html文件的方法
- flutter日期選擇器 flutter時間選擇器
- 詳解flutter之網(wǎng)絡(luò)請求dio,請求,攔截器簡單示例
- Flutter實(shí)現(xiàn)抖音點(diǎn)贊效果
相關(guān)文章
Android實(shí)現(xiàn)新增及編輯聯(lián)系人的方法
這篇文章主要介紹了Android實(shí)現(xiàn)新增及編輯聯(lián)系人的方法,是Android應(yīng)用開發(fā)常見的功能,需要的朋友可以參考下2014-07-07Android實(shí)現(xiàn)倒計時結(jié)束后跳轉(zhuǎn)頁面功能
最近在工作中遇到一個需求,需要在倒計時一段時間后進(jìn)行跳轉(zhuǎn)頁面,通過查找相關(guān)資料發(fā)現(xiàn)其中涉及的知識還不少,所以分享出來,下面這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)倒計時結(jié)束后跳轉(zhuǎn)頁面功能的相關(guān)資料,需要的朋友可以參考下。2017-11-11Retrofit2.0 實(shí)現(xiàn)圖文(參數(shù)+圖片)上傳方法總結(jié)
本篇文章主要介紹了Retrofit2.0 實(shí)現(xiàn)圖文(參數(shù)+圖片)上傳方法總結(jié),具有一定的參考價值,有興趣的可以了解一下2017-08-08Android巧用ViewPager實(shí)現(xiàn)左右循環(huán)滑動圖片
這篇文章主要為大家詳細(xì)介紹了Android巧用ViewPager實(shí)現(xiàn)左右循環(huán)滑動圖片的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-05-05Android ViewFlipper的詳解及實(shí)例
這篇文章主要介紹了Android ViewFlipper的詳解及實(shí)例的相關(guān)資料,通過本文希望能幫助大家理解這部分內(nèi)容,需要的朋友可以參考下2017-08-08android使用handler ui線程和子線程通訊更新ui示例
這篇文章主要介紹了android使用handler ui線程和子線程通訊更新ui的方法,大家參考使用吧2014-01-01Android百度地圖應(yīng)用之創(chuàng)建顯示地圖
這篇文章主要為大家詳細(xì)介紹了Android百度地圖應(yīng)用之創(chuàng)建顯示地圖,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-06-06Android實(shí)現(xiàn)擴(kuò)展Menu的方法
這篇文章主要介紹了Android實(shí)現(xiàn)擴(kuò)展Menu的方法,涉及Android操作menu菜單的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10