Flutter應(yīng)用啟動頁設(shè)置,解決 白/黑 屏情況

解決啟動白屏或黑屏

出現(xiàn)此情況的原因有兩種

  1. FlutterView顯示第一幀之前,安卓會加載flutter的SDK,將dart代碼加載在內(nèi)存中,過程中android沒有可以顯示的東西,出現(xiàn)白屏。(android 方面原因)


    初始化過程.png

解決:
找到 \app\src\main\res\drawable\launch_background.xml 文件,這個(gè)里面初始化了布局標(biāo)簽,只需要把圖片替換為我們自己的就可以。

<?xml version="1.0" encoding="utf-8"?>
<!-- Modify this file to customize your launch splash screen -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/white" />

    <!-- You can insert your own image assets here -->
    <!-- <item>
        <bitmap
            android:gravity="center"
            android:src="@mipmap/launch_image" />
    </item> -->

<!--@drawable/launch_screen就是圖片路徑 可以在當(dāng)前同文件夾下 放置要顯示的圖片-->
     <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/launch_screen" /> 
    </item> 
</layer-list>

或者根據(jù)不同手機(jī)的分辨率 在mipmap下放置圖片例如:


轉(zhuǎn)載.png

之后前往 styles.xml 文件設(shè)置啟動頁

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
    <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:windowFullscreen">true</item>-->
<!--        設(shè)置啟動背景 引入啟動頁 xml-->
        <item name="android:windowBackground">@drawable/launch_background</item>
<!--        啟動時(shí)狀態(tài)欄狀態(tài)是否透明-->
        <item name="android:windowIsTranslucent">true</item>

    </style>
</resources>

重新打包就可以看到 剛剛設(shè)置的啟動頁了
效果例如:
[圖片上傳失敗...(image-7e5c2-1586668143446)]

  1. 從現(xiàn)象觀察,啟動頁中間有一段時(shí)間黑屏,這個(gè)為什么呢?前面我們說過,F(xiàn)lutter的啟動流程分成兩部分,一部分是Android啟動階段,一個(gè)是Flutter的啟動階段,這個(gè)黑屏就是Flutter的啟動階段沒有啟動頁所造成的。我們從源碼入手,詳細(xì)分析一下,下面是FlutterActivityDelegate的部分源碼。參考鏈接
    解決:找到 \app\src\main\AndroidManifest.xml 文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.flutterxc">
    <!-- io.flutter.app.FlutterApplication is an android.app.Application that
         calls FlutterMain.startInitialization(this); in its onCreate method.
         In most cases you can leave this as-is, but you if you want to provide
         additional functionality it is fine to subclass or reimplement-->
    <application
        android:usesCleartextTraffic="true"
        android:name="io.flutter.app.FlutterApplication"
        android:label="flutterxc" //自定義app名稱
        android:icon="@mipmap/xc_logo" //自定義app logo
>
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
    // 添加這兩段代碼 可解決 黑屏問題(出現(xiàn)紅線 或者 異常沒關(guān)系,可以編譯成功)
          + <meta-data
          +     android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
          +     android:value="true" />
       // 將資源指向我們的啟動頁路徑
          +  <meta-data
          +     android:name="io.flutter.embedding.android.SplashScreenDrawable"
          +     android:resource="@drawable/launch_background" />

            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>

        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>

至此可以流暢的打開啟動頁了

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容