进度条默认是不确定(indeterminate=true)Android进度条

Android进度条有4种风格可以使用。

默认值是progressBarStyle。 设置成progressBarStyleSmall后,图标变小。 设置成progressBarStyleLarge后,图标变大 设置成progressBarStyleHorizontal后,变成横向长方形。

自定义圆形进度条ProgressBar的一般有三种方式:

一、通过动画实现

定义res/anim/loading_anim.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<animation-list android:oneshot="false"
  xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:duration="150" android:drawable="@drawable/loading_blue_16_f0" />
  <item android:duration="150" android:drawable="@drawable/loading_blue_16_f1" />
  <item android:duration="150" android:drawable="@drawable/loading_blue_16_f2" />
  <item android:duration="150" android:drawable="@drawable/loading_blue_16_f3" />
  <item android:duration="150" android:drawable="@drawable/loading_blue_16_f4" />
  <item android:duration="150" android:drawable="@drawable/loading_blue_16_f5" />
  <item android:duration="150" android:drawable="@drawable/loading_blue_16_f6" />
  <item android:duration="150" android:drawable="@drawable/loading_blue_16_f7" />
  <item android:duration="150" android:drawable="@drawable/loading_blue_16_f8" />
  <item android:duration="150" android:drawable="@drawable/loading_blue_16_f9" />
  <item android:duration="150" android:drawable="@drawable/loading_blue_16_f10" />
  <item android:duration="150" android:drawable="@drawable/loading_blue_16_f11" />
  <item android:duration="150" android:drawable="@drawable/loading_blue_16_f12" />
  <item android:duration="150" android:drawable="@drawable/loading_blue_16_f13" />
  <item android:duration="150" android:drawable="@drawable/loading_blue_16_f14" />
  <item android:duration="150" android:drawable="@drawable/loading_blue_16_f15" />
  <item android:duration="150" android:drawable="@drawable/loading_blue_16_f16" />
  <item android:duration="150" android:drawable="@drawable/loading_blue_16_f17" />
</animation-list>

在layout文件中引用如下:

<ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="20dip"
        android:layout_marginTop="20dip"
        android:indeterminate="false"
        android:indeterminateDrawable="@anim/loading_anim" />

二、通过自定义颜色实现

定义res/drawable/loading_color.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="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>

在layout文件中引用如下:

<ProgressBar
        android:id="@+id/progressBar2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminate="false"
        android:indeterminateDrawable="@drawable/loading_color" />

三、使用一张图片进行自定义

定义res/drawable/loading_img.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <rotate
            android:drawable="@drawable/exchange_loading"
            android:fromDegrees="0.0"
            android:pivotX="50.0%"
            android:pivotY="50.0%"
            android:toDegrees="360.0" />
    </item>
</layer-list>

在layout文件中引用如下:

    <ProgressBar
        android:id="@+id/progressBar3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminate="false"
        android:indeterminateDrawable="@drawable/lodaing_img"
        />