ตัวอย่างนี้สาธิตวิธีตั้งค่าความโปร่งใส (Alpha/Opacity) ให้ View ใน Android ทั้งแบบ Static (XML) และ Dynamic (Animation)
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
Android Studio → File → New Project
ขั้นตอนที่ 2: Layout (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="86dp"
android:background="#011ffd"
android:gravity="center"
android:text="TextView"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp"
android:text="Set Alpha Animation (0.6)" />
</RelativeLayout>
ขั้นตอนที่ 3: MainActivity.java (ใช้ ObjectAnimator)
package com.example.sample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.animation.ObjectAnimator;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView txt;
Button btn;
ObjectAnimator objectAnimator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = findViewById(R.id.textView1);
btn = findViewById(R.id.button1);
// สร้าง Animator สำหรับ Property "alpha" (0.0 - 1.0)
objectAnimator = ObjectAnimator.ofFloat(txt, "alpha", 0.6f);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
objectAnimator.setDuration(4000); // 4 วินาที
objectAnimator.start();
}
});
}
}
วิธีตั้งค่า Alpha แบบต่างๆ
1. XML แบบ Static (API 11+)
<TextView
...
android:alpha="0.5" /> <!-- 50% โปร่งใส -->
2. Java Code แบบ Static
view.setAlpha(0.5f); // 0.0 = โปร่งใสหมด, 1.0 = ทึบเต็มที่
3. Animation แบบ Dynamic (ObjectAnimator - API 11+)
// Animator แบบง่าย
ObjectAnimator.ofFloat(view, "alpha", 1f, 0.3f, 1f)
.setDuration(2000)
.start();
// แบบมี Listener
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "alpha", 0f, 1f);
animator.setDuration(1000);
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
// ทำงานเมื่อจบ
}
});
animator.start();
4. Alpha Animation แบบเก่า (View Animation - API 1+)
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.3f); alphaAnimation.setDuration(2000); alphaAnimation.setFillAfter(true); // คงสถานะหลังจบ view.startAnimation(alphaAnimation);
ขั้นตอนที่ 4: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.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 vs Visibility
| คุณสมบัติ | Alpha (0f) | Visibility (GONE) |
|---|---|---|
| พื้นที่ Layout | ยังคงครองพื้นที่ | ไม่ครองพื้นที่ |
| รับ Touch Event | รับได้ (ถ้า clickable) | ไม่รับ |
| Performance | ดีกว่า (GPU) | ต้อง Layout ใหม่ |
การทดสอบ
รันแอป → กดปุ่ม "Set Alpha Animation" → TextView จะจางลงเหลือ 60% (Alpha 0.6) ภายใน 4 วินาที