App性能优化之启动优化

2019-01-08  本文已影响0人  Peakmain

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

产生的原因

容易产生黑白屏的地方

黑白屏解决办法

 <item name="android:windowBackground">@drawable/logo</item>

App启动页优化

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

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

<?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布局就是我们原本的主布局

       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();
 getWindow().getDecorView().post(new Runnable() {
            @Override
            public void run() {
                View mainView = mainLayout.inflate();
               //进行findviewbyid等操作
            }
        });
       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);
            }
        });
上一篇下一篇

猜你喜欢

热点阅读