美文网首页
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性能优化(一)启动优化

    iOS App 启动性能优化WWDC之优化App启动速度 Instruments 包含的工具有很多,Time Pr...

  • iOS 性能优化三

    主要讲解APP冷启动的优化 iOS 性能优化一iOS 性能优化二iOS 性能优化三 1. APP 启动的分类 冷...

  • iOS性能优化 - 整理

    本文主要包含: 性能优化 - 卡顿性能优化 - 耗电优化性能优化 - APP启动优化安装包瘦身 一  性能优化 -...

  • MS(4):Android之性能优化篇

    六、性能及优化 1、App优化之性能分析工具 Android App优化之性能分析工具 2、ListView优化 ...

  • Android性能优化之启动速度优化

    Android性能优化之启动速度优化 Android app 启动速度优化,首先谈谈为什么会走到优化这一步,如果一...

  • 冷启动优化

    冷启动优化主要优化两个方面 Application 性能优化 App启动页性能优化业务优化不在本章优化范围内。本章...

  • App优化 - ANR优化

    前言 App优化 - 需要优化哪些? App优化 - 性能分析工具 App的3种启动方式 App优化 - App启...

  • Android系统原理

    Android性能优化(一)App启动原理分析及启动时间优化 - CSDN博客 Android性能优化(二)布局渲...

  • 收集_性能优化

    Android性能优化(一)之启动加速35%Android性能优化(二)之布局优化面面观Android性能优化(三...

  • iOS App 启动性能优化

    iOS App 启动性能优化

网友评论

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

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