如何在Android上使图像透明?

时间:2022-08-27 12:16:26

I am using a linear layout and frame layout. In the linear layout I keep an image as background and in the frame layout I keep an imageView. In that imageView I give an image.

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

Now I want to make the second image (that is in the imageView) transparent. How can I do this?

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

10 个解决方案

#1


103  

Try this:

尝试这个:

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

Note: setAlpha(int) is deprecated in favor of setAlpha(float) where 0 is fully transparent and 1 is fully opaque. Use it like: myImage.setAlpha(0.5f)

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

#2


71  

android:alpha does this in XML:

android:alpha在XML中执行此操作:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/blah"
    android:alpha=".75"/>

#3


4  

Set an id attribute on the ImageView:

在ImageView上设置id属性:

<ImageView android:id="@+id/myImage"

In your code where you wish to hide the image, you'll need the following code.

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

First, you'll need a reference to the ImageView:

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

ImageView myImage = (ImageView) findViewById(R.id.myImage);

Then, set Visibility to GONE:

然后,将Visibility设置为GONE:

myImage.setVisibility(View.GONE);

If you want to have code elsewhere that makes it visible again, just set it to Visible the same way:

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

myImage.setVisibility(View.VISIBLE);

If you mean "fully transparent", the above code works. If you mean "partially transparent", use the following method:

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

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

#4


4  

If you are in an XML file, use the following to make your imageview transparent!

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

 android:background="@null" 

#5


3  

On newer versions of Android (post Android 4.2 (Jelly Bean) at least), the setAlpha(int value) method is depreciated. Instead, use the setAlpha(float value) method that takes a float between 0 and 1 where 0 is complete transparency and 1 is no transparency.

在较新版本的Android(至少在Android 4.2(Jelly Bean)之后),setAlpha(int value)方法被折旧。相反,使用setAlpha(float value)方法,该方法将浮点值设置为0到1,其中0表示完全透明,1表示不透明。

#6


1  

Set transparency using setAlpha(float alpha). The below code works for me were I used an alpha value in float, 0 - 1.

使用setAlpha(float alpha)设置透明度。以下代码适用于我,我在float中使用了alpha值,0 - 1。

  • 0: Full Transparent
  • 0:完全透明
  • 0.5 - 50%: Transparent
  • 0.5 - 50%:透明
  • 1: Full Opaque

    1:完全不透明

    ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView); imageView.setImageResource(mResources[position]); imageView.setAlpha(.80f);

    ImageView imageView =(ImageView)itemView.findViewById(R.id.imageView); imageView.setImageResource(mResources [位置]); imageView.setAlpha(.80f);

#7


1  

In XML, use:

在XML中,使用:

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

#8


0  

The method setAlpha(int) from the type ImageView is deprecated.

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

Instead of

代替

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

#9


0  

For 20% transparency, this worked for me:

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

Button bu = (Button)findViewById(R.id.button1);
bu.getBackground().setAlpha(204);

#10


0  

Use:

使用:

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.

#1


103  

Try this:

尝试这个:

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

Note: setAlpha(int) is deprecated in favor of setAlpha(float) where 0 is fully transparent and 1 is fully opaque. Use it like: myImage.setAlpha(0.5f)

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

#2


71  

android:alpha does this in XML:

android:alpha在XML中执行此操作:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/blah"
    android:alpha=".75"/>

#3


4  

Set an id attribute on the ImageView:

在ImageView上设置id属性:

<ImageView android:id="@+id/myImage"

In your code where you wish to hide the image, you'll need the following code.

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

First, you'll need a reference to the ImageView:

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

ImageView myImage = (ImageView) findViewById(R.id.myImage);

Then, set Visibility to GONE:

然后,将Visibility设置为GONE:

myImage.setVisibility(View.GONE);

If you want to have code elsewhere that makes it visible again, just set it to Visible the same way:

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

myImage.setVisibility(View.VISIBLE);

If you mean "fully transparent", the above code works. If you mean "partially transparent", use the following method:

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

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

#4


4  

If you are in an XML file, use the following to make your imageview transparent!

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

 android:background="@null" 

#5


3  

On newer versions of Android (post Android 4.2 (Jelly Bean) at least), the setAlpha(int value) method is depreciated. Instead, use the setAlpha(float value) method that takes a float between 0 and 1 where 0 is complete transparency and 1 is no transparency.

在较新版本的Android(至少在Android 4.2(Jelly Bean)之后),setAlpha(int value)方法被折旧。相反,使用setAlpha(float value)方法,该方法将浮点值设置为0到1,其中0表示完全透明,1表示不透明。

#6


1  

Set transparency using setAlpha(float alpha). The below code works for me were I used an alpha value in float, 0 - 1.

使用setAlpha(float alpha)设置透明度。以下代码适用于我,我在float中使用了alpha值,0 - 1。

  • 0: Full Transparent
  • 0:完全透明
  • 0.5 - 50%: Transparent
  • 0.5 - 50%:透明
  • 1: Full Opaque

    1:完全不透明

    ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView); imageView.setImageResource(mResources[position]); imageView.setAlpha(.80f);

    ImageView imageView =(ImageView)itemView.findViewById(R.id.imageView); imageView.setImageResource(mResources [位置]); imageView.setAlpha(.80f);

#7


1  

In XML, use:

在XML中,使用:

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

#8


0  

The method setAlpha(int) from the type ImageView is deprecated.

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

Instead of

代替

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

#9


0  

For 20% transparency, this worked for me:

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

Button bu = (Button)findViewById(R.id.button1);
bu.getBackground().setAlpha(204);

#10


0  

Use:

使用:

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.