大龙的博客

常用链接

统计

最新评论

Android UI 优化-使用theme 预加载

在很多时候,我们需要给一个Layout设置一个背景。例如,我们下下面的layout中使用了这样一个背景:

01<?xml version=”1.0″ encoding=”utf-8″?>
02<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”    android:orientation=”vertical”
03android:layout_width=”fill_parent”
04android:layout_height=”fill_parent”
05android:background=”@drawable/antelope_canyon”>
06<TextView android:text=”@+id/TextView01″
07android:id=”@+id/TextView01″
08android:layout_width=”wrap_content”
09android:layout_height=”wrap_content” >
10</TextView>
11 
12</LinearLayout>

其中的LinearLayout使用了 背景图片antelope_canyon。

如果仔细观察程序的运行过过程,我们首先看到了黑色的activity背景,然后才看到背景图被加载,那是因为在activity start以后,我们才能调用setContentView设置我们的layout,然后才绘制我们在layout中放置的背景图。而在此之前,程序中绘 制的是android中默认黑色背景。 这样会给用户感觉我们的activity启动较慢。
然而,如果将背景图定义在一个主题中,如下:

1<?xml version=”1.0″ encoding=”utf-8″?>
2<resources>
3<style name=”Theme.Droidus” parent=”android:Theme”>
4<item name=”android:windowBackground”>@drawable/antelope_canyon</item>
5<item name=”android:windowNoTitle”>true</item>
6</style>
7</resources>

然后在activity中使用这个主题 :

01<?xml version=”1.0″ encoding=”utf-8″?>
02<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
03package=”com.droidus”
04android:versionCode=”1″
05android:versionName=”1.0″>
06<application android:icon=”@drawable/icon” android:label=”@string/app_name”>
07<activity android:name=”.SpeedUpStartupActivity”
08android:label=”@string/app_name”
09android:theme=”@style/Theme.Droidus”
10>
11<intent-filter>
12<action android:name=”android.intent.action.MAIN” />
13<category android:name=”android.intent.category.LAUNCHER” />
14</intent-filter>
15</activity>
16 
17</application>
18<uses-sdk android:minSdkVersion=”4″ />
19 
20</manifest>

运行程序,可以看到背景图马上显示了,没有再看到黑色的背景图。

为什么会有这样的现象呢?那是因为 程序的主题是在程序启动的时候加载的,而不是在activity启动之后加载!
而如果在layout使用背景,背景图是在activity启动之后才加载,故而会让用户看到一个黑色背景闪动的过程。

posted on 2013-01-15 00:22 大龙 阅读(553) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理