Home > Web Front-end > JS Tutorial > body text

A brief discussion on three methods of setting transparency and darkness on Android

little bottle
Release: 2019-04-27 10:58:09
forward
3455 people have browsed it

This article describes three methods of setting transparency, translucency, and darkness using Android. It has certain reference value. Interested friends can learn about it.

There are roughly three types of settings for transparency effects

1. Use the transparent effect of the android system

Java code

android:background="@android:color/transparent"
Copy after login

For example, set the button

Java code

Copy after login

2. Use ARGB to control

Java code

//半透明
<Button android:background="#e0000000" /> 
//透明
<Button android:background="#00000000" />
Copy after login

Related tutorials: Java video tutorial

3. Set alpha

Java code

View v = findViewById(R.id.content);//找到你要设透明背景的layout 的id 
v.getBackground().setAlpha(100);//0~255透明度值
Copy after login

android form transparency, darkness, etc. setting tips

Set transparency (this is the transparency of the form itself, not the background)

WindowManager.LayoutParams lp=getWindow().getAttributes();
lp.alpha=0.3f;
getWindow().setAttributes(lp);
Copy after login

alpha is between 0.0f and 1.0f. 1.0 is completely opaque, 0.0f is completely transparent

Set the darkness

WindowManager.LayoutParams lp=getWindow().getAttributes();
lp.dimAmount=0.5f;
getWindow().setAttributes(lp);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Copy after login

dimAmount is between 0.0f and 1.0f, 0.0f is not dark at all, 1.0f is completely dark

Activity’s transparent and translucent effect settings transparent

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>
Copy after login

res/values/color.xml

<?xml version="1.0" encoding="utf-8"?>  
<resources>  
  <color name="transparent_background">#50000000</color>  
</resources>  
//注意:
//color.xml的#5000000前两位是透明的效果参数从00--99(透明--不怎么透明),
//后6位是颜色的设置
Copy after login

manifest.xml

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

java code

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

The above configuration is completed, I hope it will be helpful to you.

Related tutorials: Android video tutorial

The above is the detailed content of A brief discussion on three methods of setting transparency and darkness on Android. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template