android图像去掉透明部分,透明度-如何在Android上使图像透明?

时间:2025-02-14 11:30:31

透明度-如何在Android上使图像透明?

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

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

khan asked 2019-11-03T09:11:03Z

12个解决方案

123 votes

试试这个:

ImageView myImage = (ImageView) findViewById();

(127); //value: [0-255]. Where 0 is fully transparent and 255 is fully opaque.

注意:不建议使用setAlpha(int),而建议使用setAlpha(float),其中0是完全透明的,而1是完全不透明的。 像这样使用:(0.5f)

Rubycon answered 2019-11-03T09:11:22Z

83 votes

android:alpha使用XML执行此操作:

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/blah"

android:alpha=".75"/>

Eric answered 2019-11-03T09:11:48Z

5 votes

在ImageView上设置一个id属性:

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

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

ImageView myImage = (ImageView) findViewById();

然后,将“可见性”设置为“消失”:

();

如果要在其他地方使它再次可见的代码,只需用相同的方式将其设置为可见:

();

如果您的意思是“完全透明”,则上面的代码有效。 如果您表示“部分透明”,请使用以下方法:

int alphaAmount = 128; // Some value 0-255 where 0 is fully transparent and 255 is fully opaque

(alphaAmount);

Rich answered 2019-11-03T09:12:49Z

4 votes

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

android:background="@null"

Karoly answered 2019-11-03T09:13:14Z

4 votes

在较新版本的Android(至少在Android 4.2(Jelly Bean)之后)上,setAlpha(int value)方法已弃用。 而是使用setAlpha(float value)方法,该方法采用介于0和1之间的浮点数,其中0是完全透明,而1是不透明。

wolfaviators answered 2019-11-03T09:13:41Z

1 votes

不推荐使用ImageView类型的方法setAlpha(int)。

代替

image.setImageAlpha(127);

//value: [0-255]. Where 0 is fully transparent and 255 is fully opaque.

Iman Marashi answered 2019-11-03T09:14:09Z

1 votes

使用setAlpha(float alpha)设置透明度。如果我在float中使用alpha值0-1,则以下代码对我有用。

0:全透明

0.5-50%:透明

1:完全不透明

ImageView imageView =(ImageView)();(mResources [position]);(.80f);

RajaSekar answered 2019-11-03T09:15:04Z

1 votes

在XML中,使用:

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

pankaj gehlot answered 2019-11-03T09:15:31Z

0 votes

对于20%的透明度,这对我有用:

Button bu = (Button)findViewById(.button1);

().setAlpha(204);

Muhamed Riyas M answered 2019-11-03T09:15:59Z

0 votes

使用:

ImageView image = (ImageView) findViewById();

(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.

Shubham Kamlapuri answered 2019-11-03T09:16:24Z

0 votes

图像Alpha设置了ImageView的不透明度,这会使Image模糊,请尝试在ImageView中添加tint属性

android:tint="#66000000"

也可以通过编程方式完成:

();

您需要在中定义透明颜色

#66000000

Ajay Chauhan answered 2019-11-03T09:17:10Z

0 votes

由于不建议使用setAlpha int,因此可以使用setImageAlpha(int)

ImageView img = (ImageView) findViewById(.img_image);

(127); //value: [0-255]. Where 0 is fully transparent and 255 is fully opaque.

Al-Punk answered 2019-11-03T09:17:37Z