先來看看 ProgressBar 的常用屬性:
- android:max:設(shè)置 ProgressBar 的最大值,默認(rèn)是 100。
- android:indeterminate:設(shè)置是否開啟不確定模式。true 表示進(jìn)度條會展示實際的進(jìn)度;而 false 表示在加載時會無限循環(huán)展示 loading 動畫。
- android:minHeight:設(shè)置最小高度。
- android:minWidth:設(shè)置最小寬度。
- android:progress:設(shè)置進(jìn)度條的當(dāng)前進(jìn)度。
-
style:設(shè)置進(jìn)度條的樣式,默認(rèn)情況下展示一個循環(huán)轉(zhuǎn)圈的 loading 樣式,而如果需要設(shè)置其他樣式,就要用的 style 屬性,比如通過設(shè)置
style
為android:attr/progressBarStyleHorizontal
可以設(shè)置成橫向進(jìn)度條的樣式。 - android:progressDrawable:設(shè)置進(jìn)度條對應(yīng)的 Drawable 對象的樣式
- android:secondaryProgress:設(shè)置二級進(jìn)度條的進(jìn)度??匆曨l會有兩級進(jìn)度條,一級是播放進(jìn)度,二級是緩沖進(jìn)度。
值得注意的是max和progress屬性只有在水平進(jìn)度的時候才有效,ProgressBar默認(rèn)是圓形無限循環(huán)loading的樣式,在圓形樣式的情況下,max和progress是無效的。
當(dāng)然如果想要開發(fā)圓形樣式并且具有progress進(jìn)度顯示的進(jìn)度條,也是可以的。只需要按以下步驟:
1、首先我們的目的是想要使圓形的進(jìn)度條具有水平進(jìn)度條可以顯示進(jìn)度的功能,因此需要使用style屬性將進(jìn)度條的樣式改成水平進(jìn)度條的樣式
style="?android:attr/progressBarStyleHorizontal"
2、有了水平進(jìn)度條progress和max就有效了,但是還需要修改水平進(jìn)度條的progressDrawable,讓水平進(jìn)度條顯示為圓環(huán)形
- 在res/drawable目錄下新建widget_pg_style.xml文件,文件名可以自取。在此文件中定義水平進(jìn)度條的顯示樣式。
<?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 android:innerRadiusRatio="3.5" android:shape="ring" android:useLevel="false" android:type="sweep" android:thicknessRatio="12.0"> <solid android:color="@color/widget_track"/> </shape> </item> <item android:id="@android:id/progress"> <rotate android:pivotX="50%" android:pivotY="50%" android:fromDegrees="-90" android:toDegrees="-90"> <shape android:innerRadiusRatio="3.5" android:shape="ring" android:angle="0" android:type="sweep" android:thicknessRatio="12.0"> <solid android:color="#44aa00"/> </shape> </rotate> </item> </layer-list>
- 設(shè)置progressDrawable屬性值為widget_pg_style
android:progressDrawable="@drawable/widget_pg_style"
完整的ProgressBar代碼,關(guān)鍵就是上述的progressDrawable和style屬性文章來源:http://www.zghlxwxcb.cn/news/detail-401537.html
<ProgressBar
android:id="@+id/progressbar"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:layout_marginRight="15dp"
android:max="100"
android:progress="50"
android:indeterminate="false"
android:progressDrawable="@drawable/widget_pg_style"
style="?android:attr/progressBarStyleHorizontal"
/>
完成上述步驟后得到的就是一個可以自由設(shè)置進(jìn)度的圓形進(jìn)度條了,而不是一直無限加載的圓形加載進(jìn)度了。文章來源地址http://www.zghlxwxcb.cn/news/detail-401537.html
到了這里,關(guān)于android實現(xiàn)圓形的ProgressBar停止轉(zhuǎn)動的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!