Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีทำพื้นหลังโปร่งแสง 25% ใน Android

การตั้งค่าความโปร่งใส (Transparency/Alpha) ให้กับ View หรือ Layout

ตัวอย่างนี้แสดงวิธีใช้แอตทริบิวต์ android:alpha เพื่อทำให้พื้นหลังโปร่งใส 25% (ค่า 0.25)

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

Android Studio → File → New Project → กรอกรายละเอียด → Finish

ขั้นตอนที่ 2: Layout - res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:alpha="0.25"
    android:background="@drawable/stones"
    tools:context=".MainActivity" />

ขั้นตอนที่ 3: MainActivity.java

package app.tutorialspoint.com.sample;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

ขั้นตอนที่ 4: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.tutorialspoint.com.sample">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

ค่าของ Alpha (0.0 - 1.0)

ค่า Alphaความโปร่งใสความมองเห็น
1.00%ไม่โปร่งใส (ปกติ)
0.7525%โปร่งใสเล็กน้อย
0.550%โปร่งใสปานกลาง
0.2575%โปร่งใสมาก (เห็นได้น้อย)
0.0100%โปร่งใสสมบูรณ์ (มองไม่เห็น)

วิธีอื่นๆ ในการทำความโปร่งใส

1. ใช้สีพร้อม Alpha (ARGB) ใน XML

<!-- Format: #AARRGGBB -->
android:background="#40000000" <!-- 25% black (40 hex = 64/255 ≈ 25%) -->
android:background="#80FF0000" <!-- 50% red -->
android:background="#CCFFFFFF" <!-- 80% white -->

2. ใช้โค้ด (Programmatically)

// Alpha 0-255
view.getBackground().setAlpha(64); // 25%
view.setAlpha(0.25f); // ทั้ง View รวม child views

// สีพร้อม Alpha
int color = Color.argb(64, 0, 0, 0); // 25% black
view.setBackgroundColor(color);

3. ใช้ Theme/Style สำหรับ Dialog/Activity

<style name="TransparentDialog" parent="Theme.AppCompat.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item>
</style>

⚠️ ข้อควรระวัง

  • android:alpha ทำให้ View ลูก (Children) โปร่งใสด้วย
  • หากต้องการแค่พื้นหลัง ใช้ Background Color แบบ ARGB แทน
  • Performance: Alpha ทำให้ต้อง render off-screen buffer (อาจช้าลงเล็กน้อย)
  • Clickable: View ที่ alpha=0 ยังรับ touch event ได้ (ใช้ android:clickable="false" ถ้าต้องการปิด)

การทดสอบ

เตรียมรูป @drawable/stones ใน res/drawable/ → รันแอป → จะเห็นรูปพื้นหลังโปร่งใส 25%