Androidで背景を20%透明にする方法


回答:


1038

アルファチャネルの色が80%になるようにします。たとえば、赤の使用の場合#CCFF0000:

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

例でCCは、はの16進数です255 * 0.8 = 204。最初の2桁の16進数はアルファチャネル用であることに注意してください。形式は#AARRGGBBで、AAはアルファチャネル、RRは赤のチャネル、GGは緑のチャネル、BBは青のチャネルです。

20%の透明度は80%の不透明度を意味すると想定しています。逆の意味の場合は、代わりにを16進数でCC使用33し255 * 0.2 = 51ます。

アルファ透明度値の適切な値を計算するには、次の手順に従います。

  1. 透明度のパーセンテージ、たとえば20%が与えられた場合、不透明度のパーセンテージの値は80%であることがわかります(これは100-20=80)
  2. アルファチャネルの範囲は8ビット(2^8=256)で、範囲は0〜255です。
  3. 不透明なパーセンテージをアルファ範囲に投影します。つまり、範囲(255)にパーセンテージを掛けます。この例では255 * 0.8 = 204。必要に応じて、最も近い整数に丸めます。
  4. 3.で得られた値(基数は10)を16進数(基数16)に変換します。これまたは任意の計算機にGoogleを使用できます。Googleを使用して「204 to hexa」と入力すると、16進数値が表示されます。この場合はそうです0xCC。
  5. 4.で取得した値を目的の色に付加します。たとえば、赤のFF0000場合はになりますCCFF0000。

色については、Androidのドキュメントをご覧ください。


透明度のある白色の場合は、質問に応じてを使用してくださいandroid:background="#CCFFFFFF"。
— gotwo

このルールを画像付きの背景に適用するにはどうすればよいですか?
— user1090751

1517

黒には以下のコードを使用します。

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

不透明度を使用したい場合は、以下のコードを使用できます。

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

そして、以下の不透明度コード:すべての不透明度レベルはこちら

16進数の不透明度の値

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

3
これを見て、私は#99ffffffを使用し、それは私のために
— うまくいき

このルールを画像付きの背景に適用するにはどうすればよいですか?
— user1090751

149

色の定義の最初の2文字を変更して、色の不透明度を管理できます。

#99 000000

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 

1
これは既存の回答に何を追加しますか?
— Code-Apprentice

6
コード-見習い私は、あなたが、87%または54%のような、よりきめの細かい透明性を必要とするとき、この答えは便利だと思います@で述べたもののようmaterial.google.com/style/color.html#color-color-schemes
— ブルース・

5
はい、昨日は13%のアルファ版を管理する必要があり、検索結果をこの便利なスレッドに追加することにしました。
— carlol

LinearLayoutを色なしで完全に透明にしたい場合はどうなりますか?
— Si8

1
すべてのビューはデフォルトで透明です。プログラムでmLinearLayout.setBackgroundColor(Color.TRANSPARENT);を使用できます。または、何らかの理由で強制する必要がある場合は、xmlでandroid:background = "@ android:color / transparent"にします(明らかに、この
— 色は子

106

のようなアルファ値を持つ色を#33------使用し、XML属性を使用してeditTextの背景として設定しますandroid:background=" "。

  1. 0%(透明)->#00(16進数)
  2. 20%->#33
  3. 50%->#80
  4. 75%->#C0
  5. 100%(不透明)-> #FF

255 * 0.2 = 51→16進数33


91

あなたは次のようなことを試すことができます:

textView.getBackground().setAlpha(51);

ここでは、不透明度を0(完全に透明)〜255(完全に不透明)に設定できます。51はまさに20%です。


17
@koti、textView変数はなのでnull。
— yugidroid 2013

素敵なエレガントなソリューション。これを選択すると、リストアイテムの背景にこれを適用できます。そのため、色が出てきますが、アイテムテキストとは競合しません。
— Gene Bo

setAlpha(int)は非推奨になり、setAlpha(float)が優先されます。0は完全に透明で、1は完全に不透明ですmyImage.setAlpha(0.5f);
— ポルマブリ

@polmabri、そうではない。myImage.setAlpha(0.5f);アルファをビュー自体に適用しますが、私の回答で示すものはビューの背景ドローアブルに適用されています。
— yugidroid

これはグラフィックのバグを引き起こす可能性があります!注意深く多くのテストを行って使用してください。
— マイケル


32

スクリーンショットを見る

3つのビューを取得しました。最初のビューではフル(アルファなし)カラーを設定し、2番目のビューではハーフ(0.5アルファ)カラーを設定し、3番目のビューでは明るいカラー(0.2アルファ)を​​設定しました。

以下のコードを使用して、任意の色を設定し、アルファで色を取得できます。

ファイル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;
    }
}

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

できた


31

逆に透明にすることもできます。

ホワイトカラーコード-FFFFFF

白20%-#33 FFFFFF

20%— 33

70%ホワイト-#B3 FFFFFF

70%— B3

100%から0%までのすべての16進値

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


1
すばらしい答え。がんばり続ける。
— Tarun

22

100%から0%アルファまでのすべての16進値。以下に説明するアルファ値で任意の色を設定できます。例:#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

19

今のAndroid Studioの3.3以降のバージョンが変更する作り付けの機能を提供アルファ色の値を、

Androidスタジオエディターで色をクリックし、でアルファ値を指定するだけpercentageです。

詳細については、下の画像を参照してください

ここに画像の説明を入力してください


18

alphadouble値を取るXML値があります。

API 11+範囲は0f〜を1f含むため、0f透明1fで不透明です。

  • android:alpha="0.0" 見えない

  • android:alpha="0.5" シースルー

  • android:alpha="1.0" 完全に見える

それはそれが機能する方法です。


1
内容全体を含め、レイアウト全体が半透明になります
— jack_the_beast 2015年

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

Android API 11以降のアルファ範囲は0(透明)〜1(不透明)


3

下記の人気度を見る

     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:alpha = "0.38"

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

3

Kotlinでは、このようなアルファを使用して、

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

結果はこのスクリーンショットのようになります。20%透明

これがお役に立てば幸いです。ありがとう


1

私は知っています、それは非常に古い質問です。

色の値を使用したい場合は、その短いバージョンをで使用することもできます#ARGB。Aアルファチャネルの値はどこにありますか。

白色の場合、次の透明度の値があります。

#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"
    ... />

0

これは、アルファチャネルの16進値を計算するための@Aromeroの回答からのプログラムによる解決策です。:)

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

3
質問はAndroidにタグ付けされています。このコードはAndroidをサポートしていません。
— Pawan、2016年


0

Kotlinで色を50%透明にしたい場合は、

val percentage = 50f/100 //50%
ColorUtils.setAlphaComponent(resources.getColor(R.color.whatEverColor), (percentage * 255).toInt())
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.