在android 程序中,当某一个活动启动滞后可能需要使用背景透明的效果,本例用于描述背景透明的应用。
参考示例程序:TranslucentActivity(ApiDemo=>App=>Activity=>Translucent)
TranslucentBlurActivity(App=>Activity=>TranslucentBlur)
源代码:com/example/android/apis/app/TranslucentActivity.java
com/example/android/apis/app/TranslucentBlurActivity.java
样式文件:values/styles.xml
AndroidManifest.xml 中的定义如下所示:
<activity android:name=".app.TranslucentActivity"
android:label="@string/activity_translucent"
android:theme="@style/Theme.Translucent“ >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter>
</activity>
<activity android:name=".app.TranslucentBlurActivity"
android:label="@string/activity_translucent_blur"
android:theme="@style/Theme.Transparent“ >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter>
</activity>
这两个程序使用的都是窗口透明的效果,
TranslucentActivity 获得的效果是背
景普通的透明,TranslucentBlurActivity 获得的效果是背景模糊的透明。它们的样
式被设置成了 Translucent ,这是一个用于描述背景透明的自定义样式,在
styles.xml 中定义。
<style name="Theme.Translucent" parent="android:style/Theme.Translucent">
<item name="android:windowBackground">
@drawable/translucent_background
</item>
<item name="android:windowNoTitle">true</item>
<item name="android:colorForeground">#fff</item>
</style>
translucent_background 值 用 于 设 置 窗 口 的 背 景 为 透 明 , 同 时 设 置 了
windowNoTitle 表示窗口不包含标题栏。
TranslucentBlurActivity 之所以能够获得背景模糊的效果,是因为在源代码中
进行了进一步的设置,如下所示:
public class TranslucentBlurActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
123
getWindow().
setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
setContentView(R.layout.translucent_background);
}
}
设置模糊效果是通过窗口管理器(WindowManager)设置参数来完成的,这
种设置只有在背景设置为透明后才能显示效果。