<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"
/>
</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">
<ViewStub
android: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,优化如下
<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_width="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" />
</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中针对广告业务的初始化业务优化,异步下载图片,等下次启动控制展示。通用应用启动加速套路,利用主题快速显示界面;异步初始化组件;梳理业务逻辑,延迟初始化组件、操作;正确使用线程;去掉无用代码、重复逻辑等。