Skip to content
This repository was archived by the owner on Mar 22, 2022. It is now read-only.

Development Notes

Lutkin Wang edited this page Sep 27, 2021 · 3 revisions

Transparency level of the background of a view

To set the transparency level of the background of a view. See: https://aresei-note.com/4257

Viewの背景を半透明にする方法としては以下の2種類の方法があります。

  • Alpha値を設定する
  • 8桁色指定を使用する

Alpha 値

静的に実装する場合

Alpha値を設定することでViewの透明度を操作することができます。 Alpha値は0.00(完全透明)~1.00(完全不透明)の範囲で相対的に設定できます。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    >
 
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@mipmap/ic_launcher"
        />
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        >
 
        <View
            android:id="@+id/v1"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:background="#000000"
            android:alpha="0.25"
            />
 
        <View
            android:id="@+id/v2"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:background="#000000"
            android:alpha="0.50"
            />
 
        <View
            android:id="@+id/v3"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:background="#000000"
            android:alpha="0.75"
            />
 
        <View
            android:id="@+id/v4"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:background="#000000"
            android:alpha="1.00"
            />
 
    </LinearLayout>
 
</FrameLayout>

動的に実装する場合

動的に実装する場合はViewオブジェクトのsetAlphaメソッドを使用します。 以下がサンプルコードです。

MainActivity.java

public class MainActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        findViewById(R.id.v1).setAlpha(0.25f);
        findViewById(R.id.v2).setAlpha(0.50f);
        findViewById(R.id.v3).setAlpha(0.75f);
        findViewById(R.id.v4).setAlpha(1.00f);
    }
}

8桁色

通常、色を指定する場合は黒色であれば#000000のように6桁の16進数で指定します。

実は色指定は8桁まで使用することができ、上位2桁は透明度を表します。 上位2桁の不透明度は00(完全透明)~FF(完全不透明)の相対値で指定します。 (※増減がAlpha値と逆なので注意!!)

例えば、Alpha値=0.25, 0.50, 0.75, 1.00相当の8桁色指定は #3F000000, #7F000000, #BF000000, #FF000000となります。

静的に実装する場合

静的に実装する場合はViewのbackground属性にさきほどの8桁色指定をします。 以下がサンプルコード・サンプル画像です。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    >
 
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@mipmap/ic_launcher"
        />
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        >
 
        <View
            android:id="@+id/v1"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:background="#3F000000"
            />
 
        <View
            android:id="@+id/v2"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:background="#7F000000"
            />
 
        <View
            android:id="@+id/v3"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:background="#BF000000"
            />
 
        <View
            android:id="@+id/v4"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:background="#FF000000"
            />
 
    </LinearLayout>
 
</FrameLayout>

動的に実装する場合

動的に実装する場合はさきほどの8桁色指定をViewオブジェクトのsetBackgourndColorメソッドの引数に渡します。

MainActivity.java

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        findViewById(R.id.v1).setBackgroundColor(0x3f000000);
        findViewById(R.id.v2).setBackgroundColor(0x7f000000);
        findViewById(R.id.v3).setBackgroundColor(0xbf000000);
        findViewById(R.id.v4).setBackgroundColor(0xff000000);
    }
}