首頁  >  文章  >  web前端  >  淺談Android設定透明度、黑暗度的三種方法

淺談Android設定透明度、黑暗度的三種方法

little bottle
little bottle轉載
2019-04-27 10:58:093385瀏覽

本篇文章講述的是用Android設定透明、半透明、黑暗度的三種方法,具有一定的參考價值,有興趣的朋友可以了解一下。

設定透明效果大概有三種

1、用android系統的透明效果

Java程式碼 

android:background="@android:color/transparent"

例如設定按鈕

Java程式碼 

2、用ARGB來控制

Java程式碼 

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

相關教學:Java影片教學

#3、設定alpha

Java程式碼

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

android 表單透明的,黑暗度等的設定技巧

設定透明度(這是表單本身的透明度,非背景)

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

alpha在0.0f到1.0f之間。 1.0完全不透明,0.0f完全透明

設定黑暗度

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

dimAmount在0.0f和1.0f之間,0.0f完全不暗,1.0f全暗

#Activity的透明、半透明效果的設定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>

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位是颜色的设置

manifest.xml

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

java程式碼

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

以上配置結束,希望對你有幫助。

相關教學:Android影片教學

以上是淺談Android設定透明度、黑暗度的三種方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:51cto.com。如有侵權,請聯絡admin@php.cn刪除