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

我试过了:

mImageview.setBackgroundResource(R.color.trans);

其中trans→<color name="trans">#00000000 </color>。


当前回答

黑色使用下面的代码:

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

现在如果你想使用不透明度,那么你可以使用下面的代码:

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

下面是不透明代码:

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

其他回答

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

但是我认为你可能想要使你放在WebView中的图像的背景透明,例如,使用透明的PNG,而不是ImageView背景。如果实际的图像不是完全透明的,那么ImageView背景就不能通过它被看到。

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

Note

android:background="#CCFF0088

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

一些不透明代码:

十六进制不透明度值

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

你也可以像这样通过编程来设置不透明度:

yourView.getBackground().setAlpha(127);

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

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

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

对于25%的透明度:

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

在Android Studio中,使用内置工具调整颜色和不透明度非常简单:

对于那些仍然面临这个问题的人,你可以试试这个 element.getBackground () .setAlpha (0);

ImageView.setBackground(R.drawable.my_background);

ImageView.setBackgroundResource(R.color.colorPrimary);

ImageView.getImageAlpha();

ImageView.setAlpha(125); // transparency