我如何使一个Textview的背景大约20%透明(不完全透明),在哪里有一个颜色的背景(即白色)?


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

0%(透明)-> #00十六进制 20% -> #33 50% -> #80 75% -> # c0 100%(不透明)-> #FF

255 * 0.2 = 51→十六进制33


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

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

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

我假设20%透明意味着80%不透明。如果你的意思是另一种方式,用33代替CC,这是255 * 0.2 = 51的十六进制。

为了计算alpha透明度值的正确值,您可以遵循以下步骤:

Given a transparency percentage, for example 20%, you know the opaque percentage value is 80% (this is 100-20=80) The range for the alpha channel is 8 bits (2^8=256), meaning the range goes from 0 to 255. 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. 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. Prepend the value obtained in 4. to the desired color. For example, for red, which is FF0000, you will have CCFF0000.

你可以看看Android的颜色文档。


你可以尝试这样做:

textView.getBackground().setAlpha(51);

这里你可以将不透明度设置在0(完全透明)到255(完全不透明)之间。51就是你想要的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),在第二个视图中,我设置了半颜色(0.5 alpha),在第三个视图中,我设置了浅色(0.2 alpha)。

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

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

文件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;
    }
}

芬兰湾的科特林版:

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


下面是来自@Aromero的答案的编程解决方案,用于计算alpha通道的十六进制值。:)

 public static void main(String[] args) throws Exception {
    final Scanner scanner = new Scanner(System.in);
    int transPerc;
    float fPerc;
    System.out.println("Enter the transparency percentage without % symbol:");
    while((transPerc=scanner.nextInt())>=0 && transPerc <=100){
        fPerc = (float) transPerc / 100;
        transPerc = Math.round(255 * fPerc);
        System.out.println("= " + Integer.toHexString(transPerc));
        System.out.print("another one please : ");
    }
    scanner.close();
}

有一个XML值alpha,它接受双重值。

从API 11+开始,范围从0f到1f(包括),0f是透明的,1f是不透明的:

Android:alpha="0.0"这是不可见的 android:α= " 0.5 "透明 Android:alpha="1.0"完全可见

它就是这样运作的。


<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:alpha="0.9"
        />

在Android API 11+中,Alpha范围在0(透明)和1(不透明)之间


你可以管理颜色不透明度改变颜色定义中的前2个字符:

# 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 

查看下面的流行textView使用这个

     android:alpha="0.38"

XML

android:color="#3983BE00"    // Partially transparent sky blue

动态

btn.getBackground () .setAlpha (128);// 50%透明

tv_name.getBackground () .setAlpha (128);// 50%透明

Where the INT ranges from 0 (fully transparent) to 255 (fully opaque).


  <TextView
            style="@style/TextAppearance.AppCompat.Caption"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:alpha="0.38"
            android:gravity="start"
            android:textStyle="bold"
            tools:text="1994|EN" />

android:α= " 0.38 "

Text View alpha property set 0.38 to your textView visibility is faid 

试试这段代码:)

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


所有十六进制值从100%到0%的alpha,您可以设置任何颜色的alpha值下面提到。如# 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

我们也可以用这种方法使透明。

白色颜色代码- 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**

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

只需在Android工作室编辑器中单击一种颜色,并提供Alpha值的百分比。

更多信息见下图


在Kotlin中,你可以像这样使用alpha,

   //Click on On.//
    view.rel_on.setOnClickListener{
        view.rel_off.alpha= 0.2F
        view.rel_on.alpha= 1F

    }

    //Click on Off.//
    view.rel_off.setOnClickListener {
        view.rel_on.alpha= 0.2F
        view.rel_off.alpha= 1F
    }

结果如屏幕截图所示。

希望这对你有所帮助。谢谢


我知道,这是个很老的问题。

如果你想使用颜色值,你也可以使用#ARGB的简短版本。其中A是alpha通道的值。

在白色的情况下,有以下透明度值:

#FFFF  -     0%
#EFFF  -   6,7%
#DFFF  -  13,3%
#CFFF  -  20,0%
#BFFF  -  26,7%
#AFFF  -  33,3%
#9FFF  -  40,0%
#FFF8  -  46,7%
#7FFF  -  53,3%
#6FFF  -  60,0%
#5FFF  -  66,7%
#4FFF  -  73,3%
#3FFF  -  80,0%
#2FFF  -  86,7%
#1FFF  -  93,3%
#0FFF  - 100,0%

所以你可以为TextView添加以下行20%的透明度:

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

如果你想用kotlin制作50%透明的颜色,

val percentage = 50f/100 //50%
ColorUtils.setAlphaComponent(resources.getColor(R.color.whatEverColor), (percentage * 255).toInt())

我建议使用alpha属性。

<TextView
   android:alpha="0.8" />

或者现在你可以使用选择器。在colors包中创建background_color_25.xml。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:alpha="0.8" android:color="@color/background_color" />
</selector>

用法如下:

<TextView
   android:background="@color/background_color_25" />