Android使用Shape實現(xiàn)ProgressBar樣式

使用Shape實現(xiàn)樣式

使用Shape中的ring實現(xiàn),如下布局ring.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:thickness="5dp"
    android:useLevel="false" >
    <gradient
        android:endColor="#888"
        android:startColor="#ccc"
        android:type="sweep"
        android:useLevel="false" />
</shape>

使用rotate使之旋轉(zhuǎn)

如下progressbar.xml

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

注意:其中fromDegrees與toDegrees之差要是360的倍數(shù),不然中間會有轉(zhuǎn)的時候會跳一下,另外,這個差越大就轉(zhuǎn)得越快,具體效果大家可以自行試一下

使用

<ProgressBar
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:indeterminateDrawable="@drawable/progressbar"  <!-- 這里使用自定義的progressbar樣式 -->
/> 

更簡單的方法

直接使用一個布局文件就可以實現(xiàn):

<?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="1080" >

    <shape
        android:shape="ring"
        android:thickness="5dp"
        android:useLevel="false" >
        <gradient
            android:endColor="#888"
            android:startColor="#ccc"
            android:type="sweep"
            android:useLevel="false" />
    </shape>
</rotate>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容