本文翻译自:Set transparent background of an imageview on Android
I am using a web view in which I am adding an image view. 我正在使用Web视图,我在其中添加图像视图。 How can I set the background of this image view to transparent? 如何将此图像视图的背景设置为透明?
I have tried this: 我试过这个:
mImageview.setBackgroundResource(R.color.trans);
Where trans
→ <color name="trans">#00000000 </color>
. trans
→ <color name="trans">#00000000 </color>
。
#1楼
参考:https://stackoom.com/question/6GHS/在Android上设置imageview的透明背景
#2楼
如果您想在代码中使用它,只需:
mComponentName.setBackgroundColor(Color.parseColor("#80000000"));
#3楼
或者,作为备用,使用以下代码解析资源ID:
mComponentName.setBackgroundColor(getResources().getColor(android.R.color.transparent));
#4楼
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. 两个十六进制字符的范围为00到FF。 For example, 例如,
- Normal opaque black hex- "#000000" 普通不透明黑色六角形 - “#000000”
- Fully transparent - "#00000000" 完全透明 - “#00000000”
- Fully opaque - "#FF000000" 完全不透明 - “#FF000000”
- 50% transparent - "#7F000000" 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. 将百分比数除以100并乘以255得到小数值。 Convert the decimal to hexadecimal here . 在这里将十进制转换为十六进制。
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:如何创建透明或不透明的背景
#5楼
There is already a transparent built into Android: R.color.transparent. Android内置了一个透明的:R.color.transparent。 http://developer.android.com/reference/android/R.color.html#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. 但我认为您可能希望将放置到WebView中的图像的背景透明化,例如,使用透明的PNG,而不是ImageView背景。 If the actual image is not at all see-through then the ImageView background can't be seen through it. 如果实际图像根本没有透视,则无法通过它看到ImageView背景。
#6楼
You could also use View.setAlpha(float)
to change the visibility precisely. 您还可以使用View.setAlpha(float)
精确更改可见性。
0 would be transparent, 1 fully visible. 0将是透明的,1是完全可见的。 ;) ;)