การสร้าง ProgressBar แบบวงกลม (Circular/Determinate) ด้วย Drawable XML
ตัวอย่างนี้แสดงวิธีสร้าง ProgressBar วงกลมที่แสดงความคืบหน้าเป็นเปอร์เซ็นต์ (Determinate) โดยใช้ Shape Drawable และ Gradient แบบ Sweep
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
Android Studio → File → New Project → กรอกรายละเอียด → Finish
ขั้นตอนที่ 2: สร้าง Progress Drawable - res/drawable/circular_progress_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="https://schemas.android.com/apk/res/android"
android:fromDegrees="270"
android:toDegrees="270">
<shape
android:innerRadiusRatio="2.5"
android:shape="ring"
android:thickness="1dp"
android:useLevel="true">
<!-- แก้ไขปัญหาสำหรับ API 21 (Lollipop) -->
<gradient
android:angle="0"
android:endColor="#007DD6"
android:startColor="#007DD6"
android:type="sweep"
android:useLevel="false" />
</shape>
</rotate>
ขั้นตอนที่ 3: สร้าง Background Shape - res/drawable/circular_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android"
android:innerRadiusRatio="2.5"
android:shape="ring"
android:thickness="1dp"
android:useLevel="false">
<solid android:color="#CCCCCC" />
</shape>
ขั้นตอนที่ 4: Layout - res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_margin="16dp"
tools:context=".MainActivity">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
android:background="@drawable/circular_shape"
android:indeterminate="false"
android:max="100"
android:progress="65"
android:progressDrawable="@drawable/circular_progress_bar" />
</RelativeLayout>
ขั้นตอนที่ 5: MainActivity.java
package app.tutorialspoint.com.sample;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
ขั้นตอนที่ 6: 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>
คำอธิบายสำคัญ
- android:shape="ring" - สร้างรูปทรงวงแหวน
- android:innerRadiusRatio - อัตราส่วนรัศมีภายใน (ค่ามาก = วงแหวนบาง)
- android:thickness - ความหนาของวงแหวน
- android:type="sweep" - Gradient แบบวนรอบศูนย์กลาง
- android:useLevel="true" - ใช้ level เพื่อแสดงความคืบหน้า (สำหรับ progressDrawable)
- rotate 270° - เริ่มต้นจากตำแหน่ง 12 โมง (แทนที่จะเป็น 3 โมง)
การปรับแต่งเพิ่มเติม
// เปลี่ยน progress โดยโค้ด ProgressBar progressBar = findViewById(R.id.progressBar); progressBar.setProgress(80); // 0-100 // Animation ObjectAnimator animator = ObjectAnimator.ofInt(progressBar, "progress", 0, 100); animator.setDuration(3000); animator.start();