android啓動頁優化小技巧(一)

      android應用啓動時默認會有黑屏/白屏的閃過,這是由於View加載時會先加載DectorView,然後由外層View向內層View逐層加載,
大概ViewGroup ->view -> subview...而每個View都會佔有一塊相應的矩形的區域,這樣View是有背景的,因此會有黑屏/白屏的閃
現,解決思路: 今早加載啓動頁面背景,不加載View背景就會避免該現象;

   
 《一》,通過設置主題來提前加載:
   當我們建立項目時習慣自定義一個Theme,在Manifest.xml文件中聲明使用:

   <application
        android:name=".application.BaseApplication"
        android:hardwareAccelerated="true"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/app_theme" ...>
   
       “ 在建立自定義主題時我們要注意,如果Activity我們繼承的AppCampatActivity時,Parent 主題樣式一定是
   Theme.AppCampat.系列的主題,否則報錯 ”
       
       自定義Style:
<resources> <!--app 主題 no ActionBar--> <style name="app_them" parent="Theme.AppCompat.Light.NoActionBar"> <!--默認所有window設置全屏模式--> <item name="android:windowFullscreen">true</item> <!--設置所有頁面不爲透明模式,開發半透明模式時使用該屬性--> <item name="android:windowIsTranslucent">false</item> <!--默認所有window窗口背景設爲透明無顏色--> <item name="android:windowBackground">@android:color/transparent</item> <!--動畫樣式--> <item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item> <!--window窗口設爲無標題模式,默認windowFullscreen=true,此方法不生效--> <item name="android:windowNoTitle">true</item> </style> <style name="LauncherTheme" parent="app_them"> <item name="android:state_window_focused">false</item> <!--啓動優化,不會產生白屏間隔快速顯示背景--> <item name="android:windowBackground">@mipmap/bg_default</item> </style> </resources>

我們可以先創建一個基礎樣式,"name = base_theme",然後不同的頁面fen別去擴展,parent="base_theme

       

";

《二》,通過代碼在運行時加載:

同時我們還可以通過java代碼去實現,如下:
     @Override
     protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.app_them);
        super.onCreate(savedInstanceState);
        //去除Title
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        //設置滿屏
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                                           WindowManager.LayoutParams.FLAG_FULLSCREEN);
  setContentView(R.layout.activity_launcher); infoManager = InfoManager.getInstance();
}



xzcv

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章