转载: Android App 启动页(Splash)黑/白闪屏现象产生原因与解决办法   首先感谢博主分享,本文作为学习记录

惊鸿一瞥

微信的启动页,相信大家都不陌生。 
不知道大家有没有发现一个现象,微信每次启动的时候,是直接进入这个启动页面。 
我的意思是,很多应用,往往会先白屏停顿一下后再进入启动页面(Splash)。为了印证这一点,我把手机上所有的App都点了一遍。选几个例子 
如下图: 
微信: 
 
斗鱼: 
 
斗鱼和微信是直接进入了,他们的Splash页面。 
知乎: 
 
B站: 
 
知乎和B站要先进入一个白屏,特别是B站,白屏后再进入的Splash页面。

动手实现

想一想,如果让我们自己来写一个Splash页面怎么实现?

  1. 创建SplashActivity样式,我们需要他是启动界面,固定垂直方向,全屏显示
  <activity android:name=".MainActivity" />
<activity
android:name=".SplashActivity"
android:screenOrientation="portrait"
android:theme="@style/ThemeSplash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

  2.设置主题为ThemeSplash

  <style name="ThemeSplash" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">@android:color/white</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="windowActionBar">false</item>
//一定要,否则有状态栏显示,不能全屏
<item name="windowNoTitle">true</item>
</style>

  3.设置SplashActivity的布局文件 activity_splash.xml为背景图片的全屏显示

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/bg_splash"
tools:context="com.example.makeapp.SplashActivity">
</RelativeLayout>

  4.设置SplashActivity代码,延迟2秒跳转到MainActivity

public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
//延迟2S跳转
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}, 2000);
}
}

 
糟糕,出现了白屏。 
而且不止这个应用,把以前写的几个应用都打开看了下,都有出现黑白屏的现象,斗鱼不闪屏

看看斗鱼怎么做的

使用apktool打开斗鱼

>E:
>cd E:\Android\反编译\apktool
E:\Android\反编译\apktool>apktool d E:\Android\Blog\douyu.apk

先看他的配置清单文件

<activity android:name="tv.douyu.view.activity.SplashActivity"
android:screenOrientation="portrait" android:theme="@style/Theme.AppLauncher">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

因为是程序入口,很快找到,使用了Android:theme="@style/Theme.AppLauncher",那么看看他的样式Theme.AppLauncher怎么实现的

 <style name="Theme.AppLauncher" parent="@android:style/Theme.NoTitleBar.Fullscreen">
<item name="android:windowBackground">@drawable/bg_splash</item>
</style>

**看到这里,我们发现他居然把@drawable/bg_splash设置成了窗口背景,而这张叫bg_splash的图片就是他们的启动图片(我们的App已经拿过来用了),继续往下看 。 
找到他的SplashActivity布局文件,在他的res目录,根据命名规则他多半使用splash做关键字,搜索 

打开

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"> <RelativeLayout
     android:id="@id/ad_layout"
     android:layout_width="fill_parent"
  android:layout_height="fill_parent"
     android:visibility="gone"> <ImageView
android:id="@id/ad_img"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop" /> <RelativeLayout
android:id="@id/btn_skip"
android:layout_width="fill_parent"
android:layout_height="64.0dip"
android:layout_alignParentBottom="true"
android:background="#4f333333"
android:paddingLeft="24.0dip"
android:paddingRight="18.0dip"> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@drawable/splash_ad_logo" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/start_arrow_iv"
android:text="进入 "
android:textColor="@color/text_color_white"
android:textSize="15.0sp" /> <ImageView
android:id="@id/start_arrow_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/start_icon_anim" />
</RelativeLayout>
</RelativeLayout>
</LinearLayout>

几个关键词 进入 、@drawable/start_icon_anim@drawable/splash_ad_logo这不就是最开始,进入的广告页面么: 

只不过,他的这张图的大图背景,没有配置,很可能是网络获取的。

到这里,我们大致已经清楚了,斗鱼启动是怎么个逻辑

  1. 把启动图bg_splash设置为窗体背景,避免刚刚启动App的时候出现,黑/白屏
  2. 设置为背景bg_splash显示的时候,后台负责加载资源,同时去下载广告图,广告图下载成功或者超时的时候显示SplashActivity的真实样子
  3. 随后进入MainAcitivity 
    据我观察,淘宝启动的时候和斗鱼逻辑是一样的,有兴趣可以探究下。 
    到这里,偷师成功,我们可以回来改自己的程序了。
 <style name="ThemeSplash" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:background">@mipmap/bg_splash</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

 
好的已经不闪屏了。

为什么会这样

最后,思考一下这个问题,为什么会出现这种情况,在启动Acitivty的onCreate()方法里面,执行setContentView(R.layout.activity_splash);出现白屏。 
设想,onCreate---setContentView这个并不是发生在,窗体绘制的第一步,系统会在执行这个步骤之前,先绘制窗体,这时候布局资源还没加载,于是就使用默认背景色。

<style name="ThemeSplash" parent="Theme.AppCompat.Light">

这种亮色系,造成白色闪屏

<style name="ThemeSplash" parent="ThemeOverlay.AppCompat.Dark">

这种暗色系主题,造成了黑色闪屏

使用方法:

将下面的代码放入到工程下的style.xml  ,记得将下面的@mipmap/bg_splash 图片改成自己app的图片

<style name="ThemeSplash" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:background">@mipmap/bg_splash</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

然后将启动页面的theme改成上面的style ,就ok了~

<activity
android:name=".activity.WelcomeActivity"
android:configChanges="screenSize|keyboardHidden|orientation"
android:theme="@style/ThemeSplash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

代码地址:https://github.com/zhouruikevin/makeapp

Android App 启动页(Splash)黑/白闪屏现象产生原因与解决办法(转)的更多相关文章

  1. App启动页设计实例与技巧

    App启动页,也称闪屏页,最初是为缓解用户等待Web/iOS/Android App数据加载的焦虑情绪而出现,后被设计师巧妙用于品牌文化展示,服务特色介绍以及功能界面熟悉等平台进行设计,被赋予了更加丰 ...

  2. Android app启动是出现白屏或者黑屏如何解决?

    1.为什么 APP 启动时会出现白屏或者黑屏? 当打开一个 Activity 时,如果这个 Activity 所属的应用还没有在运行,系统会为这个 Activity 所属的应用创建一个进程,但进程的创 ...

  3. Android app启动出现白屏闪屏

    出现白屏闪屏原因: 进入到AppStartActivity,但是未加载到布局文件,就先显示了窗口的背景,白屏就是显示的windows的背景,即所设置的theme. onCreate()中的setCon ...

  4. [FMX] Android APP 启动黑屏优化补丁

    使用说明 *************************************************** Android APP 启动黑屏优化补丁 作者: Swish, YangYxd 201 ...

  5. android app启动就闪退怎么办?

    开发过程中,如遇到android app启动就闪退,不要急,直接进入调试模式运行app,就会取得出错的原因. http://blog.sina.com.cn/s/blog_44fa172f0102wg ...

  6. React-Native App启动页制作(安卓端)

    原文地址:React-Native App启动页制作(安卓端) 这篇文章是根据开源项目react-native-splash-screen来写的.在使用react-native-link命令安装该包后 ...

  7. App启动页倒计时功能

    转载请注明出处:http://www.cnblogs.com/cnwutianhao/p/6753418.html 示例代码采用 RxJava + RxLifecycle + Data-Binding ...

  8. 用代码获取APP启动页图片

    用代码获取APP启动页图片 源码 - swift // // AppleSystemService.swift // Swift-Animations // // Created by YouXian ...

  9. Android app启动activity并调用onCreate()方法时都默默地干了什么?

    Android app启动activity并调用onCreate() 方法时都默默地干了什么?   在AndroidManifest.xml文件中的<intent-filter>元素中有这 ...

随机推荐

  1. 【jetty】Jetty与Tomcat的区别

    Jetty 的架构从前面的分析可知,它的所有组件都是基于 Handler 来实现,当然它也支持 JMX.但是主要的功能扩展都可以用 Handler 来实现.可以说 Jetty 是面向 Handler ...

  2. hdu 3986(最短路变形好题)

    Harry Potter and the Final Battle Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/6553 ...

  3. Cryptography I 学习笔记 --- 认证加密

    1. 认证加密,Alice与Bob共享一个密钥k,Alice可以发送密文E给Bob,Bob可以确定接收到的E一定是拥有密钥k的Alice产生的.而不是攻击者随便产生的. 2. 认证加密必须能抵挡住选择 ...

  4. es6 解构写法:给变量取别名

    在变量后面加一个: var {f: foo} = {f: 5}; foo == 5 // true

  5. Codeforces 777D Cloud of Hashtags(贪心)

    题目链接 Cloud of Hashtags 题目还是比较简单的,直接贪心,但是因为我有两个细节没注意,所以FST了: 1.用了cin读入,但是没有加 std::ios::sync_with_stdi ...

  6. P1243~P1247 线段树模板题总结

    前言 这几天刚刚刷了5道线段树(水)题,现在来总结一下. 首先是犯的不少错误: 1.建树.更新函数没有return.这是最气的,每次最后程序错误查了半天也没查出来,最后发现是没有return.递归边界 ...

  7. iOS5可能会删除本地文件储存

    文/ Nick (iphoneincubator) 关于iOS 5的本地文件储存Marco(Instapaper 的开发者)写过一篇很好的帖子阐述过相关问题,有兴趣的同学可以先阅读下他的文章然后再看下 ...

  8. Android Spinner In Toolbar

    As the title of the post suggest in this tutorial we will see how to have spinner widget inside the ...

  9. js 日期计算星座 根据生日的月份和日期,一行代码计算星座的js小函数(转)

    本博客根据 开源中国作者清风徐不来 的文章 根据生日的月份和日期,一行代码计算星座的js小函数(转) 原文出自CSDN 无心的专栏 的文章,知识产权归原文作者所有! 点击查看原文:js 日期计算星座

  10. EasyMvc入门教程-基本控件说明(4)折叠面板

    折叠面板一般出现在管理后台,大家用的OutLook里就是用了折叠面板,样子大概是这样的: 把其中的内容替换成图标按钮,是不是就是我们常见的样子了?:)那么如何实现呢?请看例子: @{ var data ...