zoukankan      html  css  js  c++  java
  • Android开发之解决APP启动白屏或者黑屏闪现的问题

    在做搜芽的过程中,发现那个外包人缘做的不行,由于启动的时候会停顿,然后白屏一会,联想到几个月前我在我的三僚企业通信软件里面拉起9K-Mail的时候也会黑屏,所以决定学习一下。解决一下。这不,万能的网络还是很实用的。

    在这里总结一下。

    (參考及转载:http://www.2cto.com/kf/201409/339293.html)

    欢迎页启动的线程由于请求和处理的数据量过大而,导致欢迎页在出现之前界面上会有一个短暂的白色闪屏停留。当然白色闪屏的停留是由于 application 的主题样式android:theme=@style/AppTheme 使用了 Theme.Light 题导致的,Light 样式的 windowBackground、colorBackground、colorForeground 等属性的值均为 light 也就是白色偏亮,所以才会出现白色闪屏。以下是我的 APP 出现白色闪屏时样式引用的代码:

    ?
    1
    <style name="AppTheme"parent="android:Theme.Light"type="text/css"><br><br>  </style>

    简单的改动后。闪屏颜色为黑色。代码例如以下:

    ?

    1
    <style name="AppTheme"parent="android:style/Theme.Black.NoTitleBar.Fullscreen"type="text/css">  </style>

    代码改动后引用的样式为黑色主题,但欢迎页仍然会有黑色闪屏短暂的停留。继续进行改动,设置透明属性为 true。代码例如以下:

    ?
    1
    <style name="AppTheme"parent="android:style/Theme.Black.NoTitleBar.Fullscreen"type="text/css"><item name=android:windowIsTranslucent>true</item></style>

    经过这次的改动之后黑色闪屏现象消失了,终于达到了自己理想的效果。最后,经过查阅资料发现已经有人总结和处理过这类问题了,而且给出了优缺点的分析,我在这里以我的理解对其进行引用。

    原来避免黑色闪屏有2种方法,分别为:1.为 Theme 设置背景图;2.为 Theme 设置透明属性。显然我採用的是另外一种方式,先分别看看这2种方式所引用的代码:

    ?

    1
    <!-- 为 Theme 设置背景图 --><style name="AppTheme"parent="android:style/Theme.Black.NoTitleBar.Fullscreen"type="text/css"><item name=android:windowBackground>@drawable/splash_bg</item></style>
    ?

    1
    <!-- 为 Theme 设置透明属性 --><style name="AppTheme"parent="android:style/Theme.Black.NoTitleBar.Fullscreen"type="text/css"><item name=android:windowIsTranslucent>true</item></style>

    上面的2种 Theme 中,为 Theme 设置背景图后程序在启动的时候,会首先显示这张图,避免发生黑屏;为 Theme 设置透明属性,程序启动后不会黑屏而是透明。等到界面初始化完毕后才一次性显示出来。以下是两种方式的优缺点:

    • 为 Theme 设置背景图 给人程序启动快的感觉,界面先显示背景图。然后再刷新其它界面控件。刷新不同步。
    • 为 Theme 设置透明属性 给人程序启动慢的感觉,界面会一次性刷出来,刷新同步。

      可是问题有出现了,原先在配置了activity的切换动画效果。设置完android:windowIsTranslucent=true之后切换动画失效了。临时我也不知道android系统的theme属性之间关系的错综复杂,继承来继承去的。

      。。为什么会出现这样的问题。只是还好无意间找到了解决的方法

       

      ?

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      <style name="AppTheme"parent="AppBaseTheme"type="text/css"><!-- <item name=android:windowAnimationStyle>@style/Animation.Activity.Style</item> -->
              <item name=android:windowAnimationStyle>@style/Animation.Activity.Translucent.Style</item>
              <item name=android:windowBackground>@android:color/transparent</item>
              <item name=android:windowIsTranslucent>true</item></style><style name="Animation.Activity.Style"parent="@android:style/Animation.Activity"type="text/css"><item name=android:activityOpenEnterAnimation>@anim/base_slide_right_in</item>
              <item name=android:activityOpenExitAnimation>@anim/base_stay_orig</item>
              <item name=android:activityCloseEnterAnimation>@anim/base_stay_orig</item>
              <item name=android:activityCloseExitAnimation>@anim/base_slide_right_out</item>
              <item name=android:taskOpenEnterAnimation>@anim/base_slide_right_in</item>
              <item name=android:taskOpenExitAnimation>@anim/base_stay_orig</item>
              <item name=android:taskCloseEnterAnimation>@anim/base_stay_orig</item>
              <item name=android:taskCloseExitAnimation>@anim/base_slide_right_out</item>
              <item name=android:taskToFrontEnterAnimation>@anim/base_slide_right_in</item>
              <item name=android:taskToFrontExitAnimation>@anim/base_stay_orig</item>
              <item name=android:taskToBackEnterAnimation>@anim/base_stay_orig</item>
              <item name=android:taskToBackExitAnimation>@anim/base_slide_right_out</item></style><style name="Animation.Activity.Translucent.Style"parent="@android:style/Animation.Translucent"type="text/css"><item name=android:windowEnterAnimation>@anim/base_slide_right_in</item>
              <item name=android:windowExitAnimation>@anim/base_slide_right_out</item></style>

      配置style继承的parent为

       

       

      ?
      1
      2
      <style name="Animation.Activity.Translucent.Style"parent="@android:style/Animation.Translucent"type="text/css"><item name=android:windowEnterAnimation>@anim/base_slide_right_in</item>
              <item name=android:windowExitAnimation>@anim/base_slide_right_out</item></style>

      然后让apptheme的
      ?
      1
      android:windowAnimationStyle为上面的style

       

       

      ?
      1
      2
      3
      <style name="AppTheme"parent="AppBaseTheme"type="text/css"><item name=android:windowAnimationStyle>@style/Animation.Activity.Translucent.Style</item>
              <item name=android:windowBackground>@android:color/transparent</item>
              <item name=android:windowIsTranslucent>true</item></style>

      假设想在全部的activity切换时候使用该theme,能够配置application,也能够单个单个配置在activity里面。

       

      当然首页退出的时候能够单独配置MainActivity的退出动画和进入动画

       

      ?

      1
      <style name="Animation.Activity.Translucent.Style.Main"parent="@android:style/Animation.Translucent"type="text/css"><item name=android:windowExitAnimation>@anim/slide_right_out</item></style>

       

      MainActivity的退出和进入动画能够引用系统提供的,可是好像在style里面配置引用不了有些系统的anim,

      在目录sdkplatformsandroid-20data es以下。activity_open_enter.xml,activity_close_exit.xml能够直接复制到项目中。改动

      单独写一个进入或者退出。然后其他默认。。。

      。。。。

       

      多动手,測试,假设有错误的地方麻烦留言一起交流,谢谢

    还有一篇值得看的文章:(http://blog.csdn.net/u012970411/article/details/16981441)

    前几天Boss就反应说,机器每次启动程序都会闪一下黑屏,这个客户不接受。

    没办法,仅仅能想想怎么解决,最后找到了以下的方法。闪黑屏的原因主要是我们启动Activity的时候,须要跑完onCreate和onResume才会显示界面。

    也就是说须要处理一些数据后,才会显示。依照这样的思路,是不是我把初始化的工作尽量降低就能够避免黑屏?事实是,就算你onCreate啥都不做。仍然会闪一下黑屏。由于初始化解析界面时须要一定时间。以下是解决的方法:

    (PS:新建的QQ群,有兴趣能够增加一起讨论:Android群:322599434)  

    1、自己定义Theme

    复制代码
    
    
    //Edited by mythou
    //http://www.cnblogs.com/mythou/
    //1、设置背景图Theme
    <style name="Theme.AppStartLoad" parent="android:Theme">  
        <item name="android:windowBackground">@drawable/ipod_bg</item>  
        <item name="android:windowNoTitle">true</item>  
    </style>
    
    //2、设置透明Theme
    <style name="Theme.AppStartLoadTranslucent" parent="android:Theme">  
        <item name="android:windowIsTranslucent">true</item> 
        <item name="android:windowNoTitle">true</item>  
    </style>
    复制代码

      上面我定义了两种Theme,第一种Theme就是设置一张背景图。当程序启动时。首先显示这张背景图,避免出现黑屏。另外一种Theme是把样式设置为透明,程序启动后不会黑屏而是整个透明了,等到界面初始化完才一次性显示出来。以下说说两种方式的优缺点:

    • Theme1 程序启动快,界面先显示背景图,然后再刷新其它界面控件。

      给人刷新不同步感觉。

    • Theme2 给人程序启动慢感觉。界面一次性刷出来,刷新同步。

     

    2、改动AndroidManifest.xml

    为了使上面Theme生效。我们须要设置一些Activity的Theme

    复制代码
    //Edited by mythou
    //http://www.cnblogs.com/mythou/
    <application
        android:allowBackup="true"
        android:icon="@drawable/ipod_icon"
        android:label="@string/app_name"
        android:launchMode="singleTask">
    
    <!-- iPod主界面 -->
    <activity
        android:name="com.apical.apicalipod.IPodMainActivity"
      <!-- 使用上面定义的样式 mythou-->
        android:theme="@style/Theme.AppStartLoad"
        android:label="@string/app_name" >
        <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    
    //......
    
    </application>
    复制代码
    • 能够在Activity里面添加上面自己定义的样式。

      另外在Application里面添加也是能够的,并且是全局效果。

    • 自己定义Theme放在 /res/values/styles.xml 里面。假设没有这个文件,自己加入一个就可以。
    • 假设存在多个Activity切换。中间也可能会存在短暂黑屏问题。原因也是Activity启动的时候须要初始化载入数据。假设想避免这样的情况,能够在你切换的Activity里面添加上面的样式。
    • 上面两种样式都能够避免黑屏。

      能够实际測试一下你的程序选择一种效果。

    • 这个仅仅是把黑屏避免了,可是假设你程序初始化启动慢。还是会给人程序启动慢的感觉。须要自行优化程序初始化过程。

     

    3、Theme属性具体解释

    复制代码
    
    
    //Edited by mythou
    //http://www.cnblogs.com/mythou/
    android:theme="@android:style/Theme.Dialog" //Activity显示为对话框模式
    
    android:theme="@android:style/Theme.NoTitleBar" //不显示应用程序标题栏
    
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" //不显示应用程序标题栏,并全屏
    
    android:theme="Theme.Light " //背景为白色
    
    android:theme="Theme.Light.NoTitleBar" //白色背景并无标题栏
    
    android:theme="Theme.Light.NoTitleBar.Fullscreen" //白色背景,无标题栏,全屏
    
    android:theme="Theme.Black" //背景黑色
    
    android:theme="Theme.Black.NoTitleBar" //黑色背景并无标题栏
    
    android:theme="Theme.Black.NoTitleBar.Fullscreen" //黑色背景,无标题栏。全屏
    
    android:theme="Theme.Wallpaper" //用系统桌面为应用程序背景
    
    android:theme="Theme.Wallpaper.NoTitleBar" //用系统桌面为应用程序背景。且无标题栏
    
    android:theme="Theme.Wallpaper.NoTitleBar.Fullscreen" //用系统桌面为应用程序背景。无标题栏,全屏
    
    android:theme="Theme.Translucent" //透明背景
    
    android:theme="Theme.Translucent.NoTitleBar" //透明背景并无标题
    
    android:theme="Theme.Translucent.NoTitleBar.Fullscreen" //透明背景并无标题。全屏
    
    android:theme="Theme.Panel " //面板风格显示
    
    android:theme="Theme.Light.Panel" //平板风格显示
    复制代码

     

    4、Theme和Style

      Android里面除了Theme外还有Style。比如以下是Launcher里面配置workspace的一个Style

    复制代码
    //Edited by mythou
    //http://www.cnblogs.com/mythou/
      <style name="WorkspaceIcon">
            <item name="android:layout_width">match_parent</item>
            <item name="android:layout_height">match_parent</item>
            <item name="android:layout_gravity">center</item>
            <item name="android:gravity">center_horizontal</item>
            <item name="android:singleLine">true</item>
            <item name="android:ellipsize">marquee</item>
            <item name="android:textSize">12sp</item>
            <item name="android:textColor">#FFF</item>
            <item name="android:shadowRadius">2.0</item>
            <item name="android:shadowColor">#B0000000</item>
        </style>
    复制代码

    Style能够理解为一组属性集合。方便不同的View设置使用。我们在View里面使用Style的时候,跟使用Theme是一样的应用方法。那么Style和Theme有什么差别?以下列出两者差别:

    • 样式用在单独的View。如:Button、TextView等
    • 主题通过AndroidManifest.xml中的<application>和<activity>用在整个应用或者某个 Activity,主题对整个应用或某个Activity存在全局性影响。

    • 假设一个应用使用了主题,同一时候应用下的view也使用了样式,那么当主题与样式属性发生冲突时,样式的优先级高于主题。

      上面就是通过Theme解决程序启动闪黑屏问题,而且解说了Theme和Style,通过Theme配置,事实上还能够做个欢迎页面。只是我们都希望程序启动速度越快越好。因此还是须要多多优化自己的程序。

     

    Edited by mythou

    原创博文。转载请标明出处:http://www.cnblogs.com/mythou/p/3196042.html 



  • 相关阅读:
    Xna小游戏开发【飞机空间大战】 碧血黄沙
    参考XNA官方Platformer模版,修改Platformer为横版可以滚动的小游戏 碧血黄沙
    云计算「半步巅峰」
    get_custom_field使用
    CCTM_FormElement 类
    php opendir说明
    js(chrome)打印对象函数
    operamasks.omItemSelector获取右边框ID方法
    {$vo.CreateDate|date="ymd H:i:s",###}
    添加xmlns:android="http://schemas.android.com/apk/res/android"的意思
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/5417379.html
Copyright © 2011-2022 走看看