设置透明效果
- android:background=”@android:color/transparent”
- 用ARGB来控制
半透明 android:background=”#e0000000”
透明 android:background=”#00000000” - 设置alpha
View view = findViewById(R.id.content);//找到你要设透明背景的控件的id
view.getBackground().setAlpha(100);//0~255透明度值
Android中的颜色值通常遵循RGB/ARGB标准,使用时通常以“#”字符开头,以16进制表示。
常用的颜色值格式为:
- RGB
- ARGB
- RRGGBB
- AARRGGBB
ARGB 依次代表透明度(alpha)、红色(red)、绿色(green)、蓝色(blue)
透明度分为256阶(0-255),计算机上用16进制表示为(00-ff)。透明就是0阶,不透明就是255阶,如果50%透明就是127阶(256的一半当然是128,但因为是从0开始,所以实际上是127)。
透明度 和 不透明度 是两个概念, 它们加起来是1,或者100%.
ARGB 中的透明度alpha,表示的是不透明度。
android 窗体透明的,黑暗度等的设置技巧
设置透明度(这是窗体本身的透明度,非背景)
WindowManager.LayoutParams layoutParams =getWindow().getAttributes();
layoutParams.alpha=0.3f;
getWindow().setAttributes(layoutParams);
alpha在0.0f到1.0f之间。1.0完全不透明,0.0f完全透明
设置黑暗度
WindowManager.LayoutParams layoutParams =getWindow().getAttributes();
layoutParams .dimAmount=0.5f;
getWindow().setAttributes(layoutParams);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
设置背景模糊
getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
以上设置对dialog对话框同样有效
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>
color.xml的#50000000前两位是透明的效果参数从00–99(透明–>不怎么透明),后6位是RGB的设置
manifest.xml Activity的背景的设置
<activity
android:name=".TransparentActivity"
android:theme="@style/Transparent">
</activity>
//相当于
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.Transparent);
setContentView(R.layout.transparent);
}
换算
在开发过程中,UI/UE给的标注图上,所有颜色值是RGB,但是透明度经常都是百分比,例如:颜色值:#FFFFFF,透明度40%。
使用过程中我们需要进行换算。以之前的值为例,换算过程如下:
1.将透明度转换成不透明度。(转换方式参考:两者加起来是1,或者100%) 不透明度为60%
2.不透明度乘以255。 我们得到结果:153
3.将计算结果转换成16进制。得到最终的不透明度:99
4.将不透明度和颜色值拼接成ARGB格式。得到最终的颜色值: #99FFFFFF
简单的换算,可以先将透明度,转换成不透明度,再根据下面的表格进行对应
常用
100% — FF 95% — F2 90% — E6 85% — D9 80% — CC
75% — BF 70% — B3 65% — A6 60% — 99 55% — 8C
50% — 80 45% — 73 40% — 66 35% — 59 30% — 4D
25% — 40 20% — 33 15% — 26 10% — 1A 5% — 0D
0% — 00
图片透淡入效果
标签为alpha透明度节点
android:fromAlpha=”1.0” 设置动画起始透明度为1.0 表示完全不透明
android:toAlpha=”0.0”设置动画结束透明度为0.0 表示完全透明
也就是说alpha的取值范围为0.0 - 1.0 之间
这个动画布局设置动画从完全不透明渐变到完全透明。
<?xml version="1.0" encoding="utf-8"?>
<alpha
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:repeatCount="infinite"
android:duration="2000">
</alpha>
//然后在Java代码中设置
public class AlphaActivity extends Activity {
/**显示动画的ImageView**/
ImageView mImageView = null;
/**透明动画**/
Animation mAnimation = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.translate);
/**拿到ImageView对象**/
mImageView = (ImageView)findViewById(R.id.imageView);
/**加载透明动画**/
mAnimation = AnimationUtils.loadAnimation(this,R.anim.alpha);
/**播放透明动画**/
mImageView.startAnimation(mAnimation);
}
}