转载: 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. derby数据库的一些总结

     本文主要是针对在osgi开发过程中的一些问题进行总结,其中dbcp数据源的配置是在SpringDM下配置的.一,derby数据源的内嵌模式       该模式的主要应用是嵌入式程序,因为其小巧,且不 ...

  2. macro expand error

    cat test_macro.c #define TEST_MACRO(b) chip->##b int main(void) { TEST_MACRO(yyy) return 0; } gcc ...

  3. git使用代理clone加速

    不设置代理10kb/s不到....,设置后,500kb/s左右跑- 开shadowsocks,代理127.0.0.1:1080 编写一个脚本 /YOUR PATH/gitproxy.sh #!/bin ...

  4. eWebEditor不支持IE7以上版本Bug修改

    修改: \Include\Editor.js //把此行 if (element.YUSERONCLICK) eval(element.YUSERONCLICK + "anonymous() ...

  5. AC日记——[Ahoi2009]Seq 维护序列seq bzoj 1798

    1798 思路: 维护两个标记: 乘:m  和  加:a 先下放乘,再下放加: 下放乘的时候要把子节点的加一块乘了: 开long long: 来,上代码: #include <cstdio> ...

  6. AC日记——N的倍数 51nod 1103

    1103 N的倍数 思路: 先计算出前缀和: 然后都%n: 因为有n个数,所以如果没有sum[i]%n==0的化,一定有两个取模后的sum相等: 输出两个sum中间的数就好: 来,上代码: #incl ...

  7. Linux Performance Observability Tools

  8. ASP.NET MVC创建静态页

    1.在MVC下新建一个类:StaticPageHelper public class StaticPageHelper { /// <summary> /// 根据View视图生成静态页面 ...

  9. Ruby on rails初体验(三)

    继体验一和体验二中的内容,此节将体验二中最开始的目标来实现,体验二中已经将部门添加的部分添加到了公司的show页面,剩下的部分是将部门列表也添加到公司的显示页面,整体思路和体验二中相同,但是还是会有点 ...

  10. 每天学一点Python(2)

    9月16日(python扩展的安装和使用) 接着上一篇继续.按照之前计划,先分析导出的数据,再做进一步统计. 导出的数据是html类型的,想到的处理方法有: 1.直接readlines然后一行一行找我 ...