美文网首页Android开发Android知识Android技术知识
Android使用Shape实现ProgressBar样式

Android使用Shape实现ProgressBar样式

作者: AlvinL | 来源:发表于2017-04-19 16:02 被阅读2330次

使用Shape实现样式

使用Shape中的ring实现,如下布局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使之旋转

如下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的倍数,不然中间会有转的时候会跳一下,另外,这个差越大就转得越快,具体效果大家可以自行试一下

使用

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

更简单的方法

直接使用一个布局文件就可以实现:

<?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>

相关文章

网友评论

    本文标题:Android使用Shape实现ProgressBar样式

    本文链接:https://www.haomeiwen.com/subject/mvkhzttx.html