ภาพรวม
ตัวอย่างนี้สาธิตวิธีเปลี่ยนสีของ Drawable ใน Android โดยใช้ AnimationDrawable เพื่อสร้างเอฟเฟกต์เปลี่ยนสีแบบไล่ระดับ (Gradient Animation) บนพื้นหลัง Layout
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
เปิด Android Studio ไปที่ File → New Project แล้วกรอกข้อมูลที่จำเป็นเพื่อสร้างโปรเจกต์ใหม่
ขั้นตอนที่ 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: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"
android:background="@drawable/background"
android:id="@+id/activity_main"
android:orientation="vertical"
tools:context=".MainActivity"
tools:ignore="InvalidId">
</LinearLayout>
ขั้นตอนที่ 3: เขียนโค้ดหลัก (src/MainActivity.java)
package com.app.sample;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout yourLayout = findViewById(R.id.activity_main);
AnimationDrawable animationDrawable = (AnimationDrawable) yourLayout.getBackground();
// ตั้งค่าเวลา Fade In/Out (มิลลิวินาที)
animationDrawable.setEnterFadeDuration(4000);
animationDrawable.setExitFadeDuration(4000);
// เริ่มอนิเมชัน
animationDrawable.start();
}
}
ขั้นตอนที่ 4: สร้าง Animation List (res/drawable/background.xml)
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="https://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/gradient1"
android:duration="4000" />
<item
android:drawable="@drawable/gradient2"
android:duration="4000" />
</animation-list>
ขั้นตอนที่ 5: สร้าง Gradient แรก (res/drawable/gradient1.xml)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
<gradient
android:angle="225"
android:endColor="#044fab"
android:startColor="#21d6d3" />
</shape>
ขั้นตอนที่ 6: สร้าง Gradient ที่สอง (res/drawable/gradient2.xml)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
<gradient
android:angle="45"
android:endColor="#933c94"
android:startColor="#517f95" />
</shape>
ขั้นตอนที่ 7: กำหนดค่า Manifest (Manifests/AndroidManifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.app.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>
การทดสอบ
เชื่อมต่ออุปกรณ์ Android จริงกับคอมพิวเตอร์ ใน Android Studio เปิดไฟล์ Activity แล้วคลิกไอคอน Run - พื้นหลังจะเปลี่ยนสีแบบไล่ระดับไปมาอย่างต่อเนื่อง