美文网首页
App性能优化之启动优化

App性能优化之启动优化

作者: Peakmain | 来源:发表于2019-01-08 21:43 被阅读0次

    黑白屏产生的原因和解决方法

    产生的原因

    • 1、还没加载到布局文件,就已经显示了window窗口背景
    • 2、黑屏白屏就是window窗口背景

    容易产生黑白屏的地方

    • Application的onCreate()中逻辑过于复杂
    • Activity的onCreate()中的setContentView之前过于耗时操作或者布局过于复杂

    黑白屏解决办法

    • 1、避免在加载布局文件之前在主线程中执行耗时操作
    • 2、在主题中给Window设置背景
     <item name="android:windowBackground">@drawable/logo</item>
    

    App启动页优化

    如果MainActivity中有大量的初始化操作,从SplashActivity(启动页)中直接进入主界面,用户会需要进行长时间的等待,这种用户体验会很不好,
    解决办法

    • 1、把SplashActivity改成SplashFragment
    • 2、在SplashFragment显示的时间内进行网络数据缓存等工作
    • 3、采用ViewStub的形式加载activity_main的布局

    通常写法我就不写了,直接写我们自己的代码
    修改后主布局:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#99CC33"
        android:orientation="vertical"
        tools:context=".SplashActivity">
    
        <ViewStub
            android:id="@+id/main_content_viewstub"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout="@layout/main_layout" />
    
    
        <FrameLayout
            android:id="@+id/splash_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    </RelativeLayout>
    

    main_layout布局就是我们原本的主布局

    • 1.显示Splash界面
           splashFragment = new SplashFragment();
            mainLayout = (ViewStub) findViewById(R.id.main_content_viewstub);
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.splash_container, splashFragment);
            fragmentTransaction.commit();
    
    • 2、开始实现业务逻辑,数据初始化工作,比如,我们有RecyclerView进行加载数据操作。这里代码我就不贴了
    • 3、加载主体布局
     getWindow().getDecorView().post(new Runnable() {
                @Override
                public void run() {
                    View mainView = mainLayout.inflate();
                   //进行findviewbyid等操作
                }
            });
    
    • 4、延时一段时间,显示我们主界面(去除Splash界面)
           getWindow().getDecorView().post(new Runnable() {
                @Override
                public void run() {
    
                    mHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
    
                            FragmentManager fragmentManager = getSupportFragmentManager();
                            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                            fragmentTransaction.remove(splashFragment);
                            fragmentTransaction.commit();
    
                        }
                    }, 3000);
                }
            });
    

    相关文章

      网友评论

          本文标题:App性能优化之启动优化

          本文链接:https://www.haomeiwen.com/subject/ryqirqtx.html