博客
关于我
通过设置Activity的Theme来优化App启动白屏问题
阅读量:786 次
发布时间:2019-03-25

本文共 1398 字,大约阅读时间需要 4 分钟。

通过设置Activity的Theme优化App启动白屏问题

在App的冷启动过程中,系统会首先加载AppTheme设置的WindowBackground。默认情况下,系统主题会设置为白屏(Light)或黑屏(Dark),随后才会启动App的主流程。如果App的onCreate()操作耗时较长,大量用户可能会在启动过程中看到一段白屏。针对此问题本文将详细介绍如何通过设置Activity的Theme来优化启动白屏问题。

针对启动白屏问题,我们可以采取以下优化方案:

  • 通过设置启动Activity的Theme为App本身的图片或颜色,从感官层面让用户立即感受到App的加载进度。
  • 对于方案1,由于其与App的具体业务逻辑密切相关,本文不予展开。方案2和方案3则相对行之有效,尤其是方案3本文将重点实践。以下将详细介绍方案3的实现方法。

    当我们在styles.xml中为SplashTheme设置drawable资源时,可以通过如下方式为启动Activity设置自定义背景:

    在drawable资源中,我们可以使用layer-list布局文件来实现上述效果:

    在设置SplashTheme后,我们需要在AndroidManifest.xml中将其应用于默认启动Activity:

    在主Activity中,我们需要正确设置Activity的切换效果,以确保主题切换时的流畅性。此外,在SplashActivity的onCreate()方法中,建议使用postDelayed来延迟启动主要Activity:

    @Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_splash);    View view = getWindow().getDecorView();    int startDelay = 1000;    view.postDelayed(new Runnable() {        @Override        public void run() {            // 开始加载主Activity            Intent intent = new Intent(SplashActivity.this, MainActivity.class);            startActivity(intent);            // 延迟关闭当前Activity            view.postDelayed(SplashActivity.this::finish, 1000);        }    }, startDelay);}

    通过以上配置,你将能够从视觉上向用户展示一个已加载的界面,从而有效减少启动白屏的时间。虽然这不是性能优化,但效果依然出色。

    使用此种方案,你可以根据实际需求调整启动延迟时间和主题美化效果,以营造最佳的用户体验。

    转载地址:http://akluk.baihongyu.com/

    你可能感兴趣的文章
    no available service ‘default‘ found, please make sure registry config corre seata
    查看>>
    No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>
    no session found for current thread
    查看>>
    no such file or directory AndroidManifest.xml
    查看>>
    No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
    查看>>
    NO.23 ZenTaoPHP目录结构
    查看>>