在 Android 上设置图像视图的透明背景

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1492554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:08:45  来源:igfitidea点击:

Set transparent background of an imageview on Android

androidandroid-widget

提问by mudit

I am using a web view in which I am adding an image view. How can I set the background of this image view to transparent?

我正在使用 Web 视图,在其中添加图像视图。如何将此图像视图的背景设置为透明?

I have tried this:

我试过这个:

mImageview.setBackgroundResource(R.color.trans);

Where trans<color name="trans">#00000000 </color>.

哪里trans<color name="trans">#00000000 </color>

回答by Chirag Patel

You can set the background transparent of any layout, any view, or any component by adding this code in XML:

您可以通过在 XML 中添加以下代码来设置任何布局、任何视图或任何组件的背景透明:

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

回答by Harshad

In your XML set the Backgroundattribute to any colour, White(#FFFFFF)shade or Black(#000000)shade. If you want transparency, just put 80 before the actual hash code:

在您的 XML 中,将Background属性设置为任何颜色、White(#FFFFFF)阴影或Black(#000000)阴影。如果您想要透明,只需在实际哈希码前加上 80:

#80000000

This will change any colour you want to a transparent one.. :)

这会将您想要的任何颜色更改为透明颜色.. :)

回答by Aaron

In addition to what Harshad mentioned:

除了哈沙德提到的:

Two hexadecimal characters can be appended to any hexadecimal color code. The first two characters in an 8-digit hex color code represents its opacity in Android.

可以将两个十六进制字符附加到任何十六进制颜色代码。8 位十六进制颜色代码中的前两个字符表示其在 Android 中的不透明度。

The two hexadecimal characters can range from 00 to FF. For example,

这两个十六进制字符的范围可以从 00 到 FF。例如,

  • Normal opaque black hex- "#000000"
  • Fully transparent - "#00000000"
  • Fully opaque - "#FF000000"
  • 50% transparent - "#7F000000"
  • 正常不透明黑色十六进制-“#000000”
  • 完全透明 - “#00000000”
  • 完全不透明 - “#FF000000”
  • 50% 透明 - “#7F000000”

This way you can change any color to any level of transparency.

通过这种方式,您可以将任何颜色更改为任何级别的透明度。

To find the hexadecimal prefix from a percentage:

要从百分比中查找十六进制前缀:

Divide the percentage number by 100 and multiply by 255 to get the decimal value. Convert the decimal to hexadecimal here.

将百分比数除以 100 再乘以 255 得到十进制值。在这里将十进制转换为十六进制。

For example, for 50%, 50/100 * 255 = 127. Using the link we get hexadecimal value 7F.

例如,对于 50%,50/100 * 255 = 127。使用链接我们得到十六进制值 7F。

Source: Android: how to create a transparent or opaque background

来源:Android:如何创建透明或不透明背景

回答by Jawad Zeb

If you want to add 20% or 30% transparency, you should pre-pend two more characters to the hexadecimal code, like CC.

如果您想添加 20% 或 30% 的透明度,您应该在十六进制代码前再添加两个字符,例如 CC。

Note

笔记

android:background="#CCFF0088"in XML

android:background="#CCFF0088"以 XML 格式

where CC is the alpha value, FF is the red factor, 00 is the green factor, and 88 is the blue factor.

其中 CC 是 alpha 值,FF 是红色因子,00 是绿色因子,88 是蓝色因子。

Some opacity code:

一些不透明度代码:

Hex Opacity Values

十六进制不透明度值

100% — FF
95% — F2
90% — E6
85% — D9
80% — CC
75% — BF
70% — B3
65% — A6
60% — 99
55% — 8C
50% — 80
45% — 73
40% — 66
35% — 59
30% — 4D
25% — 40
20% — 33
15% — 26
10% — 1A
5%  — 0D
0% —  00

You can also set opacity programmatically like:

您还可以以编程方式设置不透明度,例如:

yourView.getBackground().setAlpha(127);

Set opacity between 0 (fully transparent) to 255 (completely opaque). The 127.5 is exactly 50%.

将不透明度设置为 0(完全透明)到 255(完全不透明)。127.5 正好是 50%。

You can create any level of transparency using the given formula. If you want half transparent:

您可以使用给定的公式创建任何级别的透明度。如果你想要半透明:

 16 |128          Where 128 is the half of 256.
    |8 -0         So it means 80 is half transparent.

And for 25% transparency:

对于 25% 的透明度:

16 |64            Where 64 is the quarter of 256.
   |4 -0          So it means 40 is quarter transparent.

回答by Narendra Sorathiya

Use the below code for black:

对黑色使用以下代码:

<color name="black">#000000</color>

Now if you want to use opacity then you can use the below code:

现在,如果您想使用不透明度,则可以使用以下代码:

<color name="black">#99000000</color>

And the below for opacity code:

下面是不透明度代码:

100% — FF

100% — FF

95% — F2

95% — F2

90% — E6

90% — E6

85% — D9

85% — D9

80% — CC

80% — 抄送

75% — BF

75% — 高炉

70% — B3

70% — B3

65% — A6

65% — A6

60% — 99

60% — 99

55% — 8C

55% — 8C

50% — 80

50% — 80

45% — 73

45% — 73

40% — 66

40% — 66

35% — 59

35% — 59

30% — 4D

30% — 4D

25% — 40

25% — 40

20% — 33

20% — 33

15% — 26

15% — 26

10% — 1A

10% — 1A

5% — 0D

5% — 0 天

0% — 00

0% — 00

回答by Ahmad Dwaik 'Warlock'

There is already a predefined constant. Use Color.TRANSPARENT.

已经有一个预定义的常量。使用Color.TRANSPARENT.

回答by Intrications

There is already a transparent built into Android: R.color.transparent. http://developer.android.com/reference/android/R.color.html#transparent

Android 中已经内置了一个透明的:R.color.transparent。http://developer.android.com/reference/android/R.color.html#transparent

But I think you may want to make the background of the image that you are placing into the WebView transparent, for example, with a transparent PNG, rather than the ImageView background. If the actual image is not at all see-through then the ImageView background can't be seen through it.

但我认为您可能希望将放置到 WebView 中的图像的背景设为透明,例如,使用透明的 PNG,而不是 ImageView 背景。如果实际图像根本不透明,则无法通过它看到 ImageView 背景。

回答by Felipe Vasconcelos

In case you want it in code, just:

如果您想在代码中使用它,只需:

mComponentName.setBackgroundColor(Color.parseColor("#80000000"));

回答by Agilanbu

Try this code :)

试试这个代码:)

Its an fully transparent hexa code - "#00000000"

它是一个完全透明的六进制代码 - “#00000000”

回答by Octoth0rpe

Or, as an alternate, parse the resource ID with the following code:

或者,作为替代,使用以下代码解析资源 ID:

  mComponentName.setBackgroundColor(getResources().getColor(android.R.color.transparent));