Implementation of translucent effect in Android application development

Source: Internet
Author: User

Here is an example of a custom activity translucent effect:
Res/values/styles.xml
<resources>
<stylename= "Transparent" >
<itemname= "Android:windowbackground" > @color/transparent_background</item>
<itemname= "Android:windownotitle" >true</item>
<itemname= "Android:windowistranslucent" >true</item>
<itemname= "Android:windowanimationstyle" >@+android:style/Animation.Translucent</item>
</style>
</resources>


Res/values/color.xml
<?xmlversion= "1.0" encoding= "Utf-8"?>
<resources>
<colorname= "Transparent_background" > #50000000 </color>
</resources>
Note: The #5000000 top two bits of the color.xml are transparent to the effect parameters from 00 to FF (transparent-not transparent), and the last 6 bits are the setting of the color manifest.xml
<activityandroid:name= ". Transparentactivity "
Android:theme= "@style/transparent"/>
Java code
Publicvoid onCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
SetTheme (r.style.transparent);
Setcontentview (r.layout.transparent);
}

Here is an example of the effect of using a system theme to achieve translucent activity:
Android provides a built-in theme for transparent effects: Theme (android:style/theme.translucent), if you want to achieve transparency, just set the Theme for your activity. If you want to achieve a translucent effect, you only need to inherit android:style/theme.translucent and rewrite it. Inherit android:style/theme.translucent and rewrite:
<?xmlversion= "1.0″encoding=" utf-8″?>
<resources>
<stylename= "Theme.translucent" parent= "Android:style/theme.translucent" >
<itemname= "Android:windowbackground" > @color/translucent_background</item>
<itemname= "Android:colorforeground" > #fff </item>
</style>
</resources>
Use this topic in Androidmainfest.xml:
<activityandroid:name= ". Translucent "android:label=" @string/app_name "
Android:theme= "@style/theme.translucent" >
<intent-filter>
<actionandroid:name= "Android.intent.action.MAIN"/>
<categoryandroid:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

Here is an example of the effect of implementing view translucency:
button or ImageButton background is set to transparent or translucent
Translucent: <button android:background= "#e0000000" .../>
Transparent: <button android:background= "#00000000" .../>
The color and opacity (alpha) values are represented by hexadecimal notation. The range of values for any one color is 0 to 255 (00 to FF)
。 For alpha,00, full transparency is expressed, and FF is 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
, you should specify the following values if you want to apply an opacity of 50% to a layer of overlay blue: 7fff0000
Set background picture transparency (super simple)
Java code
View v = Findviewbyid (r.id.content);//Find the ID of the layout you want to set the transparent background
V.getbackground (). Setalpha (100);//0~255 Transparency Value

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.