android 启动页_App启动优化一顿操作猛如虎
前言
一個應用App的啟動速度能夠影響用戶的首次體驗,用戶希望應用能夠及時響應并快速加載。啟動時間過長的應用不能滿足這個期望,并且可能會令用戶失望。這種糟糕的體驗可能會導致用戶在應用商店針對您的應用給出很低的評分,甚至完全棄用您的應用。
本次主要內容包括:
針對App啟動優化我們做了哪些工作?
1、App啟動優化方向:視覺體驗優化
2、App啟動優化方向:代碼邏輯優化
一、App啟動優化方向:視覺體驗優化
App啟動時白屏問題
App啟動階段 :
加載并啟動應用程序。
啟動后立即顯示應用程序空白的啟動窗口。
創建應用程序進程。
啟動白屏的問題就是在1~2階段,因為App應用啟動都會先進入一個閃屏頁(SplashActivity) 來展示應用信息。我們可以通過設置啟動窗口的主題來優化視覺上出現的啟動白屏的問題。
1、默認主題
默認情況對App不做處理既設置了默認主題,App啟動初始化時會出現如下啟動時顯示白屏的情況,如下圖:
2、透明主題
為了解決啟動窗口白屏問題,通過設置啟動頁為透明主題來解,,雖然白屏沒了,但是我們的App似乎是變遲鈍了,仔細觀察一下,點擊App啟動圖標后,App似乎是頓了一下,然后加載了我們的歡迎頁面,有點像ANR,只不過很短暫,所以用戶體驗還是不佳,現象如下圖:
3、設置閃屏圖片主題
welcome_layler_drawable.xml源碼:
<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<itemandroid:id="@+id/welcome_background"android:drawable="@drawable/icon_splash_bg" />
<itemandroid:bottom="@dimen/dp_16"android:gravity="center">
<bitmapandroid:gravity="center_horizontal"android:src="@drawable/icon_splash_word" />
item>
<itemandroid:bottom="@dimen/dp_41"android:gravity="bottom">
<bitmapandroid:gravity="center_horizontal|bottom"android:src="@drawable/icon_splash" />
item>
layer-list>
二、App啟動優化方向:代碼邏輯優化
1、Application優化:
Application作為應用程序的整個初始化配置入口,有很多第三方組件(包括App應用本身)都在 Application 中做初始化操作,在Application中完成各種初始化操作和復雜的邏輯就會影響到應用的啟動性能
過多的初始化任務,考慮以下優化方案:
考慮異步初始化三方組件,不阻塞主線程;
延遲部分三方組件的初始化;
優化方案如下:
組件放到子線程中初始化:
new Thread(new Runnable() {@Override
public void run() {
setThreadPriority(THREAD_PRIORITY_BACKGROUND);
initARouter();
CacheManager.getInstance().initialize(getInstance());
ConnectionManager.getInstance().initialize();
initImageFactory();
initBJY();
initGrowingIO();
initUmeng();
initBugly();
initOkHttp();
initSobot();
setRxJavaErrorHandler();
}
}).start();
將需要在主線程中初始化但是可以不用立使用的控件功能延遲加載:
handler.postDelayed(new Runnable() {@Override
public void run() {
//延遲初始化組件
}
}, 3000);
注意:?并不是每一個組件的初始化以及操作都可以異步或延遲;是否可以取決組件的調用關系以及自己項目具體業務的需要。保證一個準則:可以異步的都異步,不可以異步的盡量延遲。讓應用先啟動,再操作。
//子線程初始化第三方組件//建議延遲初始化,可以發現是否影響其它功能,或者是崩潰!
Thread.sleep(5000);
2、閃屏Activity優化:
Activity的UI層級優化:
優化前UI布局:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@mipmap/icon_splash_bg">
<ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@mipmap/icon_splash_word"android:layout_centerVertical="true"android:layout_centerHorizontal="true"android:paddingBottom="160dp"
/>
<ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:src="@mipmap/icon_splash"android:layout_alignParentBottom="true"android:layout_marginBottom="@dimen/dp_41"
/>
<com.pxwx.student.modulecore.widget.TouchRelativeLayoutandroid:id="@+id/rl_adsRl"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center_horizontal|top"android:orientation="vertical" >
<ImageViewandroid:id="@+id/iv_SplashAd"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@null"android:contentDescription="@null"android:scaleType="fitXY"android:visibility="gone" />
com.pxwx.student.modulecore.widget.TouchRelativeLayout>
<TextViewandroid:id="@+id/tv_adjump"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/ad_jump_selector"android:gravity="center_vertical|center_horizontal"android:layout_alignParentRight="true"android:layout_marginRight="@dimen/dp_18"android:layout_marginTop="@dimen/dp_30"android:paddingBottom="@dimen/dp_5"android:paddingLeft="@dimen/dp_11"android:paddingRight="@dimen/dp_11"android:paddingTop="@dimen/dp_5"android:text="跳過 3"android:textColor="@color/white"android:textSize="@dimen/font_15"android:visibility="gone"
/>
RelativeLayout>
簡化后:
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/welcome_layler_drawable">
<ViewStubandroid:id="@+id/vs"android:layout_width="match_parent"android:layout_height="match_parent"android:layout="@layout/layout_stub_avd" />
FrameLayout>
ViewStub 初始化延遲
針對項目中的啟屏廣告業務,通過ViewStub延后他們的初始化,在需要顯示的時候通過ViewStub的inflate顯示真正的view,優化如下
android:id="@+id/vs"android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout="@layout/layout_stub_avd" />
開屏廣告業務布局抽取
layout_stub_avd.xml
<?xml version="1.0" encoding="utf-8"?><com.pxwx.student.modulecore.widget.TouchRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/rl_adsRl"android:layout_width="match_parent"android:layout_height="match_parent">
<ImageViewandroid:id="@+id/iv_SplashAd"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@null"android:contentDescription="@null"android:scaleType="fitXY" />
<TextViewandroid:id="@+id/tv_adjump"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_marginTop="@dimen/dp_30"android:layout_marginRight="@dimen/dp_18"android:background="@drawable/ad_jump_selector"android:gravity="center"android:paddingLeft="@dimen/dp_11"android:paddingTop="@dimen/dp_5"android:paddingRight="@dimen/dp_11"android:paddingBottom="@dimen/dp_5"android:text="跳過 3"android:textColor="@color/white"android:textSize="@dimen/font_15" />
com.pxwx.student.modulecore.widget.TouchRelativeLayout>
然后在代碼中需要顯示webview時進行inflate:
/*** 懶加載廣告視圖
*/
private void showAvd() {
viewStub = findViewById(R.id.vs);
if (viewStub != null) {
viewStub.inflate();
mAdRl = findViewById(R.id.rl_adsRl);
mAdImage = findViewById(R.id.iv_SplashAd);
mAdJump = findViewById(R.id.tv_adjump);
}
}
優化點:
廢棄之前的啟屏頁UI布局,直接使用先前自定義好的welcome_layler_drawable作為啟屏頁背景
將開屏廣告Ui抽取分離
懶加載廣告視圖
onCreate業務邏輯優化:
減少廣告等業務邏輯時間這里屬于業務邏輯的優化。
onCreate中針對廣告業務的初始化業務優化,異步下載圖片,等下次啟動控制展示
總結
通用應用啟動加速套路
利用主題快速顯示界面;
異步初始化組件;
梳理業務邏輯,延遲初始化組件、操作;
正確使用線程;
去掉無用代碼、重復邏輯等。
問題:
1、啟動速度的衡量指標啟動時間如何計算?
2、為什么啟動會有白屏?
3、為什么這樣優化是有效的?
總結
以上是生活随笔為你收集整理的android 启动页_App启动优化一顿操作猛如虎的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: rhel 8.2不识别unicode_基
- 下一篇: 内存压力测试软件_日常游戏,毫无压力,荣