如何在 Android 上使背景透明 20%

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11285961/
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 06:44:05  来源:igfitidea点击:

How to make a background 20% transparent on Android

androidtransparencytextview

提问by Adham

How do I make the background of a Textviewabout 20% transparent (not fully transparent), where there is a color in the background (i.e. white)?

如何制作Textview大约 20% 透明(不是完全透明)的背景,其中背景中有颜色(即白色)?

回答by duggu

Use the below code for black:

对黑色使用以下代码:

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

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

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

 <color name="black">#99000000</color> <!-- 99 is for alpha and others pairs zero's are for R G B -->

And below for opacity code: and all opacity level here

下面是不透明度代码:和所有不透明度级别在这里

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

If you always to forget what code for transparency then you must have to see below link and no worry about to remember anything regarding transparent code :-

如果您总是忘记透明代码,那么您必须查看以下链接,而不必担心记住有关透明代码的任何内容:-

https://github.com/duggu-hcd/TransparentColorCode

https://github.com/duggu-hcd/TransparentColorCode

textviewHeader.setTextColor(Color.parseColor(ColorTransparentUtils.transparentColor(R.color.border_color,10)));

回答by aromero

Make the color have 80% in the alpha channel. For example, for red use #CCFF0000:

使颜色在 alpha 通道中有 80%。例如,对于红色使用#CCFF0000

<TextView
   ...
   android:background="#CCFF0000" />

In the example, CCis the hexadecimal number for 255 * 0.8 = 204. Note that the first two hexadecimal digits are for the alpha channel. The format is #AARRGGBB, where AAis the alpha channel, RRis the red channel, GGis the green channel and BBis the blue channel.

在示例中,CC是 的十六进制数255 * 0.8 = 204。请注意,前两个十六进制数字用于 alpha 通道。格式为#AARRGGBB,其中AA是 alpha 通道,RR是红色通道,GG是绿色通道,BB是蓝色通道。

I'm assuming that 20% transparent means 80% opaque. If you meant the other way, instead of CCuse 33which is the hexadecimal for 255 * 0.2 = 51.

我假设 20% 的透明意味着 80% 的不透明。如果您的意思是其他方式,而不是CC使用33which 是255 * 0.2 = 51.

In order to calculate the proper value for an alpha transparency value you can follow this procedure:

为了计算 alpha 透明度值的正确值,您可以按照以下步骤操作:

  1. Given a transparency percentage, for example 20%, you know the opaque percentage value is 80% (this is 100-20=80)
  2. The range for the alpha channel is 8 bits (2^8=256), meaning the range goes from 0 to 255.
  3. Project the opaque percentage into the alpha range, that is, multiply the range (255) by the percentage. In this example 255 * 0.8 = 204. Round to the nearest integer if needed.
  4. Convert the value obtained in 3., which is in base 10, to hexadecimal (base 16). You can use Google for this or any calculator. Using Google, type "204 to hexa" and it will give you the hexadecimal value. In this case it is 0xCC.
  5. Prepend the value obtained in 4. to the desired color. For example, for red, which is FF0000, you will have CCFF0000.
  1. 给定透明度百分比,例如 20%,您知道不透明百分比值为 80%(这是100-20=80
  2. Alpha 通道的范围是 8 位 ( 2^8=256),这意味着范围从 0 到 255。
  3. 将不透明百分比投影到 alpha 范围内,即将范围 (255) 乘以百分比。在这个例子中255 * 0.8 = 204。如果需要,四舍五入到最接近的整数。
  4. 将 3. 中获得的以 10 为底的值转换为十六进制(以 16 为底)。您可以为此使用 Google 或任何计算器。使用谷歌,输入“204 to hexa”,它会给你十六进制值。在这种情况下是0xCC
  5. 将 4. 中获得的值添加到所需的颜色。例如,对于红色,即FF0000,您将拥有CCFF0000

You can take a look at the Android documentation for colors.

您可以查看颜色Android 文档

回答by carlol

You can manage color opacity changing the first 2 characters in the color definition:

您可以通过更改颜色定义中的前 2 个字符来管理颜色不透明度:

#99000000

# 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 

回答by K_Anas

Use a color with an alpha value like #33------, and set it as background of your editText using the XML attribute android:background=" ".

使用带有 alpha 值的颜色,例如#33------,并使用 XML 属性将其设置为 editText 的背景android:background=" "

  1. 0% (transparent) -> #00 in hex
  2. 20% -> #33
  3. 50% -> #80
  4. 75% -> #C0
  5. 100% (opaque) -> #FF
  1. 0%(透明)-> #00 十六进制
  2. 20% -> #33
  3. 50% -> #80
  4. 75% -> #C0
  5. 100%(不透明)-> #FF

255 * 0.2 = 51 → in hex 33

255 * 0.2 = 51 → 十六进制 33

回答by yugidroid

You can try to do something like:

您可以尝试执行以下操作:

textView.getBackground().setAlpha(51);

Here you can set the opacity between 0 (fully transparent) to 255 (completely opaque). The 51 is exactly the 20% you want.

您可以在此处设置 0(完全透明)到 255(完全不透明)之间的不透明度。51 正是您想要的 20%。

回答by Jayakrishnan PM

In Android Studio there is a built-in tool to adjust the color and alpha/opacity value:

在 Android Studio 中有一个内置工具来调整颜色和 alpha/opacity 值

Android Adjust Color Opacity

Android 调整颜色不透明度

回答by Hiren Patel

See screenshot

看截图

I have taken three Views. In the first view I set full (no alpha) color, on the second view I set half (0.5 alpha) color, and on the third view I set light color (0.2 alpha).

我已经采取了三个观点。在第一个视图中,我设置了全色(无 alpha),在第二个视图中,我设置了一半(0.5 alpha)颜色,在第三个视图中,我设置了浅色(0.2 alpha)。

You can set any color and get color with alpha by using the below code:

您可以使用以下代码设置任何颜色并使用 alpha 获取颜色:

File activity_main.xml

文件activity_main.xml

<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>

File MainActivity.java

文件MainActivity.java

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 version:

科特林版本:

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))
}

Done

完毕

回答by Ashish Kumar

We can make transparent in dis way also.

我们也可以以 dis 方式使透明。

White color code - FFFFFF

白色代码 - FFFFFF

20% white - #33FFFFFF

20% 白色 - # 33FFFFFF

20% — 33

20% — 33

70% white - #B3FFFFFF

70% 白色 - # B3FFFFFF

70% — B3

70% — B3

All teh hex value from 100% to 0%

从 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% — 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

回答by Anant Shah

All hex value from 100% to 0% alpha, You can set any color with alpha values mentioned below. e.g #FAFFFFFF(ARRGGBB)

从 100% 到 0% alpha 的所有十六进制值,您可以使用下面提到的 alpha 值设置任何颜色。例如#FAFFFFFF(ARRGGBB)

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

回答by Chandan Sharma

Now Android Studio 3.3and later version provide an inbuilt feature to change an Alphavalue of the color,

现在Android Studio 3.3及更高版本提供了一个内置功能来更改颜色的Alpha值,

Just click on a color in Android studio editor and provide Alpha value in percentage.

只需在 Android Studio 编辑器中单击一种颜色并percentage.

For more information see below image

有关更多信息,请参见下图

enter image description here

在此处输入图片说明