ProgressBar
參考文章:
Android 高手進(jìn)階之自定義View,自定義屬性(帶進(jìn)度的圓形進(jìn)度條)
android自定義進(jìn)度條漸變色View的實(shí)例代碼
1鹦付、android.widget. ProgressBar,繼承自android.view.View 择卦。在android.widget包中敲长。對(duì)應(yīng)對(duì)話框ProgressDialog。ProgressBar有兩種展示方式秉继,表盤形式(普通祈噪、小、大)和條形填充形式尚辑。在layout定義時(shí)辑鲤,需要通過設(shè)施style屬性類設(shè)置展示方式。
XML屬性
屬性名 | 描述 |
---|---|
android:animationResolution | 超時(shí)的動(dòng)畫幀之間的毫秒 杠茬;必須是一個(gè)整數(shù)值,如“100”月褥。 |
android:indeterminate | 是否允許使用不確定模式弛随,在不確定模式下,進(jìn)度條動(dòng)畫無限循環(huán) |
android:indeterminateBehavior | 定義當(dāng)進(jìn)度達(dá)到最大時(shí)宁赤,不確定模式的表現(xiàn)舀透;該值必須為repeat或者cycle,repeat表示進(jìn)度從0重新開始决左;cycle表示進(jìn)度保持當(dāng)前值愕够,并且回到0 |
android:indeterminateDrawable | 定義不確定模式是否可拉 |
android:indeterminateDuration | 時(shí)間不定的動(dòng)畫 |
android:indeterminateOnly | 限制為不定模式 |
android:interpolator | |
android:max | 定義進(jìn)度的最大值 |
android:maxHeight | 進(jìn)度Widget最大高 |
android:miniHeight | 進(jìn)度Widget最小高 |
android:maxWidth | 進(jìn)度Widget最大寬 |
android:minWidth | 進(jìn)度Widget最小寬 |
android:mirrorForRtl | 定義了相關(guān)畫板如果需要反映在RTL模式 |
android:progress | 設(shè)置進(jìn)度的默認(rèn)值,值介于0到max之間 |
android:progressDrawable | |
android:secondaryProgress | 定義二級(jí)進(jìn)度值佛猛,值介于0到max惑芭。該進(jìn)度在主進(jìn)度和背景之間。比如用于網(wǎng)絡(luò)播放視頻時(shí)继找,二級(jí)進(jìn)度用于表示緩沖進(jìn)度遂跟,主進(jìn)度用于表示播放進(jìn)度。 |
ProgressBar的樣式有四種:
android:progressBarStyle:默認(rèn)進(jìn)度條樣式码荔,不確定模式
android:progressBarStyleHorizontal:水平進(jìn)度條樣式
android:progressBarStyleLarge :大號(hào)進(jìn)度條樣式漩勤,也是不確定進(jìn)度模式
android:progressBarStyleSmall :小號(hào)進(jìn)度條樣式感挥,也是不確定進(jìn)度模式
2缩搅、XML重要屬性
android:max-- 這事進(jìn)度條長(zhǎng)度最大值
android:progress--設(shè)定度條當(dāng)前進(jìn)度值
android:secondaryProgress--第二進(jìn)度條進(jìn)度值
android:progressBarStyle:默認(rèn)進(jìn)度條樣式
android:progressBarStyleHorizontal:水平樣式
style="?android:attr/progressBarStyleLarge" --- 屬性風(fēng)格類型--大圓圈,如下圖
style=”?android:attr/progressBarStyleSmall”--- 屬性風(fēng)格類型--小圓圈
3触幼、重要方法
getMax():返回這個(gè)進(jìn)度條的范圍的上限
getProgress():返回當(dāng)前進(jìn)度值
getSecondaryProgress():返回次要當(dāng)前進(jìn)度值
incrementProgressBy(int diff):指定增加的進(jìn)度--即步長(zhǎng)
isIndeterminate():指示進(jìn)度條是否在不確定模式下
setIndeterminate(boolean indeterminate):設(shè)置不確定模式下
setVisibility(int v):設(shè)置該進(jìn)度條是否可視
4硼瓣、重要事件
onSizeChanged(int w, int h, int oldw, int oldh):當(dāng)進(jìn)度值改變時(shí)引發(fā)此事件
接下來看案例:
一、自定義progressbar顏色:
drawable 資源目錄下
1.定義一個(gè)圖片資源文件:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="400"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360">
<shape
android:innerRadiusRatio="3"
android:shape="ring"
android:thicknessRatio="10"
android:useLevel="false">
<gradient
android:centerColor="@color/progress_end"
android:centerY="0.50"
android:endColor="#00000000"
android:startColor="@color/progress"
android:type="sweep"
android:useLevel="false"/>
</shape>
</rotate>
2.定義布局文件:
<ProgressBar
android:indeterminateDrawable="@drawable/color_progress2"
android:indeterminateOnly="true"
android:indeterminateDuration="500"
android:indeterminateBehavior="repeat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ProgressBar>
說明:indeterminateDuration 設(shè)置轉(zhuǎn)動(dòng)時(shí)間
二置谦、自定義漸變色進(jìn)度條:
定義drawable資源文件color_progressbar.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<corners android:radius="5dip" />
<gradient android:startColor="#ff9d9e9d"
android:centerColor="#ff5a5d5a"
android:centerY="0.75"
android:endColor="#ff747674"
android:angle="270"
/>
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient android:startColor="#80ffd300"
android:centerColor="#80ffb600"
android:centerY="0.75"
android:endColor="#a0ffcb00"
android:angle="270"
/>
</shape>
</clip>
</item>
<item android:id="@android:id/progress"
>
<clip>
<shape>
<corners android:radius="5dip" />
<gradient android:startColor="#FF3030"
android:endColor="#AEEEEE"
android:angle="270" />
</shape>
</clip>
</item>
</layer-list>
2.定義對(duì)應(yīng)的不布局文件:progressbar.xml在此文件中引用我們定義的drawable資源配置文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<ProgressBar
android:id="@+id/color_progressBar"
android:indeterminateDrawable="@drawable/color_progress"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
</LinearLayout>
三堂鲤、用圖片實(shí)現(xiàn)滾動(dòng)效果:
1.添加圖片到drawable下
2.自定義圖片資源文件iamge_progress.xml
<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/image_progress"
android:pivotX="50%"
android:pivotY="50%" />
3.定義布局文件,progress.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<ProgressBar
android:indeterminateDrawable="@drawable/drawable_progress"
android:layout_height="100dp"
android:layout_width="100dp"/>
</LinearLayout>