如何使带有背景颜色(例如白色)的Textview
的背景大约透明20%(不是完全透明)?
如何使带有背景颜色(例如白色)的Textview
的背景大约透明20%(不是完全透明)?
以下代码可用于黑色:
<color name="black">#000000</color>
现在如果我想使用不透明度,您可以使用以下代码:
<color name="black">#99000000</color> <!-- 99 is for alpha and others pairs zero's are for R G B -->
以下是透明度代码: 以及所有透明度级别
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
如果您经常忘记如何编写透明代码,那么您必须查看下面的链接,并且不用担心记住任何关于透明代码的内容:
https://github.com/duggu-hcd/TransparentColorCode
textviewHeader.setTextColor(Color.parseColor(ColorTransparentUtils.transparentColor(R.color.border_color,10)));
将颜色的 alpha 通道设置为 80%。例如,对于红色使用 #CCFF0000
:
<TextView
...
android:background="#CCFF0000" />
在这个例子中,CC
是十六进制表示法下 255 * 0.8 = 204
的数值,注意前两位十六进制数字是用来表示 alpha 通道的。格式为 #AARRGGBB
,其中 AA
为 alpha 通道,RR
为红色通道,GG
为绿色通道,BB
为蓝色通道。33
,它表示 255 * 0.2 = 51
。100-20=80
)2^8=256
),也就是说它的范围从0到255。255 * 0.8 = 204
。如果需要,取最近的整数。0xCC
。FF0000
,你将得到 CCFF0000
。android:background="#CCFFFFFF"
。 - gotwo#99000000
100% — FF
99% — FC
98% — FA
97% — F7
96% — F5
95% — F2
94% — F0
93% — ED
92% — EB
91% — E8
90% — E6
89% — E3
88% — E0
87% — DE
86% — DB
85% — D9
84% — D6
83% — D4
82% — D1
81% — CF
80% — CC
79% — C9
78% — C7
77% — C4
76% — C2
75% — BF
74% — BD
73% — BA
72% — B8
71% — B5
70% — B3
69% — B0
68% — AD
67% — AB
66% — A8
65% — A6
64% — A3
63% — A1
62% — 9E
61% — 9C
60% — 99
59% — 96
58% — 94
57% — 91
56% — 8F
55% — 8C
54% — 8A
53% — 87
52% — 85
51% — 82
50% — 80
49% — 7D
48% — 7A
47% — 78
46% — 75
45% — 73
44% — 70
43% — 6E
42% — 6B
41% — 69
40% — 66
39% — 63
38% — 61
37% — 5E
36% — 5C
35% — 59
34% — 57
33% — 54
32% — 52
31% — 4F
30% — 4D
29% — 4A
28% — 47
27% — 45
26% — 42
25% — 40
24% — 3D
23% — 3B
22% — 38
21% — 36
20% — 33
19% — 30
18% — 2E
17% — 2B
16% — 29
15% — 26
14% — 24
13% — 21
12% — 1F
11% — 1C
10% — 1A
9% — 17
8% — 14
7% — 12
6% — 0F
5% — 0D
4% — 0A
3% — 08
2% — 05
1% — 03
0% — 00
使用带有 alpha 值的颜色,例如 #33------
,并将其设置为您的 editText 的背景,使用 XML 属性 android:background=" "
。
255 * 0.2 = 51 → 转换成十六进制即为 33
textView.getBackground().setAlpha(51);
您可以在此处将不透明度设置为0(完全透明)到255(完全不透明)。51正是您想要的20%。
textView
变量是 null
。 - yugidroidmyImage.setAlpha(0.5f);
将alpha应用于View本身,但我在答案中展示的是应用于View的背景可绘制对象。 - yugidroid我们也可以用这种方式来制作透明效果。
白色代码 - FFFFFF
20%的白色 - #33FFFFFF
20% — 33
70%的白色 - #B3FFFFFF
70% — B3
所有十六进制值从100%到0%
**100% —** **FF**,
99% — FC,
98% — FA,
97% — F7,
96% — F5,
95% — F2,
94% — F0,
93% — ED,
92% — EB,
91% — E8,
**90% —** **E6**,
89% — E3,
88% — E0,
87% — DE,
86% — DB,
85% — D9,
**84% —** **D6**,
83% — D4,
82% — D1,
81% — CF,
**80% —** **CC**,
79% — C9,
78% — C7,
77% — C4,
76% — C2,
75% — BF,
74% — BD,
73% — BA,
72% — B8,
71% — B5,
**70% — B3**,
69% — B0,
68% — **AD**,
67% — AB,
66% — A8,
65% — A6,
64% — A3,
63% — A1,
62% — 9E,
61% — 9C,
**60% —** **99**,
59% — 96,
58% — 94,
57% — 91,
56% — 8F,
55% — 8C,
54% — 8A,
53% — 87,
52% — 85,
51% — 82,
**50% —** **80**,
49% — 7D,
48% — 7A,
47% — 78,
46% — 75,
45% — 73,
44% — 70,
43% — 6E,
42% — 6B,
41% — 69,
**40% —** **66**,
39% — 63,
38% — 61,
37% — 5E,
36% — 5C,
35% — 59,
34% — 57,
33% — 54,
32% — 52,
31% — 4F,
**30% —** **4D**,
29% — 4A,
28% — 47,
27% — 45,
26% — 42,
25% — 40,
24% — 3D,
23% — 3B,
22% — 38,
21% — 36,
**20% — 33**,
19% — 30,
18% — **2E**,
17% — 2B,
16% — 29,
15% — 26,
14% — 24,
13% — 21,
12% — 1F,
11% — 1C,
**10% —** **1A**,
9% — 17,
8% — 14,
7% — 12,
6% — 0F,
5% — 0D,
4% — 0A,
3% — 08,
2% — 05,
1% — 03,
**0% —** **00**
所有十六进制值从100%到0%的透明度,您可以使用以下透明度值设置任何颜色。例如#FAFFFFFF(AARRGGBB)
100% — FF
99% — FC
98% — FA
97% — F7
96% — F5
95% — F2
94% — F0
93% — ED
92% — EB
91% — E8
90% — E6
89% — E3
88% — E0
87% — DE
86% — DB
85% — D9
84% — D6
83% — D4
82% — D1
81% — CF
80% — CC
79% — C9
78% — C7
77% — C4
76% — C2
75% — BF
74% — BD
73% — BA
72% — B8
71% — B5
70% — B3
69% — B0
68% — AD
67% — AB
66% — A8
65% — A6
64% — A3
63% — A1
62% — 9E
61% — 9C
60% — 99
59% — 96
58% — 94
57% — 91
56% — 8F
55% — 8C
54% — 8A
53% — 87
52% — 85
51% — 82
50% — 80
49% — 7D
48% — 7A
47% — 78
46% — 75
45% — 73
44% — 70
43% — 6E
42% — 6B
41% — 69
40% — 66
39% — 63
38% — 61
37% — 5E
36% — 5C
35% — 59
34% — 57
33% — 54
32% — 52
31% — 4F
30% — 4D
29% — 4A
28% — 47
27% — 45
26% — 42
25% — 40
24% — 3D
23% — 3B
22% — 38
21% — 36
20% — 33
19% — 30
18% — 2E
17% — 2B
16% — 29
15% — 26
14% — 24
13% — 21
12% — 1F
11% — 1C
10% — 1A
9% — 17
8% — 14
7% — 12
6% — 0F
5% — 0D
4% — 0A
3% — 08
2% — 05
1% — 03
0% — 00
我创建了三个视图。在第一个视图中,我设置了完整的颜色(没有透明度);在第二个视图中,我设定了半透明的颜色(透明度为0.5);在第三个视图中,我设定了浅色的颜色(透明度为0.2)。
您可以使用以下代码设置任何带有透明度的颜色:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools = "http://schemas.android.com/tools"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:gravity = "center"
android:orientation = "vertical"
tools:context = "com.example.temp.MainActivity" >
<View
android:id = "@+id/fullColorView"
android:layout_width = "100dip"
android:layout_height = "100dip" />
<View
android:id = "@+id/halfalphaColorView"
android:layout_width = "100dip"
android:layout_height = "100dip"
android:layout_marginTop = "20dip" />
<View
android:id = "@+id/alphaColorView"
android:layout_width = "100dip"
android:layout_height = "100dip"
android:layout_marginTop = "20dip" />
</LinearLayout>
public class MainActivity extends Activity {
private View fullColorView, halfalphaColorView, alphaColorView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fullColorView = (View)findViewById(R.id.fullColorView);
halfalphaColorView = (View)findViewById(R.id.halfalphaColorView);
alphaColorView = (View)findViewById(R.id.alphaColorView);
fullColorView.setBackgroundColor(Color.BLUE);
halfalphaColorView.setBackgroundColor(getColorWithAlpha(Color.BLUE, 0.5f));
alphaColorView.setBackgroundColor(getColorWithAlpha(Color.BLUE, 0.2f));
}
private int getColorWithAlpha(int color, float ratio) {
int newColor = 0;
int alpha = Math.round(Color.alpha(color) * ratio);
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
newColor = Color.argb(alpha, r, g, b);
return newColor;
}
}
Kotlin版本:
private fun getColorWithAlpha(color: Int, ratio: Float): Int {
return Color.argb(Math.round(Color.alpha(color) * ratio), Color.red(color), Color.green(color), Color.blue(color))
}
完成
有一个XML值alpha
接受double值。
从API 11+
开始,范围从0f
到1f
(包括两端),其中0f
表示透明,1f
表示不透明:
android:alpha="0.0"
表示完全不可见
android:alpha="0.5"
半透明
android:alpha="1.0"
完全可见
就是这样。