天天看點

Android開發之shape自定義ProgressBar進度條樣式

1.xml代碼方式一:如下(此shape是放到drawable目錄下的)

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="360">
    <shape
        android:innerRadiusRatio="3"
        android:shape="ring"
        android:thicknessRatio="8"
        android:useLevel="false">
        <gradient
            android:centerColor="#ff0000"
            android:endColor="#111111"
            android:startColor="#B23AEE"
            android:type="sweep"
            android:useLevel="false" />
    </shape>
</rotate>
           

2.看下效果圖:

Android開發之shape自定義ProgressBar進度條樣式

xml代碼方式二如下:

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="360">

    <shape
        android:innerRadiusRatio="3"
        android:shape="ring"
        android:thicknessRatio="20"
        android:useLevel="false">
        <gradient
            android:centerColor="#66FF0000"
            android:endColor="#FF0000"
            android:startColor="#FFFFFF"
            android:type="sweep" />
    </shape>

</rotate>

           
Android開發之shape自定義ProgressBar進度條樣式

然後直接在progressBar中的indeterminateDrawable屬性中使用即可

Android開發之shape自定義ProgressBar進度條樣式
<ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminateDrawable="@drawable/custom_progressbar"
        app:layout_constraintBottom_toTopOf="@+id/btOpen"
        app:layout_constraintEnd_toEndOf="@+id/btOpen"
        app:layout_constraintHorizontal_bias="0.496"
        app:layout_constraintStart_toStartOf="@+id/btOpen"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.34" />