android ProgressBar定製模式,自訂模式動畫檔案,androidprogressbar
1.indeterminate mode | 沒有具體進度的進度條
1.1 定製動畫檔案
[html] view plaincopy
- <ProgressBar
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:indeterminateDrawable="@drawable/progress_my_style"
- style="?android:attr/progressBarStyle"
- />
註:style="?android:attr/progressBarStyle" 這是預設樣式 ,可換改
step2: 在drawable檔案夾下建立progress_my_style.xml檔案:內容可如下:
[html] view plaincopy
- <animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
- android:drawable="@drawable/spinner_color"
- android:pivotX="50%"
- android:pivotY="50%"
- />
注意:
1. android:drawable="@drawable/spinner_color" 這裡,你需要在drawable下建立spinner_color.png圖片(自己畫成你所想到的效果(漸層))
2. progress_my_style.xml檔案內容參考至D:\andrirod\android-sdk-windows\platforms\android-7\data\res\drawable\progress_medium_white.xml檔案內容,
android:framesCount與
android:frameDuratiion是frameworks內部的屬性,無法直接使用,所以省略)
以上是在XML中直接定義,下面是我在代碼裡直接定義的:
[java] view plaincopy
- mProgressBar.setIndeterminateDrawable(AppConfig.getResources().getDrawable(R.drawable.style_common_processbar));
style_common_processbar.xml還是需要定義在xml裡的
[html] view plaincopy
- <?xml version="1.0" encoding="UTF-8"?>
- <animated-rotate
- android:drawable="@drawable/icon"
- android:pivotX="50.0%"
- android:pivotY="50.0%"
- xmlns:android="http://schemas.android.com/apk/res/android">
- </animated-rotate>
結果是就是用圖片icon旋轉來表示進度,圖片製作參考 android的 drawable_hdpi 下的 spinner_black_48.png
1.2 定製顏色和形狀
利用shape來定製顏色和形狀
[java] view plaincopy
- // 定製模式
- mProgressBar.setIndeterminateDrawable(AppConfig.getResources().getDrawable(R.drawable.style_common_processbar));
style_common_processbar.xml
[html] view plaincopy
- <?xml version="1.0" encoding="UTF-8"?>
- <rotate
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:duration="30"
- android:fromDegrees="0.0"
- android:toDegrees="360.0"
- android:pivotX="50.0%"
- android:pivotY="50.0%"
- android:repeatCount="infinite">
- <shape
- android:shape="ring"
- android:innerRadiusRatio="3.2"
- android:thicknessRatio="5.333"
- android:useLevel="false">
- <size
- android:height="16.0dip"
- android:width="16.0dip" />
- <gradient
- android:startColor="#4cffffff"
- android:endColor="#ffff0000"
- android:useLevel="false"
- android:type="sweep"
- android:centerY="0.5"
- android:centerColor="#4cffffff" />
- </shape>
- </rotate>
通過修改
android:shape=["rectangle" | "oval" | "line" | "ring"] 來修改形狀
gradient來修改顏色屬性
2.progress mode | 有百分比進度的進度條
代碼調用:
mProgressBar.setProgressDrawable(AppConfig.getResources().getDrawable(R.drawable.color));
color.xml
[html] view plaincopy
- <?xml version="1.0" encoding="UTF-8"?>
- <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:id="@android:id/background" android:drawable="@drawable/bg" />
- <item android:id="@android:id/secondaryProgress" android:drawable="@drawable/secondary" />
- <item android:id="@android:id/progress" android:drawable="@drawable/progress" />
- </layer-list>