program story

Android에서 배경을 20 % 투명하게 만드는 방법

inputbox 2020. 10. 3. 10:36
반응형

Android에서 배경을 20 % 투명하게 만드는 방법


배경에 Textview색상이있는 (예 : 흰색) 약 20 % 투명 (완전히 투명하지 않음) 의 배경을 어떻게 만들 수 있습니까?


알파 채널에서 색상이 80 %가되도록합니다. 예를 들어, 빨간색 사용 #CCFF0000:

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

이 예 CC에서은의 16 진수입니다 255 * 0.8 = 204. 처음 두 16 진수는 알파 채널 용입니다. 형식은입니다 #AARRGGBB. 여기서는 AA알파 채널, RR빨간색 채널, GG녹색 채널 및 BB파란색 채널입니다.

20 % 투명은 80 % 불투명을 의미한다고 가정합니다. 당신은 다른 방법 대신에 의미하는 경우 CC사용 33의 16 진수입니다 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 문서를 살펴볼 수 있습니다 .


검정색에는 아래 코드를 사용하십시오.

<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.transparentColor10(R.color.border_color)));

색상 정의에서 처음 두 문자를 변경하여 색상 불투명도를 관리 할 수 ​​있습니다.

# 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 

와 같은 알파 값이있는 색상을 #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


다음과 같이 시도 할 수 있습니다.

textView.getBackground().setAlpha(51);

여기서 불투명도를 0 (완전 투명)에서 255 (완전 불투명) 사이로 설정할 수 있습니다. 51은 정확히 원하는 20 %입니다.


Android Studio에는 색상 및 알파 / 불투명도 값을 조정하는 내장 도구가 있습니다 .

Android 조정 색상 불투명도


스크린 샷보기

나는 세 가지 견해를 가지고 있습니다. 첫 번째 뷰에서는 전체 (알파 없음) 색상을 설정하고 두 번째 뷰에서는 절반 (0.5 알파) 색상을 설정하고 세 번째 뷰에서는 밝은 색상 (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))
}

끝난


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

alpha이중 값을 취하는 XML 값 이 있습니다.

이후 API 11+범위 내지 0f1f(포함), 0f투명성 및 1f불투명 인 :

  • android:alpha="0.0" 그것은 보이지 않는다

  • android:alpha="0.5" 시스루

  • android:alpha="1.0" 완전 가시

그것이 작동하는 방식입니다.


이런 식으로도 투명하게 만들 수 있습니다.

화이트 색상 코드-FFFFFF

70 % 흰색-# B3 FFFFFF.

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 Studio 3.3 이상 버전 은 색상 알파을 변경하는 내장 기능을 제공합니다 .

Android 스튜디오 편집기에서 색상을 클릭 하고percentage .

자세한 내용은 아래 이미지를 참조하십시오.

여기에 이미지 설명 입력


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

Android API 11 이상에서 알파 범위는 0 (투명)과 1 (불투명) 사이입니다.


이것을 사용하여 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 : alpha = "0.38"

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

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 % 투명

이것이 당신을 도울 수 있기를 바랍니다.


다음 은 알파 채널의 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();
}

이 코드를 시도하십시오 :)

완전히 투명한 16 진수 코드 - "# 00000000"

참고 URL : https://stackoverflow.com/questions/11285961/how-to-make-a-background-20-transparent-on-android

반응형