Android 启动页 - chuwuwang/ReadingNote GitHub Wiki

1. 只显示一次启动页( App 没被 kill 的情况下)

描述:微信打开之后,按下返回键回到桌面,再打开微信,并不会再看到启动页(除非手动清了微信的后台或者被系统 kill 了)

@Override
public void onBackPressed() {
    // super.onBackPressed(); // 不要调用父类的方法
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addCategory(Intent.CATEGORY_HOME);
    startActivity(intent);
}

2. 全屏页面切换到非全屏页面的问题

描述:由于启动页一般是全屏显示的,而主页则不是,因此从全屏切换到非全屏就存在一个卡顿的问题

private void smoothSwitchScreen() {
    // 5.0以上修复了此bug
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        ViewGroup rootView = (ViewGroup) this.findViewById(android.R.id.content);
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        int statusBarHeight = getResources().getDimensionPixelSize(resourceId);
        rootView.setPadding(0, statusBarHeight, 0, 0);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    }
}

该方法在切换之后的activity的setContentView() 方法之前调用,就可以解决全屏切换到非全屏的问题。(必须在此之前)

3. 过度绘制的问题

过度绘制的相关的 API 是隐藏的,集中在 SystemProperties 类中,可以通过如下代码设置:

SystemProperties.set(HardwareRenderer.DEBUG_OVERDRAW_PROPERTY, "show");

注意:直接编译源码拿到没隐藏的 jar 包,暂时能调用到该类,但是运行之后发现需要系统权限才能设置。 adb 指令设置:

// 开启『调试 GPU 过度绘制』
adb shell setprop debug.hwui.overdraw show
// 关闭『调试 GPU 过度绘制』
adb shell setprop debug.hwui.overdraw false

参考文章:https://jaeger.itscoder.com/