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

วิธีสร้าง Progress Bar แบบกำหนดเอง (Custom) ใน Android

การสร้าง Progress Bar แบบกำหนดเอง (Custom ProgressBar) ใน Android

ตัวอย่างนี้สาธิตวิธีสร้าง Progress Bar รูปวงกลม (Circular) ด้วย Drawable XML แบบ Ring แล้วนำมาใช้กับ ProgressBar widget

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

เปิด Android Studio เลือก File → New Project และกรอกข้อมูลที่จำเป็น

ขั้นตอนที่ 2: จัดทำ Layout (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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">
    <ProgressBar
        android:layout_width="150dp"
        android:layout_height="150dp"
        style="?android:progressBarStyleLarge"
        android:progress="50"
        android:id="@+id/progressBar"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:progressDrawable="@drawable/circle" />
</androidx.constraintlayout.widget.ConstraintLayout>

ขั้นตอนที่ 3: สร้าง Drawable Resource (res/drawable/circle.xml)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:innerRadiusRatio="2.5"
    android:thickness="4dp"
    android:useLevel="true">
    <solid android:color="@color/colorAccent" />
    <!-- หรือใช้ gradient สำหรับสีสวยงาม -->
    <!--
    <gradient
        android:startColor="#FF6B6B"
        android:endColor="#4ECDC4"
        android:type="sweep" />
    -->
</shape>

ขั้นตอนที่ 4: เขียนโค้ดหลัก (src/MainActivity.java)

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {
    ProgressBar progressBar;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        progressBar = findViewById(R.id.progressBar);
        progressBar.setMax(100);
        progressBar.setProgress(20);
        
        // ตัวอย่าง: จำลองโหลดข้อมูล
        simulateLoading();
    }
    
    private void simulateLoading() {
        new Thread(() -> {
            for (int i = 20; i <= 100; i += 10) {
                final int progress = i;
                try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); }
                runOnUiThread(() -> progressBar.setProgress(progress));
            }
        }).start();
    }
}

ขั้นตอนที่ 5: กำหนดค่าใน AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.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>

การทดสอบ

รันแอปพลิเคชัน จะเห็น Progress Bar วงกลม แสดงความคืบหน้า 20% แล้วเพิ่มขึ้นจนถึง 100%

วิธีสร้าง Progress Bar แบบกำหนดเอง (Custom) ใน Android

การปรับแต่ง ProgressBar แบบ Ring (Shape Attributes)

Attribute คำอธิบาย ตัวอย่าง
android:shape="ring" รูปทรงแหวน (สำหรับ ProgressBar วงกลม) จำเป็น
android:innerRadiusRatio อัตราส่วนรัศมีภายใน (ยิ่งน้อย = แหวนหนา) 2.5, 3.0, 4.0
android:thickness ความหนาของแหวน (dp) 4dp, 8dp, 12dp
android:useLevel="true" ใช้ level สำหรับ progress (จำเป็น) true

การใช้ Gradient สีสวยงาม (Sweep Gradient)

<shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:innerRadiusRatio="3"
    android:thickness="6dp"
    android:useLevel="true">
    <gradient
        android:type="sweep"
        android:startColor="#FF6B6B"
        android:centerColor="#FFD93D"
        android:endColor="#4ECDC4"
        android:centerX="0.5"
        android:centerY="0.5" />
</shape>

Indeterminate ProgressBar (หมุนวนลูป)

<ProgressBar
    android:layout_width="50dp"
    android:layout_height="50dp"
    style="?android:progressBarStyleLarge"
    android:indeterminate="true"
    android:indeterminateDrawable="@drawable/custom_spinner" />

Material Design Progress Indicators (แนะนำใหม่)

ใช้ Material Components Library:

<!-- Circular -->
<com.google.android.material.progressindicator.CircularProgressIndicator
    android:layout_width="64dp"
    android:layout_height="64dp"
    app:indicatorSize="64dp"
    app:trackThickness="4dp"
    app:indicatorColor="@color/colorPrimary"
    app:trackColor="@color/colorPrimaryTransparent" />

<!-- Linear -->
<com.google.android.material.progressindicator.LinearProgressIndicator
    android:layout_width="match_parent"
    android:layout_height="8dp"
    app:trackThickness="4dp"
    app:indicatorColor="@color/colorAccent" />