如何在Android上使图像透明?

kha*_*han 58 transparency android

我正在使用线性布局和框架布局.在线性布局中,我将图像保留为背景,在框架布局中,我保留了一个imageView.在那个imageView我给出了一个图像.

现在我想让第二个图像(在imageView中)透明.我怎样才能做到这一点?

Rub*_*con 126

试试这个:

ImageView myImage = (ImageView) findViewById(R.id.myImage);
myImage.setAlpha(127); //value: [0-255]. Where 0 is fully transparent and 255 is fully opaque.
Run Code Online (Sandbox Code Playgroud)

注意:setAlpha(int)不推荐使用setAlpha(float)0 ,其中0表示完全透明,1表示完全不透明.使用它像:myImage.setAlpha(0.5f)

  • 不推荐使用setAlpha(int)来支持setAlpha(float),其中0是完全透明的,1是完全不透明的`myImage.setAlpha(0.5f);` (14认同)

小智 85

android:alpha 用XML做到这一点:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/blah"
    android:alpha=".75"/>
Run Code Online (Sandbox Code Playgroud)


Ric*_*ich 5

在ImageView上设置id属性:

<ImageView android:id="@+id/myImage"
Run Code Online (Sandbox Code Playgroud)

在您希望隐藏图像的代码中,您需要以下代码.

首先,您需要对ImageView的引用:

ImageView myImage = (ImageView) findViewById(R.id.myImage);
Run Code Online (Sandbox Code Playgroud)

然后,将Visibility设置为GONE:

myImage.setVisibility(View.GONE);
Run Code Online (Sandbox Code Playgroud)

如果你想让别处的代码再次显示,只需将其设置为Visible,方法如下:

myImage.setVisibility(View.VISIBLE);
Run Code Online (Sandbox Code Playgroud)

如果您的意思是"完全透明",则上述代码有效.如果您的意思是"部分透明",请使用以下方法:

int alphaAmount = 128; // Some value 0-255 where 0 is fully transparent and 255 is fully opaque
myImage.setAlpha(alphaAmount);
Run Code Online (Sandbox Code Playgroud)


Kar*_*oly 5

如果您在 XML 文件中,请使用以下内容使您的图像视图透明!

 android:background="@null" 
Run Code Online (Sandbox Code Playgroud)


小智 5

在较新版本的 Android 上(至少在 Android 4.2 (Jelly Bean) 之后), setAlpha(int value) 方法已贬值。相反,使用setAlpha(float value)在 0 和 1 之间采用浮点数的方法,其中 0 表示完全透明,1 表示不透明。