app啓動頁優化

有不少app啓動頁仍是老一套的splashactivity,而後跳轉到mainactivity,首先splash加載了一遍數據,而後進入到main以後也會加載數據,因此致使兩次加載都要等待,若是反過來呢java

這裏能夠先加載mainactivity,而後在去觸發splashactivity,固然,這樣也有缺陷,有的main中可能加載數據量大,會致使進入splash以前卡頓,因此通常是在同一個佈局中,把splashactivity變成splashfragmentandroid

這樣佈局中默認的就是splash頁了,而後main中的數據也在同時獲得了加載,等splash結束後通常main已經初始化徹底了,因此不用在次加載,能夠直接顯示出來,這一步就節省了大量的等待時間,比其它花裏胡哨的優化實用有效多了git

 

實現也比較簡單,在你原來的MainActivity佈局中添加一層佈局github

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <variable
            name="model"
            type="com.strong.ui.MainViewModel" />
    </data>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <androidx.viewpager2.widget.ViewPager2
                android:id="@+id/viewPager"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp_0"
                app:layout_constraintBottom_toTopOf="@+id/lin_bottom"
                app:layout_constraintTop_toTopOf="parent" />

            <com.strong.ui.view.menu.BottomMenuView
                android:id="@+id/lin_bottom"
                android:layout_width="@dimen/dp_0"
                android:layout_height="wrap_content"
                android:background="@color/white"
                android:clipChildren="false"
                android:clipToPadding="false"
                android:orientation="horizontal"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent" />

        </androidx.constraintlayout.widget.ConstraintLayout>

        <FrameLayout
            android:id="@+id/fl_splash"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:visibility="gone" />

    </FrameLayout>
</layout>
View Code

而後把splashfragment動態添加到容器中app

 

這樣啓動頁就默認顯示執行了ide

而後模擬加載數據完成後關閉啓動頁,splashfragment移除後直接顯示mainactivity佈局oop

class SplashFragment : BaseBindFragment<FragmentSplashBinding, SplashViewModel>() {

    override fun layoutId() = R.layout.fragment_splash

    override fun providerVMClass() = SplashViewModel::class.java

    override fun initData(bundle: Bundle?) {
        binding.model = mViewModel
        //模擬加載圖片
        var count = 0f
        val mHandler = Handler(Looper.getMainLooper())
        val mRunnable = object :Runnable {
            override fun run() {
                count+=20
                binding.pbTime.setProgress(count)
                if (count >= binding.pbTime.getMax()) {
                    mHandler.removeCallbacks(this)
                    activity!!.window.setBackgroundDrawableResource(R.color.white)
                    //移除啓動頁
                    activity!!.supportFragmentManager.beginTransaction().remove(this@SplashFragment).commitAllowingStateLoss()
                }else{
                    mHandler.postDelayed(this,500)
                }
            }
        }
        mHandler.postDelayed(mRunnable,500)
    }

}
View Code

若是mainactivity中加載量過大在優化本身的啓動加載流程,這樣是否是加載不卡頓也省了加載時間佈局

github:https://github.com/1024477951/KotlinStrongpost

相關文章
相關標籤/搜索