How does Android implement a fuzzy translucent window?

Source: Internet
Author: User

This example uses a custom theme style to implement a fuzzy translucent Activity.

1. Define the inventory file (AndroidManifest. xml)

<? Xml version = "1.0" encoding = "UTF-8"?>

<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"

Package = "my. android. test"

Android: versionCode = "1"

Android: versionName = "1.0" type = "codeph" text = "/codeph">

<! -- Use the android: theme attribute to set the theme style for the Activity -->

<Application android: icon = "@ drawable/icon" android: label = "@ string/app_name">

<Activity android: name = ". TranslucentBlur"

Android: label = "@ string/app_name"

Android: theme = "@ style/Theme. Transparent">

<Intent-filter>

<Action android: name = "android. intent. action. MAIN"/>

<Category android: name = "android. intent. category. LAUNCHER"/>

</Intent-filter>

</Activity>

</Application>

<Uses-sdk android: minSdkVersion = "9"/>

</Manifest>

2. Define string resources (values/strings. xml)

<? Xml version = "1.0" encoding = "UTF-8"?>

<Resources>

<String name = "hello"> Hello World, TranslucentBlur! </String>

<String name = "app_name"> TranslucentBlur </string>

<String name = "translucent_background"> Example of how you can make

Activity have a translucent background, compositing over

Whatever is behind it. </string>

</Resources>

3. Define the color resource used for plotting (values/colors. xml)

<? Xml version = "1.0" encoding = "UTF-8"?>

<Resources>

<Drawable name = "transparent_background"> #00000000 </drawable>

</Resources>

4. Define a fuzzy translucent theme style (values/styles. xml)

<? Xml version = "1.0" encoding = "UTF-8"?>

<Resources>

<! -- Apply the default theme style -->

<Style name = "Theme" parent = "android: Theme">

</Style>

<! -- Modify the theme style of the application so that the Activity has a background with transparent effects.

In this example, the Android built-in translucent theme is not used, but a fully customized translucent theme style is used.

-->

<Style name = "Theme. Transparent">

<! -- Set to allow window transparency -->

<Item name = "android: javaswistranslucent"> true </item>

<! -- Reference the animated translucent style of the Android System -->

<Item name = "android: windowAnimationStyle"> @ android: style/Animation. Translucent </item>

<! -- Set the background color of the window -->

<Item name = "android: windowBackground"> @ drawable/transparent_background </item>

<! -- Hide the window title bar -->

<Item name = "android: windowNoTitle"> true </item>

<! -- Set the text foreground -->

<Item name = "android: colorForeground"> # fff </item>

</Style>

</Resources>

5. Define the layout (translucent_blackground.xml)

<? Xml version = "1.0" encoding = "UTF-8"?>

<! -- Used to display the sample text -->

<TextView xmlns: android = "http://schemas.android.com/apk/res/android" android: id = "@ + id/text"

Android: layout_width = "match_parent" android: layout_height = "match_parent"

Android: gravity = "center_vertical | center_horizontal"

Android: text = "@ string/translucent_background"/>

6. Define the Activity class (TranslucentBlur. java)

Package my. android. test;

 

Import android. app. Activity;

Import android. OS. Bundle;

Import android. view. WindowManager;

 

Publicclass TranslucentBlur extends Activity {

/** Call this callback method when the Activity is created for the first time */

@ Override

Publicvoid onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

/**

* The getWindow () method gets the Window object of the current Activity. With this object, you can directly access the Window class

* API interface. In this example, the setFlags () method is called to set the layout parameter FLAG_BLUR_BEHIND to blur every component in the window.

*/

GetWindow (). setFlags (WindowManager. LayoutParams. FLAG_BLUR_BEHIND, WindowManager. LayoutParams. FLAG_BLUR_BEHIND );

// Fill in the Activity Layout

SetContentView (R. layout. translucent_background );

}

}


From FireOfStar's column

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.