如何在 Android 上使图像透明?

发布于 2024-10-18 16:50:02 字数 127 浏览 2 评论 0原文

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

现在我想让第二个图像(位于 imageView 中)透明。我该怎么做?

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

半岛未凉 2024-10-25 16:50:02

试试这个:

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)

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)

百善笑为先 2024-10-25 16:50:02

android:alpha 确实XML 中的这个:

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

android:alpha does this in XML:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/blah"
    android:alpha=".75"/>
屋顶上的小猫咪 2024-10-25 16:50:02

在 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);

Set an id attribute on the ImageView:

<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 myImage = (ImageView) findViewById(R.id.myImage);

Then, set Visibility to 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:

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);
感受沵的脚步 2024-10-25 16:50:02

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

 android:background="@null" 

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

 android:background="@null" 
随遇而安 2024-10-25 16:50:02

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

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.

烟凡古楼 2024-10-25 16:50:02

使用setAlpha(float alpha) 设置透明度。下面的代码对我有用,我使用了浮点数 0 - 1 中的 alpha 值。0

  • :完全透明
  • 0.5 - 50%:透明
  • 1:完全不透明

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

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

  • 0: Full Transparent
  • 0.5 - 50%: Transparent
  • 1: Full Opaque

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

海拔太高太耀眼 2024-10-25 16:50:02

由于 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.

As setAlpha int has been deprecated, setImageAlpha (int) can be used

ImageView img = (ImageView) findViewById(R.id.img_image);
img.setImageAlpha(127); //value: [0-255]. Where 0 is fully transparent and 255 is fully opaque.
誰ツ都不明白 2024-10-25 16:50:02

ImageView 类型中的方法 setAlpha(int) 已弃用。

而不是

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

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

Instead of

image.setImageAlpha(127);
//value: [0-255]. Where 0 is fully transparent and 255 is fully opaque.
酒中人 2024-10-25 16:50:02

在 XML 中,使用:

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

In XML, use:

android:background="@android:color/transparent"
夜唯美灬不弃 2024-10-25 16:50:02

图像 alpha 仅设置 ImageView 的不透明度,这会使图像模糊,尝试在 ImageView 中添加色调属性

 android:tint="#66000000"

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

imageView.setColorFilter(R.color.transparent);

您需要在 color.xml 中定义透明颜色

<color name="transparent">#66000000</color>

Image alpha sets just opacity to ImageView which makes Image blurry, try adding tint attribute in ImageView

 android:tint="#66000000"

It can also be done programatically :

imageView.setColorFilter(R.color.transparent);

where you need to define transparent color in your colors.xml

<color name="transparent">#66000000</color>
尘曦 2024-10-25 16:50:02

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

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

For 20% transparency, this worked for me:

Button bu = (Button)findViewById(R.id.button1);
bu.getBackground().setAlpha(204);
鹿童谣 2024-10-25 16:50:02

使用:

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.

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.
许久 2024-10-25 16:50:02

您可以通过以下代码在 XML 文件中轻松执行此操作:

android:alpha="0.85"

它将使您的图像 15% 透明。 0 是完全透明,1 是完全不透明

You can easily do this in your XML file by this code:

android:alpha="0.85"

It will make your image 15% transparent. 0 is fully transparent and 1 is fully opaque

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文