Android应用开发中半透明效果实现方案

下面是自定义Activity半透明的效果例子:
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>
注意:color.xml的#5000000前两位是透明的效果参数从00 到 ff(透明--不么透明),后6位是颜色的设置manifest.xml
<activityandroid:name=".TransparentActivity"
android:theme="@style/Transparent"/>
java代码
publicvoid onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        setTheme(R.style.Transparent); 
        setContentView(R.layout.transparent);
}

下面是利用系统主题实现Activity半透明的效果例子:
Android为透明效果提供了内置的主题:Theme(android:style/Theme.Translucent),如果想实现透明效果,只要为Activity设置该Theme便可。如果想实现半透明效果,则只需要继承android:style/Theme.Translucent,并重写便可。继承android:style/Theme.Translucent并重写:
<?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>
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>
 
下面是实现View半透明的效果例子:
Button或者ImageButton的背景设为透明或者半透明
半透明:<Button android:background="#e0000000" ... />
透明:    <Button android:background="#00000000" ... />
颜色和不透明度 (alpha) 值以十六进制表示法表示。任何一种颜色的值范围都是 0 到 255(00 到 ff)
。对于 alpha,00 表示完全透明,ff 表示完全不透明。表达式顺序是“aabbggrr”,其中“aa=alpha”
(00 到 ff);“bb=blue”(00 到 ff);“gg=green”(00 到 ff);“rr=red”(00 到 ff)。例如
,如果您希望对某叠加层应用不透明度为 50% 的蓝色,则应指定以下值:7fff0000
设置背景图片透明度(超简单)
Java代码
View v = findViewById(R.id.content);//找到你要设透明背景的layout 的id
v.getBackground().setAlpha(100);//0~255透明度值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苦茶子12138

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值