首先,在activity_main中定義個一個ProgressBar 控件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.blue.studyui.MainActivity">
<!--indeterminateDrawable 的樣式xml爲自己定義的xml-->
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="false"
android:indeterminateDrawable="@drawable/dialog_style_xml_color"
android:layout_marginTop="148dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
上面的dialog_style_xml_color.xml 定義了一個圓形的的進度條 代碼如下
<?xml version="1.0" encoding="utf-8"?>
<!--形狀ring(環形-->
<!--innerRadiusRation 浮點型,以環的寬度比率來表示內環的半徑-->
<!--thicknessRation 以環的寬度比率來表示環的厚度-->
<!--useLevel boolean值,如果當做是LevelListDrawable使用時值爲true,否則爲false.設置爲true無漸變。false有漸變色-->
<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="#FFDC35"
android:centerY="0.50"
android:endColor="#CE0000"
android:startColor="#FFFFFF"
android:type="sweep"
android:useLevel="false" />
</shape>
</rotate>
這樣就完成成功了,啓動後就會看到 一個圓形 三種顏色的進度條在旋轉了
如果需要把一張圖片放在進度條上面自己旋轉可以這樣定義
定義一個custom_progress_draw 的xml文件
<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/load"
android:pivotX="50%"
android:pivotY="50%" />
然後我們只需要把ProgressBar的android:indeterminateDrawable 換成 custom_progress_draw 這個佈局就可以了