Implementation Scheme of translucent effect in Android Application Development

Source: Internet
Author: User

The mobile phone screen is too small, and the translucent effect is often used to increase the visible range. We will share with you the following translucent implementation methods!

The following is an example of the effect of translucent custom Activity:

Res/values/styles. xml

<resources>    <style name="Transparent ">     <item name="android:windowBackground">@color/transparent_background</item>      <item name="android:windowNoTitle">true</item>      <item name="android:windowIsTranslucent">true</item>        <item name="android:windowAnimationStyle">@+android:style/Animation.Translucent</item>    </style>  </resources>

Res/values/color. xml

<?xml version="1.0" encoding="utf-8"?>  <resources>    <color name="transparent_background">#50000000</color>  </resources>

Note: For color. xml #5000000, the first two parameters are transparent, from 00 to ff (transparent-not transparent), and the last six parameters are set colors.

Manifest. xml

<activity android:name=".TransparentActivity"android:theme="@style/Transparent" />

Java code

public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setTheme(R.style.Transparent);           setContentView(R.layout.transparent);  }

The following is an example of the effect of using the system topic to achieve Activity Translucent:

Android provides the built-in Theme (android: style/Theme. Translucent) for transparent effects. To achieve transparent effects, you only need to set the Theme for the Activity.

To achieve a Translucent effect, you only need to inherit android: style/Theme. Translucent and rewrite it.

Inherit android: style/Theme. Translucent and rewrite:

<?xml version=”1.0″ encoding=”utf-8″?><resources><style name=”Theme.Translucent” parent=”android:style/Theme.Translucent”><item name=”android:windowBackground”>@color/translucent_background</item><item name=”android:colorForeground”>#fff</item></style></resources>

This topic is used in AndroidMainfest. xml:

<activity android:name=”.Translucent” android:label=”@string/app_name”android:theme=”@style/Theme.Translucent”><intent-filter><action android:name=”android.intent.action.MAIN” /><category android:name=”android.intent.category.LAUNCHER” /></intent-filter></activity>

 

The following is an example of a translucent View:

The background of a Button or ImageButton is transparent or translucent.

Translucent: <Button android: background = "# e0000000".../>

Transparency: <Button android: background = "#00000000".../>

The color and opacity (alpha) values are expressed in hexadecimal notation. The value range of any color is 0 to 255 (00 to ff ). For alpha, 00 indicates completely transparent, and ff indicates completely opaque. The expression order is "aabbggrr", where "aa = alpha" (00 to ff); "bb = blue" (00 to ff ); "gg = green" (00 to ff); "rr = red" (00 to ff ). For example, if you want to apply a blue opacity of 50% to a stack, specify the following value: 7fff0000

Set the background image transparency (ultra-simple)

Java code

  1. View v = findViewById (R. id. content); // locate the id of layout where you want to set a transparent background

  2. V. getBackground (). setAlpha (100); // 0 ~ 255 transparency Value

Related Article

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.