2、透明主题
为了解决启动窗口白屏问题,通过设置启动页为透明主题来解,,虽然白屏没了,但是我们的App似乎是变迟钝了,仔细观察一下,点击App启动图标后,App似乎是顿了一下,然后加载了我们的欢迎页面,有点像ANR,只不过很短暂,所以用户体验还是不佳,现象如下图:
3、设置闪屏图片主题
welcome_layler_drawable.xml源码:
<?x![外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传](https://img-home.csdnimg.cn/images/20230724024159.png?origin_url=C%3A%5CUsers%5Chh%E6%98%AF%E6%88%91%E5%95%8A%5CDesktop%5CCache_57f5cc0f73c34d73.jpg&pos_id=img-IGZ4yRwc-1718738369196) 资料获取→专栏 ml version="1.0" encoding="utf-8"?><item
android:id=“@+id/welcome_background”
android:drawable=“@drawable/icon_splash_bg” />
<item
android:bottom=“@dimen/dp_16”
android:gravity=“center”>
<bitmap
android:gravity=“center_horizontal”
android:src=“@drawable/icon_splash_word” />
<item
android:bottom=“@dimen/dp_41”
android:gravity=“bottom”>
<bitmap
android:gravity=“center_horizontal|bottom”
android:src=“@drawable/icon_splash” />
二、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”>
<ImageView
android: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”
/>
<ImageView
android: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.TouchRelativeLayout
android:id=“@+id/rl_adsRl”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:gravity=“center_horizontal|top”
android:orientation=“vertical” >
<ImageView
android: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>
<TextView
android: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”
/>
简化后:
<?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”>
<ViewStub
android:id=“@+id/vs”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:layout=“@layout/layout_stub_avd” />
ViewStub 初始化延迟
针对项目中的启屏广告业务,通过ViewStub延后他们的初始化,在需要显示的时候通过ViewStub的inflate显示真正的view,优化如下
<ViewStub
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”>
<ImageView
android:id=“@+id/iv_SplashAd”
android:layout_widt
h=“match_parent”
android:layout_height=“match_parent”
android:background=“@null”
android:contentDescription=“@null”
android:scaleType=“fitXY” />
<TextView
android: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” />
id: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” />