美文网首页
Android性能优化-App启动优化

Android性能优化-App启动优化

作者: 河马过河 | 来源:发表于2018-09-13 19:38 被阅读34次

    一、App启动优化

    问题如下:
    1、App启动白屏、或者黑屏
    2、App启动时间长

    二、解决方法1

    通常的解决办法都是给Activity设置一个透明背景的主题

    <style name="SplashTheme" parent="AppTheme">
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowIsTranslucent">true</item>
    </style>
    

    结果如下:

    优点:确实没有白屏和黑屏了
    缺点:1、给SplashActivity设置后,用户点击我们APP图标后,需要等待2秒左右的时候才会显示contentView。造成了APP启动速度慢的假象,其实Activity已经启动了,只是background是透明的,这时候你点击桌面的其他地方是无效的。这样就和Google的初衷背道而驰了
    2、给其他Activity设置后,会导致通过overridePendingTransition设置的启动关闭Activity的动画无效。需要在style中重新写如下几个动画:

    <style name="AppTheme" parent="AppBaseTheme">
    <item name="android:windowAnimationStyle">@style/Animation.Activity.Translucent.Style</item>
    <item name="android:windowFullscreen">true...
    <item name="android:windowIsTranslucent">true...
    </style>
    
    <style name="Animation.Activity.Style" parent="@android:style/Animation.Activity">
    <item name="android:activityOpenEnterAnimation">...
    <item name="android:activityOpenExitAnimation">...
    <item name="android:activityCloseEnterAnimation">...
    <item name="android:activityCloseExitAnimation">...
    </style>
    
    <style name="Animation.Activity.Translucent.Style" parent="@android:style/Animation.Translucent"> 
    <item name="android:windowEnterAnimation">...
    <item name="android:windowExitAnimation">...
    </style>  
    

    3、Activity之间的跳转可能偶尔会看到桌面一闪而过(如果SplashActivity和其他Activity都设置了透明)

    三、解决方式2

    给主题设置Window背景图片

    <style name="SplashTheme" parent="AppBaseTheme">
        <!-- 欢迎页背景引用刚才写好的 -->
        <item name="android:windowBackground">@drawable/splash</item>
        <item name="android:windowFullscreen">true</item>
        <!-- <item name="android:windowIsTranslucent">true</item> --> <!-- 透明背景不要了 -->
    </style>
    

    结果如下:

    优点:确实解决了白屏、黑屏问题
    缺点:华为手机HUAWEI MT7-TL00出现bug-图片先显示了一次模糊的、带黑圈的圆角图片,再显示了一次全屏清晰图片

    四、解决方式3

    给主题设置Window背景图片

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- 背景颜色 -->
        <item android:drawable="@color/white" />
    
        <item>
            <!-- 图片 -->
            <bitmap
                android:gravity="center"
                android:src="@drawable/wel_page" />
        </item>
    </layer-list>
    <style name="SplashTheme" parent="AppBaseTheme">
        <!-- 欢迎页背景引用刚才写好的 -->
        <item name="android:windowBackground">@drawable/splash</item>
        <item name="android:windowFullscreen">true</item>
        <!-- <item name="android:windowIsTranslucent">true</item> --> <!-- 透明背景不要了 -->
    </style>
    
    

    优点:确实解决了白屏、黑屏问题
    缺点:华为手机HUAWEI MT7-TL00出现bug-图片确实只显示了一次图片,但是图片确实被截取后的图片,并没有显示全图

    五、最终解决方式

    在方法4的基础上修改

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@android:color/white" />
        <item>
            <bitmap
                android:gravity="fill"
                android:src="@drawable/bg_splash" />
        </item>
    </layer-list>
    <style name="SplashTheme" parent="AppBaseTheme">
        <!-- 欢迎页背景引用刚才写好的 -->
        <item name="android:windowBackground">@drawable/splash</item>
        <item name="android:windowFullscreen">true</item>
        <!-- <item name="android:windowIsTranslucent">true</item> --> <!-- 透明背景不要了 -->
    </style>
    

    参考:
    https://blog.csdn.net/yanzhenjie1003/article/details/52201896:严振杰

    河马过河微信公众号.jpg

    相关文章

      网友评论

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

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