我正在使用线性布局和框架布局。在线性布局中,我将图像保留为背景,在框架布局中,我保留了一个imageView。在那个imageView我给出了一个图像。
现在我想让第二个图像(在imageView中)透明。我怎样才能做到这一点?
试试这个:
ImageView myImage = (ImageView) findViewById(R.id.myImage);
myImage.setAlpha(127); //value: [0-255]. Where 0 is fully transparent and 255 is fully opaque.
注意:setAlpha(int)
不赞成使用setAlpha(float)
,其中0表示完全透明,1表示完全不透明。使用它像:myImage.setAlpha(0.5f)
使用:
ImageView image = (ImageView) findViewById(R.id.image);
image.setAlpha(150); // Value: [0-255]. Where 0 is fully transparent
// and 255 is fully opaque. Set the value according
// to your choice, and you can also use seekbar to
// maintain the transparency.
图像alpha只为ImageView设置不透明度,使图像模糊,尝试在ImageView中添加色调属性
android:tint="#66000000"
它也可以通过编程方式完成:
imageView.setColorFilter(R.color.transparent);
您需要在colors.xml中定义透明颜色
<color name="transparent">#66000000</color>
由于不推荐使用setAlpha int,因此可以使用setImageAlpha(int)
ImageView img = (ImageView) findViewById(R.id.img_image);
img.setImageAlpha(127); //value: [0-255]. Where 0 is fully transparent and 255 is fully opaque.
android:alpha
用XML做到这一点:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/blah"
android:alpha=".75"/>
在ImageView上设置id属性:
<ImageView android:id="@+id/myImage"
在您希望隐藏图像的代码中,您需要以下代码。
首先,您需要对ImageView的引用:
ImageView myImage = (ImageView) findViewById(R.id.myImage);
然后,将Visibility设置为GONE:
myImage.setVisibility(View.GONE);
如果你想让别处的代码再次显示,只需将其设置为Visible,方法如下:
myImage.setVisibility(View.VISIBLE);
如果您的意思是“完全透明”,则上述代码有效。如果您的意思是“部分透明”,请使用以下方法:
int alphaAmount = 128; // Some value 0-255 where 0 is fully transparent and 255 is fully opaque
myImage.setAlpha(alphaAmount);
如果您在XML文件中,请使用以下命令使您的imageview透明!
android:background="@null"
在较新版本的Android(至少在Android 4.2(Jelly Bean)之后),setAlpha(int value)方法已被折旧。相反,使用setAlpha(float value)
方法,该方法在0和1之间采用浮点数,其中0表示完全透明,1表示不透明。
不推荐使用ImageView类型的方法setAlpha(int)
。
代替
image.setImageAlpha(127);
//value: [0-255]. Where 0 is fully transparent and 255 is fully opaque.
使用setAlpha(float alpha)
设置透明度。以下代码适用于我,我在float中使用了alpha值,0 - 1。
在XML中,使用:
android:background="@android:color/transparent"
对于20%的透明度,这对我有用:
Button bu = (Button)findViewById(R.id.button1);
bu.getBackground().setAlpha(204);